#13921·traefik

Build OTel resource once instead of once per signal

Author: kasaderCreated Sep 17, 2026Updated Sep 17, 2026
Labelskind/enhancementcontributor/wantedarea/otel

Welcome!

  • Yes, I've searched similar issues on GitHub and didn't find any.
  • Yes, I've searched similar issues on the Traefik community forum and didn't find any.

What did you expect to see?

I was looking at the OpenTelemetry instrumentation and noticed that there's a lot of code duplication around sdk/resource creation. I expected the OTel resource to only be built once per process, but the current implementation builds it three times w/ three copies of the same block w/ just the service name changing

Logs

https://github.com/traefik/traefik/blob/e75250a31ae773e50e9c16514e3aebcb92059042/pkg/observability/types/logs.go#L206-L225

Tracing

https://github.com/traefik/traefik/blob/e75250a31ae773e50e9c16514e3aebcb92059042/pkg/observability/types/tracing.go#L75-L94

Metrics

https://github.com/traefik/traefik/blob/e75250a31ae773e50e9c16514e3aebcb92059042/pkg/observability/metrics/otel.go#L217-L236

Right now, with all the OTLP providers enabled, the container, host, OS, process, etc. detectors each run (3) times at startup, and the K8sAttributesDetector.Detect makes three separate calls to the Kubernetes API for the same pod also.

Fixes that had to be written three times as well:

  1. 78cc85283 added K8sAttributesDetector to all three call sites.
  2. c5ed376d5 added the observability.EnsureUserEnvVar() guard to tracing/tracing.go:55, metrics/otel.go:52, and logs/otel.go:22.

There could just be a single resource builder, NewResource(ctx context.Context, serviceName string, attrs map[string]string) in pkg/observability, w/ the detectors behind a sync.Once or something for caching. Then we can merge the service name/resource attributes onto the base, and it could eliminate the room for drift.

Happy to open a PR if this sounds reasonable (?)