diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a05f91eca75..491890bcbd76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ - [FEATURE] Add support for OTLP HTTP trace exporting. (@mapno) +- [FEATURE] Support autologging span attributes as log labels (@mapno) + - [ENHANCEMENT] The Grafana Agent Operator will now default to deploying the matching release version of the Grafana Agent instead of v0.14.0. (@rfratto) @@ -14,6 +16,9 @@ - [BUGFIX] Fix race condition that may occur and result in a panic when initializing scraping service cluster. (@rfratto) + +- [CHANGE] Breaking change: reduced verbosity of tracing autologging + by not logging `STATUS_CODE_UNSET` status codes. (@mapno) - [DEPRECATION] The `loki` key at the root of the config file has been deprecated in favor of `logs`. `loki`-named fields in `automatic_logging` diff --git a/docs/configuration/tempo-config.md b/docs/configuration/tempo-config.md index fa8b77f0d70a..7e93e4526fda 100644 --- a/docs/configuration/tempo-config.md +++ b/docs/configuration/tempo-config.md @@ -107,6 +107,11 @@ automatic_logging: [ process_attributes: ] # Timeout on writing logs to Loki when backend is "logs_instance." [ timeout: | default = 1ms ] + # Configures a set of key values that will be logged as labels + # They need to be span or process attributes logged in the log line + # + # This feature only applies when `backend = logs_instance` + [ labels: ] overrides: [ logs_instance_tag: | default = "tempo" ] [ service_key: | default = "svc" ] diff --git a/example/docker-compose/agent/config/agent.yaml b/example/docker-compose/agent/config/agent.yaml index bd6892bd6136..1e720021f1da 100644 --- a/example/docker-compose/agent/config/agent.yaml +++ b/example/docker-compose/agent/config/agent.yaml @@ -88,5 +88,8 @@ tempo: spans: true processes: true roots: true + labels: + - svc + - status spanmetrics: prom_instance: tempo diff --git a/pkg/tempo/automaticloggingprocessor/automaticloggingprocessor.go b/pkg/tempo/automaticloggingprocessor/automaticloggingprocessor.go index dcc2bb26e9a2..970467ff2a3a 100644 --- a/pkg/tempo/automaticloggingprocessor/automaticloggingprocessor.go +++ b/pkg/tempo/automaticloggingprocessor/automaticloggingprocessor.go @@ -47,6 +47,8 @@ type automaticLoggingProcessor struct { logsInstance *logs.Instance done atomic.Bool + labels map[string]struct{} + logger log.Logger } @@ -85,12 +87,18 @@ func newTraceProcessor(nextConsumer consumer.Traces, cfg *AutomaticLoggingConfig cfg.Overrides.DurationKey = override(cfg.Overrides.DurationKey, defaultDurationKey) cfg.Overrides.TraceIDKey = override(cfg.Overrides.TraceIDKey, defaultTraceIDKey) + labels := make(map[string]struct{}, len(cfg.Labels)) + for _, l := range cfg.Labels { + labels[l] = struct{}{} + } + return &automaticLoggingProcessor{ nextConsumer: nextConsumer, cfg: cfg, logToStdout: logToStdout, logger: logger, done: atomic.Bool{}, + labels: labels, }, nil } @@ -116,16 +124,19 @@ func (p *automaticLoggingProcessor) ConsumeTraces(ctx context.Context, td pdata. traceID := span.TraceID().HexString() if p.cfg.Spans { - p.exportToLogsInstance(typeSpan, traceID, append(p.spanKeyVals(span), p.processKeyVals(rs.Resource(), svc)...)...) + keyValues := append(p.spanKeyVals(span), p.processKeyVals(rs.Resource(), svc)...) + p.exportToLogsInstance(typeSpan, traceID, p.spanLabels(keyValues), keyValues...) } if p.cfg.Roots && span.ParentSpanID().IsEmpty() { - p.exportToLogsInstance(typeRoot, traceID, append(p.spanKeyVals(span), p.processKeyVals(rs.Resource(), svc)...)...) + keyValues := append(p.spanKeyVals(span), p.processKeyVals(rs.Resource(), svc)...) + p.exportToLogsInstance(typeRoot, traceID, p.spanLabels(keyValues), keyValues...) } if p.cfg.Processes && lastTraceID != traceID { lastTraceID = traceID - p.exportToLogsInstance(typeProcess, traceID, p.processKeyVals(rs.Resource(), svc)...) + keyValues := p.processKeyVals(rs.Resource(), svc) + p.exportToLogsInstance(typeProcess, traceID, p.spanLabels(keyValues), keyValues...) } } } @@ -134,6 +145,33 @@ func (p *automaticLoggingProcessor) ConsumeTraces(ctx context.Context, td pdata. return p.nextConsumer.ConsumeTraces(ctx, td) } +func (p *automaticLoggingProcessor) spanLabels(keyValues []interface{}) model.LabelSet { + if len(keyValues) == 0 { + return model.LabelSet{} + } + ls := make(map[model.LabelName]model.LabelValue, len(keyValues)/2) + var ( + k, v string + ok bool + ) + for i := 0; i < len(keyValues); i += 2 { + if k, ok = keyValues[i].(string); !ok { + // Should never happen, all keys are strings + level.Error(p.logger).Log("msg", "error casting label key to string", "key", keyValues[i]) + continue + } + // Try to cast value to string + if v, ok = keyValues[i+1].(string); !ok { + // If it's not a string, format it to its string representation + v = fmt.Sprintf("%v", keyValues[i+1]) + } + if _, ok := p.labels[k]; ok { + ls[model.LabelName(k)] = model.LabelValue(v) + } + } + return ls +} + func (p *automaticLoggingProcessor) Capabilities() consumer.Capabilities { return consumer.Capabilities{} } @@ -190,8 +228,11 @@ func (p *automaticLoggingProcessor) spanKeyVals(span pdata.Span) []interface{} { atts = append(atts, p.cfg.Overrides.DurationKey) atts = append(atts, spanDuration(span)) - atts = append(atts, p.cfg.Overrides.StatusKey) - atts = append(atts, span.Status().Code()) + // Skip STATUS_CODE_UNSET to be less spammy + if span.Status().Code() != pdata.StatusCodeUnset { + atts = append(atts, p.cfg.Overrides.StatusKey) + atts = append(atts, span.Status().Code()) + } for _, name := range p.cfg.SpanAttributes { att, ok := span.Attributes().Get(name) @@ -204,7 +245,7 @@ func (p *automaticLoggingProcessor) spanKeyVals(span pdata.Span) []interface{} { return atts } -func (p *automaticLoggingProcessor) exportToLogsInstance(kind string, traceID string, keyvals ...interface{}) { +func (p *automaticLoggingProcessor) exportToLogsInstance(kind string, traceID string, labels model.LabelSet, keyvals ...interface{}) { if p.done.Load() { return } @@ -222,10 +263,11 @@ func (p *automaticLoggingProcessor) exportToLogsInstance(kind string, traceID st return } + // Add logs instance label + labels[model.LabelName(p.cfg.Overrides.LogsTag)] = model.LabelValue(kind) + sent := p.logsInstance.SendEntry(api.Entry{ - Labels: model.LabelSet{ - model.LabelName(p.cfg.Overrides.LogsTag): model.LabelValue(kind), - }, + Labels: labels, Entry: logproto.Entry{ Timestamp: time.Now(), Line: string(line), diff --git a/pkg/tempo/automaticloggingprocessor/automaticloggingprocessor_test.go b/pkg/tempo/automaticloggingprocessor/automaticloggingprocessor_test.go index 5505d237cf87..42b2065345a7 100644 --- a/pkg/tempo/automaticloggingprocessor/automaticloggingprocessor_test.go +++ b/pkg/tempo/automaticloggingprocessor/automaticloggingprocessor_test.go @@ -6,6 +6,7 @@ import ( "github.com/grafana/agent/pkg/logs" "github.com/grafana/agent/pkg/util" + "github.com/prometheus/common/model" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/consumer/pdata" @@ -25,7 +26,7 @@ func TestSpanKeyVals(t *testing.T) { expected: []interface{}{ "span", "", "dur", "0ns", - "status", pdata.StatusCode(0), + "status", pdata.StatusCode(1), }, }, { @@ -33,14 +34,14 @@ func TestSpanKeyVals(t *testing.T) { expected: []interface{}{ "span", "test", "dur", "0ns", - "status", pdata.StatusCode(0), + "status", pdata.StatusCode(1), }, }, { expected: []interface{}{ "span", "", "dur", "0ns", - "status", pdata.StatusCode(0), + "status", pdata.StatusCode(1), }, }, { @@ -49,7 +50,7 @@ func TestSpanKeyVals(t *testing.T) { expected: []interface{}{ "span", "", "dur", "10ns", - "status", pdata.StatusCode(0), + "status", pdata.StatusCode(1), }, }, { @@ -58,7 +59,7 @@ func TestSpanKeyVals(t *testing.T) { expected: []interface{}{ "span", "", "dur", "90ns", - "status", pdata.StatusCode(0), + "status", pdata.StatusCode(1), }, }, { @@ -68,7 +69,7 @@ func TestSpanKeyVals(t *testing.T) { expected: []interface{}{ "span", "", "dur", "0ns", - "status", pdata.StatusCode(0), + "status", pdata.StatusCode(1), }, }, { @@ -81,7 +82,7 @@ func TestSpanKeyVals(t *testing.T) { expected: []interface{}{ "span", "", "dur", "0ns", - "status", pdata.StatusCode(0), + "status", pdata.StatusCode(1), "xstr", "test", }, }, @@ -96,7 +97,7 @@ func TestSpanKeyVals(t *testing.T) { expected: []interface{}{ "a", "", "c", "0ns", - "d", pdata.StatusCode(0), + "d", pdata.StatusCode(1), }, }, } @@ -112,6 +113,7 @@ func TestSpanKeyVals(t *testing.T) { span.Attributes().InitFromMap(tc.spanAttrs).Sort() span.SetStartTimestamp(pdata.TimestampFromTime(tc.spanStart)) span.SetEndTimestamp(pdata.TimestampFromTime(tc.spanEnd)) + span.Status().SetCode(pdata.StatusCodeOk) actual := p.(*automaticLoggingProcessor).spanKeyVals(span) assert.Equal(t, tc.expected, actual) @@ -268,3 +270,72 @@ func TestLokiNameMigration(t *testing.T) { require.NoError(t, err) require.YAMLEq(t, expect, string(bb)) } + +func TestLabels(t *testing.T) { + tests := []struct { + name string + labels []string + keyValues []interface{} + expectedLabels model.LabelSet + }{ + { + name: "happy case", + labels: []string{"loki", "svc"}, + keyValues: []interface{}{"loki", "loki", "svc", "gateway", "duration", "1s"}, + expectedLabels: map[model.LabelName]model.LabelValue{ + "loki": "loki", + "svc": "gateway", + }, + }, + { + name: "no labels", + labels: []string{}, + keyValues: []interface{}{"loki", "loki", "svc", "gateway", "duration", "1s"}, + expectedLabels: map[model.LabelName]model.LabelValue{}, + }, + { + name: "label not present in keyValues", + labels: []string{"loki", "svc"}, + keyValues: []interface{}{"loki", "loki", "duration", "1s"}, + expectedLabels: map[model.LabelName]model.LabelValue{ + "loki": "loki", + }, + }, + { + name: "label value is not type string", + labels: []string{"loki"}, + keyValues: []interface{}{"loki", 42, "duration", "1s"}, + expectedLabels: map[model.LabelName]model.LabelValue{ + "loki": "42", + }, + }, + { + name: "stringifies value if possible", + labels: []string{"status"}, + keyValues: []interface{}{"status", pdata.StatusCode(1)}, + expectedLabels: map[model.LabelName]model.LabelValue{ + "status": model.LabelValue(pdata.StatusCode(1).String()), + }, + }, + { + name: "no keyValues", + labels: []string{"status"}, + keyValues: []interface{}{}, + expectedLabels: map[model.LabelName]model.LabelValue{}, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + cfg := &AutomaticLoggingConfig{ + Spans: true, + Labels: tc.labels, + } + p, err := newTraceProcessor(&automaticLoggingProcessor{}, cfg) + require.NoError(t, err) + + ls := p.(*automaticLoggingProcessor).spanLabels(tc.keyValues) + assert.Equal(t, tc.expectedLabels, ls) + }) + } +} diff --git a/pkg/tempo/automaticloggingprocessor/factory.go b/pkg/tempo/automaticloggingprocessor/factory.go index 2aee79778602..4dd1c2911fbd 100644 --- a/pkg/tempo/automaticloggingprocessor/factory.go +++ b/pkg/tempo/automaticloggingprocessor/factory.go @@ -33,6 +33,7 @@ type AutomaticLoggingConfig struct { ProcessAttributes []string `mapstructure:"process_attributes" yaml:"process_attributes,omitempty"` Overrides OverrideConfig `mapstructure:"overrides" yaml:"overrides,omitempty"` Timeout time.Duration `mapstructure:"timeout" yaml:"timeout,omitempty"` + Labels []string `mapstructure:"labels" yaml:"labels,omitempty"` // Deprecated fields: LokiName string `mapstructure:"loki_name" yaml:"loki_name,omitempty"` // Superseded by LogsName