feat(OTel): Use global providers #3516
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary of your changes
This commit refactors the OpenTelemetry (OTel) instrumentation to use global tracer and meter providers instead of passing them through the context. This simplifies the observability setup and removes the need for custom wrappers around OTel functions. Additionally,
tracer.Start()
is simpler to use than the previous wrapper because it only takes one string argument instead of two.This approach is idiomatic in OpenTelemetry-Go. OTel uses a delegation pattern where a default, no-op provider is initially present. When the application later configures and sets the actual provider, the default provider delegates all calls to the new, configured one. This design makes the initialization order flexible and robust, ensuring all components use the same fully-configured provider, regardless of when they were initialized. See: https://seth-shi.medium.com/why-can-otel-gettracerprovider-5bfbc73db828
Key changes:
internal/infra/observability/context.go
file has been removed, as providers are no longer stored in the context.internal/infra/observability/otel.go
now initializes and manages globalTracerProvider
andMeterProvider
instances using async.Once
to ensure they are set up only once.observability.StartSpan
andobservability.MeterFromContext
helper functions have been removed. Callers now use the standardotel.Tracer(name).Start()
andotel.Meter(name)
to create spans and get meters directly.SetUpOtel
function no longer returns a context, as it now configures the global providers.