Build OTel resource once instead of once per signal
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
Tracing
Metrics
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:
78cc85283addedK8sAttributesDetectorto all three call sites.c5ed376d5added theobservability.EnsureUserEnvVar()guard totracing/tracing.go:55,metrics/otel.go:52, andlogs/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 (?)
Source: traefik/traefik