From a534d10f8da731e03735139854c9781d73f4b79e Mon Sep 17 00:00:00 2001 From: Filip Petkovski Date: Tue, 30 Apr 2024 11:37:21 +0200 Subject: [PATCH 1/2] Allow specifying OTLP resource attributes for traces This commit adds a resource_attributes field to the OTLP tracing configuration. Signed-off-by: Filip Petkovski --- docs/tracing.md | 1 + pkg/tracing/otlp/config_yaml.go | 1 + pkg/tracing/otlp/otlp.go | 31 +++++++++++++++++-------------- pkg/tracing/otlp/otlp_test.go | 8 ++------ 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/docs/tracing.md b/docs/tracing.md index e87a1fa0f3..3e052fe84a 100644 --- a/docs/tracing.md +++ b/docs/tracing.md @@ -79,6 +79,7 @@ type: OTLP config: client_type: "" service_name: "" + resource_attributes: {} reconnection_period: 0s compression: "" insecure: false diff --git a/pkg/tracing/otlp/config_yaml.go b/pkg/tracing/otlp/config_yaml.go index b8aa6f9dc8..abce5370e2 100644 --- a/pkg/tracing/otlp/config_yaml.go +++ b/pkg/tracing/otlp/config_yaml.go @@ -22,6 +22,7 @@ type retryConfig struct { type Config struct { ClientType string `yaml:"client_type"` ServiceName string `yaml:"service_name"` + ResourceAttributes map[string]string `yaml:"resource_attributes"` ReconnectionPeriod time.Duration `yaml:"reconnection_period"` Compression string `yaml:"compression"` Insecure bool `yaml:"insecure"` diff --git a/pkg/tracing/otlp/otlp.go b/pkg/tracing/otlp/otlp.go index bf29d3622b..cb5220240a 100644 --- a/pkg/tracing/otlp/otlp.go +++ b/pkg/tracing/otlp/otlp.go @@ -8,6 +8,8 @@ import ( "strconv" "strings" + "go.opentelemetry.io/otel/attribute" + "github.com/thanos-io/thanos/pkg/tracing/migration" "github.com/go-kit/log" @@ -70,26 +72,27 @@ func NewTracerProvider(ctx context.Context, logger log.Logger, conf []byte) (*tr if err != nil { logger.Log(err) } - tp := newTraceProvider(ctx, processor, logger, config.ServiceName, sampler) + tp := newTraceProvider(ctx, processor, logger, config.ServiceName, config.ResourceAttributes, sampler) return tp, nil } -func newTraceProvider(ctx context.Context, processor tracesdk.SpanProcessor, logger log.Logger, serviceName string, sampler tracesdk.Sampler) *tracesdk.TracerProvider { - var ( - r *resource.Resource - err error - ) +func newTraceProvider( + ctx context.Context, + processor tracesdk.SpanProcessor, + logger log.Logger, + serviceName string, + attrs map[string]string, + sampler tracesdk.Sampler, +) *tracesdk.TracerProvider { + resourceAttrs := make([]attribute.KeyValue, 0, len(attrs)+1) if serviceName != "" { - r, err = resource.New( - ctx, - resource.WithAttributes(semconv.ServiceNameKey.String(serviceName)), - ) - } else { - r, err = resource.New( - ctx, - ) + resourceAttrs = append(resourceAttrs, semconv.ServiceNameKey.String(serviceName)) + } + for k, v := range attrs { + resourceAttrs = append(resourceAttrs, attribute.String(k, v)) } + r, err := resource.New(ctx, resource.WithAttributes(resourceAttrs...)) if err != nil { level.Warn(logger).Log("msg", "jaeger: detecting resources for tracing provider failed", "err", err) } diff --git a/pkg/tracing/otlp/otlp_test.go b/pkg/tracing/otlp/otlp_test.go index 1f651e9c87..c0c0c8c0dc 100644 --- a/pkg/tracing/otlp/otlp_test.go +++ b/pkg/tracing/otlp/otlp_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/efficientgo/core/testutil" + "github.com/thanos-io/thanos/pkg/tracing" "github.com/thanos-io/thanos/pkg/tracing/migration" @@ -20,12 +21,7 @@ import ( func TestContextTracing_ClientEnablesTracing(t *testing.T) { exp := tracetest.NewInMemoryExporter() - tracerOtel := newTraceProvider( - context.Background(), - tracesdk.NewSimpleSpanProcessor(exp), - log.NewNopLogger(), - "thanos", - tracesdk.AlwaysSample()) + tracerOtel := newTraceProvider(context.Background(), tracesdk.NewSimpleSpanProcessor(exp), log.NewNopLogger(), "thanos", nil, tracesdk.AlwaysSample()) tracer, _ := migration.Bridge(tracerOtel, log.NewNopLogger()) clientRoot, _ := tracing.StartSpan(tracing.ContextWithTracer(context.Background(), tracer), "a") From 5960dd6d97e57cfed3c63a65534bb1b652aa0625 Mon Sep 17 00:00:00 2001 From: Filip Petkovski Date: Tue, 30 Apr 2024 11:47:48 +0200 Subject: [PATCH 2/2] Add CHANGELOG entry Signed-off-by: Filip Petkovski --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f538344d6e..a0b2c53490 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re ### Added +- [#7317](https://github.com/thanos-io/thanos/pull/7317) Tracing: allow specifying resource attributes for the OTLP configuration. + ### Changed ### Removed