Skip to content

Commit

Permalink
[telemetry] Add ability to set service.name for spans emitted by the …
Browse files Browse the repository at this point in the history
…Collector (#10490)

#### Description
This PR bridges the config that is exposed to Collector users to the
internal format expected by the config helpers around the tracer
provider.

#### Link to tracking issue
Fixes #10489

#### Testing
Manual testing performed.

Config:
```yaml
receivers:
  otlp:
    protocols:
      http:
      grpc:

exporters:
  debug:

service:
  pipelines:
    traces:
      receivers: [otlp]
      exporters: [debug]
    metrics:
      receivers: [otlp]
      exporters: [debug]
    logs:
      receivers: [otlp]
      exporters: [debug]
  telemetry:
    traces:
      processors:
        - batch:
            schedule_delay: 1000
            exporter:
              otlp:
                endpoint: https://otlp-gateway-prod-eu-west-2.grafana.net/otlp/v1/traces
                protocol: http/protobuf
                headers:
                  Authorization: "Basic ..."
    metrics:
      level: detailed
      readers:
        - periodic:
            exporter:
              otlp:
                endpoint: https://otlp-gateway-prod-eu-west-2.grafana.net/otlp/v1/metrics
                protocol: http/protobuf
                headers:
                  Authorization: "Basic ..."
    resource:
      "service.name": "otelcol-own-telemetry"
```

Sent this telemetry to the collector: 
```console
telemetrygen traces --traces 1 --otlp-insecure --otlp-attributes='cookbook="own-telemetry"'
```

Resulting in this:


![image](https://github.com/open-telemetry/opentelemetry-collector/assets/13387/b55d281b-8941-4b78-9f16-c08f495b6e89)


#### Documentation
None.

Signed-off-by: Juraci Paixão Kröhling <juraci@kroehling.de>

---------

Signed-off-by: Juraci Paixão Kröhling <juraci@kroehling.de>
  • Loading branch information
jpkrohling authored Jul 11, 2024
1 parent 09a929e commit 08b0be7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .chloggen/jpkroehling_fix-servicename-own-telemetry.yaml
Original file line number Diff line number Diff line change
@@ -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]
4 changes: 2 additions & 2 deletions service/telemetry/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}),
)
}
23 changes: 22 additions & 1 deletion service/telemetry/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
Expand Down

0 comments on commit 08b0be7

Please sign in to comment.