Skip to content
This repository was archived by the owner on Jul 28, 2026. It is now read-only.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions docs/configuration/tempo-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ automatic_logging:
[ process_attributes: <string array> ]
# Timeout on writing logs to Loki when backend is "logs_instance."
[ timeout: <duration> | 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 affects logs_instace backends
Comment thread
mapno marked this conversation as resolved.
Resolved
[ labels: <string array> ]
overrides:
[ logs_instance_tag: <string> | default = "tempo" ]
[ service_key: <string> | default = "svc" ]
Expand Down
3 changes: 3 additions & 0 deletions example/docker-compose/agent/config/agent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,8 @@ tempo:
spans: true
processes: true
roots: true
labels:
- svc
- status
spanmetrics:
prom_instance: tempo
60 changes: 51 additions & 9 deletions pkg/tempo/automaticloggingprocessor/automaticloggingprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type automaticLoggingProcessor struct {
logsInstance *logs.Instance
done atomic.Bool

labels map[string]struct{}

logger log.Logger
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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...)
}
}
}
Expand All @@ -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{}
}
Expand Down Expand Up @@ -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
Comment thread
mapno marked this conversation as resolved.
Resolved
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)
Expand All @@ -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
}
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -25,22 +26,22 @@ func TestSpanKeyVals(t *testing.T) {
expected: []interface{}{
"span", "",
"dur", "0ns",
"status", pdata.StatusCode(0),
"status", pdata.StatusCode(1),
},
},
{
spanName: "test",
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),
},
},
{
Expand All @@ -49,7 +50,7 @@ func TestSpanKeyVals(t *testing.T) {
expected: []interface{}{
"span", "",
"dur", "10ns",
"status", pdata.StatusCode(0),
"status", pdata.StatusCode(1),
},
},
{
Expand All @@ -58,7 +59,7 @@ func TestSpanKeyVals(t *testing.T) {
expected: []interface{}{
"span", "",
"dur", "90ns",
"status", pdata.StatusCode(0),
"status", pdata.StatusCode(1),
},
},
{
Expand All @@ -68,7 +69,7 @@ func TestSpanKeyVals(t *testing.T) {
expected: []interface{}{
"span", "",
"dur", "0ns",
"status", pdata.StatusCode(0),
"status", pdata.StatusCode(1),
},
},
{
Expand All @@ -81,7 +82,7 @@ func TestSpanKeyVals(t *testing.T) {
expected: []interface{}{
"span", "",
"dur", "0ns",
"status", pdata.StatusCode(0),
"status", pdata.StatusCode(1),
"xstr", "test",
},
},
Expand All @@ -96,7 +97,7 @@ func TestSpanKeyVals(t *testing.T) {
expected: []interface{}{
"a", "",
"c", "0ns",
"d", pdata.StatusCode(0),
"d", pdata.StatusCode(1),
},
},
}
Expand All @@ -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)
Expand Down Expand Up @@ -268,3 +270,72 @@ func TestLokiNameMigration(t *testing.T) {
require.NoError(t, err)
require.YAMLEq(t, expect, string(bb))
}

func TestLabels(t *testing.T) {
Comment thread
mapno marked this conversation as resolved.
Resolved
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)
})
}
}
1 change: 1 addition & 0 deletions pkg/tempo/automaticloggingprocessor/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down