Use ResourceAttributes instead of GlobalAttributes

This commit is contained in:
Bruno de Queiroz 2025-02-06 11:24:04 +01:00 committed by GitHub
parent da2278b29a
commit b74767bfa4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 6 deletions

View file

@ -25,7 +25,7 @@ import (
// Backend is an abstraction for tracking backend (OpenTelemetry, ...). // Backend is an abstraction for tracking backend (OpenTelemetry, ...).
type Backend interface { type Backend interface {
Setup(serviceName string, sampleRate float64, globalAttributes map[string]string) (trace.Tracer, io.Closer, error) Setup(serviceName string, sampleRate float64, resourceAttributes map[string]string) (trace.Tracer, io.Closer, error)
} }
// NewTracing Creates a Tracing. // NewTracing Creates a Tracing.
@ -44,7 +44,7 @@ func NewTracing(conf *static.Tracing) (*Tracer, io.Closer, error) {
otel.SetTextMapPropagator(autoprop.NewTextMapPropagator()) otel.SetTextMapPropagator(autoprop.NewTextMapPropagator())
tr, closer, err := backend.Setup(conf.ServiceName, conf.SampleRate, conf.GlobalAttributes) tr, closer, err := backend.Setup(conf.ServiceName, conf.SampleRate, conf.ResourceAttributes)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }

View file

@ -73,6 +73,7 @@ func TestTracing(t *testing.T) {
desc string desc string
propagators string propagators string
headers map[string]string headers map[string]string
resourceAttributes map[string]string
wantServiceHeadersFn func(t *testing.T, headers http.Header) wantServiceHeadersFn func(t *testing.T, headers http.Header)
assertFn func(*testing.T, string) assertFn func(*testing.T, string)
}{ }{
@ -85,6 +86,17 @@ func TestTracing(t *testing.T) {
assert.Regexp(t, `({"key":"service.version","value":{"stringValue":"dev"}})`, trace) assert.Regexp(t, `({"key":"service.version","value":{"stringValue":"dev"}})`, trace)
}, },
}, },
{
desc: "resource attributes must be propagated",
resourceAttributes: map[string]string{
"service.environment": "custom",
},
assertFn: func(t *testing.T, trace string) {
t.Helper()
assert.Regexp(t, `({"key":"service.environment","value":{"stringValue":"custom"}})`, trace)
},
},
{ {
desc: "TraceContext propagation", desc: "TraceContext propagation",
propagators: "tracecontext", propagators: "tracecontext",
@ -328,8 +340,9 @@ func TestTracing(t *testing.T) {
}) })
tracingConfig := &static.Tracing{ tracingConfig := &static.Tracing{
ServiceName: "traefik", ServiceName: "traefik",
SampleRate: 1.0, SampleRate: 1.0,
ResourceAttributes: test.resourceAttributes,
OTLP: &types.OTelTracing{ OTLP: &types.OTelTracing{
HTTP: &types.OTelHTTP{ HTTP: &types.OTelHTTP{
Endpoint: collector.URL, Endpoint: collector.URL,

View file

@ -36,7 +36,7 @@ func (c *OTelTracing) SetDefaults() {
} }
// Setup sets up the tracer. // Setup sets up the tracer.
func (c *OTelTracing) Setup(serviceName string, sampleRate float64, globalAttributes map[string]string) (trace.Tracer, io.Closer, error) { func (c *OTelTracing) Setup(serviceName string, sampleRate float64, resourceAttributes map[string]string) (trace.Tracer, io.Closer, error) {
var ( var (
err error err error
exporter *otlptrace.Exporter exporter *otlptrace.Exporter
@ -55,7 +55,7 @@ func (c *OTelTracing) Setup(serviceName string, sampleRate float64, globalAttrib
semconv.ServiceVersionKey.String(version.Version), semconv.ServiceVersionKey.String(version.Version),
} }
for k, v := range globalAttributes { for k, v := range resourceAttributes {
attr = append(attr, attribute.String(k, v)) attr = append(attr, attribute.String(k, v))
} }