diff --git a/.chloggen/jpkroehling_fix-servicename-own-telemetry.yaml b/.chloggen/jpkroehling_fix-servicename-own-telemetry.yaml new file mode 100644 index 00000000000..0fe7a6f6685 --- /dev/null +++ b/.chloggen/jpkroehling_fix-servicename-own-telemetry.yaml @@ -0,0 +1,4 @@ +change_type: bug_fix +component: service/telemetry +note: Add ability to set service.name for spans emitted by the Collector +issues: [10489] diff --git a/service/telemetry/factory.go b/service/telemetry/factory.go index c236d3c5733..edcca9e373f 100644 --- a/service/telemetry/factory.go +++ b/service/telemetry/factory.go @@ -51,9 +51,9 @@ func NewFactory() Factory { c := *cfg.(*Config) return newLogger(c.Logs, set.ZapOptions) }), - internal.WithTracerProvider(func(ctx context.Context, _ Settings, cfg component.Config) (trace.TracerProvider, error) { + internal.WithTracerProvider(func(ctx context.Context, set Settings, cfg component.Config) (trace.TracerProvider, error) { c := *cfg.(*Config) - return newTracerProvider(ctx, c) + return newTracerProvider(ctx, set, c) }), ) } diff --git a/service/telemetry/tracer.go b/service/telemetry/tracer.go index d235ca3aaf2..85fd89e8c6a 100644 --- a/service/telemetry/tracer.go +++ b/service/telemetry/tracer.go @@ -11,6 +11,7 @@ import ( "go.opentelemetry.io/contrib/propagators/b3" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/propagation" + semconv "go.opentelemetry.io/otel/semconv/v1.26.0" "go.opentelemetry.io/otel/trace" ) @@ -25,11 +26,31 @@ var ( ) // New creates a new Telemetry from Config. -func newTracerProvider(ctx context.Context, cfg Config) (trace.TracerProvider, error) { +func newTracerProvider(ctx context.Context, set Settings, cfg Config) (trace.TracerProvider, error) { + attrs := map[string]interface{}{ + string(semconv.ServiceNameKey): set.BuildInfo.Version, + } + for k, v := range cfg.Resource { + if v != nil { + attrs[k] = *v + } + + // the new value is nil, delete the existing key + if _, ok := attrs[k]; ok && v == nil { + delete(attrs, k) + } + } + sch := semconv.SchemaURL + res := config.Resource{ + SchemaUrl: &sch, + Attributes: attrs, + } + sdk, err := config.NewSDK( config.WithContext(ctx), config.WithOpenTelemetryConfiguration( config.OpenTelemetryConfiguration{ + Resource: &res, TracerProvider: &config.TracerProvider{ Processors: cfg.Traces.Processors, // TODO: once https://github.com/open-telemetry/opentelemetry-configuration/issues/83 is resolved,