Skip to content

Commit

Permalink
job name as const
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiangreco committed Nov 27, 2024
1 parent 081d4ad commit d0cfb84
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package database_observability

const JobName = "integrations/db-o11y"
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/xwb1989/sqlparser"

"github.com/grafana/alloy/internal/component/common/loki"
"github.com/grafana/alloy/internal/component/database_observability"
"github.com/grafana/alloy/internal/runtime/logging/level"
"github.com/grafana/loki/v3/pkg/logproto"
)
Expand Down Expand Up @@ -118,7 +119,7 @@ func (c *QuerySample) fetchQuerySamples(ctx context.Context) error {
}

c.entryHandler.Chan() <- loki.Entry{
Labels: model.LabelSet{"job": "integrations/db-o11y"},
Labels: model.LabelSet{"job": database_observability.JobName},
Entry: logproto.Entry{
Timestamp: time.Unix(0, time.Now().UnixNano()),
Line: fmt.Sprintf(`level=info msg="query samples fetched" op="%s" digest="%s" query_sample_seen="%s" query_sample_timer_wait="%s" query_sample_redacted="%s"`, OP_QUERY_SAMPLE, digest, query_sample_seen, query_sample_timer_wait, query_sample_redacted),
Expand All @@ -128,7 +129,7 @@ func (c *QuerySample) fetchQuerySamples(ctx context.Context) error {
tables := c.tablesFromQuery(query_sample_text)
for _, table := range tables {
c.entryHandler.Chan() <- loki.Entry{
Labels: model.LabelSet{"job": "integrations/db-o11y"},
Labels: model.LabelSet{"job": database_observability.JobName},
Entry: logproto.Entry{
Timestamp: time.Unix(0, time.Now().UnixNano()),
Line: fmt.Sprintf(`level=info msg="table name parsed" op="%s" digest="%s" table="%s"`, OP_QUERY_PARSED_TABLE_NAME, digest, table),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

loki_fake "github.com/grafana/alloy/internal/component/common/loki/client/fake"
"github.com/grafana/alloy/internal/component/database_observability"
"github.com/prometheus/common/model"
"go.uber.org/goleak"

Expand Down Expand Up @@ -59,7 +60,7 @@ func TestQuerySample(t *testing.T) {

lokiEntries := lokiClient.Received()
for _, entry := range lokiEntries {
require.Equal(t, model.LabelSet{"job": "integrations/db-o11y"}, entry.Labels)
require.Equal(t, model.LabelSet{"job": database_observability.JobName}, entry.Labels)
}
require.Equal(t, `level=info msg="query samples fetched" op="query_sample" digest="abc123" query_sample_seen="2024-01-01T00:00:00.000Z" query_sample_timer_wait="1000" query_sample_redacted="select * from some_table where id = :redacted1"`, lokiEntries[0].Line)
require.Equal(t, `level=info msg="table name parsed" op="query_parsed_table_name" digest="abc123" table="some_table"`, lokiEntries[1].Line)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/prometheus/common/model"

"github.com/grafana/alloy/internal/component/common/loki"
"github.com/grafana/alloy/internal/component/database_observability"
"github.com/grafana/alloy/internal/runtime/logging/level"
)

Expand Down Expand Up @@ -146,7 +147,7 @@ func (c *SchemaTable) extractSchema(ctx context.Context) error {
schemas = append(schemas, schema)

c.entryHandler.Chan() <- loki.Entry{
Labels: model.LabelSet{"job": "integrations/db-o11y"},
Labels: model.LabelSet{"job": database_observability.JobName},
Entry: logproto.Entry{
Timestamp: time.Unix(0, time.Now().UnixNano()),
Line: fmt.Sprintf(`level=info msg="schema detected" op="%s" schema="%s"`, OP_SCHEMA_DETECTION, schema),
Expand Down Expand Up @@ -179,7 +180,7 @@ func (c *SchemaTable) extractSchema(ctx context.Context) error {
tables = append(tables, tableInfo{schema: schema, tableName: table, createTime: createTime, updateTime: updateTime})

c.entryHandler.Chan() <- loki.Entry{
Labels: model.LabelSet{"job": "integrations/db-o11y"},
Labels: model.LabelSet{"job": database_observability.JobName},
Entry: logproto.Entry{
Timestamp: time.Unix(0, time.Now().UnixNano()),
Line: fmt.Sprintf(`level=info msg="table detected" op="%s" schema="%s" table="%s"`, OP_TABLE_DETECTION, schema, table),
Expand Down Expand Up @@ -215,7 +216,7 @@ func (c *SchemaTable) extractSchema(ctx context.Context) error {
c.cache.Add(cacheKey, table)

c.entryHandler.Chan() <- loki.Entry{
Labels: model.LabelSet{"job": "integrations/db-o11y"},
Labels: model.LabelSet{"job": database_observability.JobName},
Entry: logproto.Entry{
Timestamp: time.Unix(0, time.Now().UnixNano()),
Line: fmt.Sprintf(`level=info msg="create table" op="%s" schema="%s" table="%s" create_statement="%s"`, OP_CREATE_STATEMENT, table.schema, table.tableName, createStmt),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/DATA-DOG/go-sqlmock"
"github.com/go-kit/log"
loki_fake "github.com/grafana/alloy/internal/component/common/loki/client/fake"
"github.com/grafana/alloy/internal/component/database_observability"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
Expand Down Expand Up @@ -76,7 +77,7 @@ func TestSchemaTable(t *testing.T) {

lokiEntries := lokiClient.Received()
for _, entry := range lokiEntries {
require.Equal(t, model.LabelSet{"job": "integrations/db-o11y"}, entry.Labels)
require.Equal(t, model.LabelSet{"job": database_observability.JobName}, entry.Labels)
}
require.Equal(t, `level=info msg="schema detected" op="schema_detection" schema="some_schema"`, lokiEntries[0].Line)
require.Equal(t, `level=info msg="table detected" op="table_detection" schema="some_schema" table="some_table"`, lokiEntries[1].Line)
Expand Down
3 changes: 2 additions & 1 deletion internal/component/database_observability/mysql/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/grafana/alloy/internal/component"
"github.com/grafana/alloy/internal/component/common/loki"
"github.com/grafana/alloy/internal/component/database_observability"
"github.com/grafana/alloy/internal/component/database_observability/mysql/collector"
"github.com/grafana/alloy/internal/component/discovery"
"github.com/grafana/alloy/internal/featuregate"
Expand Down Expand Up @@ -157,7 +158,7 @@ func (c *Component) getBaseTarget() (discovery.Target, error) {
model.SchemeLabel: "http",
model.MetricsPathLabel: path.Join(httpData.HTTPPathForComponent(c.opts.ID), "metrics"),
"instance": c.instanceKey(),
"job": "integrations/db-o11y",
"job": database_observability.JobName,
}, nil
}

Expand Down

0 comments on commit d0cfb84

Please sign in to comment.