Skip to content

Conversation

orestisfl
Copy link
Contributor

@orestisfl orestisfl commented Aug 11, 2025

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:

  • The 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 global TracerProvider and MeterProvider instances using a sync.Once to ensure they are set up only once.
  • The observability.StartSpan and observability.MeterFromContext helper functions have been removed. Callers now use the standard otel.Tracer(name).Start() and otel.Meter(name) to create spans and get meters directly.
  • All call sites have been updated to use the new approach, obtaining tracers and meters from the global OTel instance.
  • The SetUpOtel function no longer returns a context, as it now configures the global providers.

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.

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:
- The `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 global `TracerProvider` and `MeterProvider` instances using a `sync.Once` to ensure they are set up only once.
- The `observability.StartSpan` and `observability.MeterFromContext` helper functions have been removed. Callers now use the standard `otel.Tracer(name).Start()` and `otel.Meter(name)` to create spans and get meters directly.
- All call sites have been updated to use the new approach, obtaining tracers and meters from the global OTel instance.
- The `SetUpOtel` function no longer returns a context, as it now configures the global providers.
@orestisfl orestisfl self-assigned this Aug 11, 2025
@orestisfl orestisfl requested a review from a team as a code owner August 11, 2025 15:43
@orestisfl orestisfl linked an issue Aug 12, 2025 that may be closed by this pull request
5 tasks
@orestisfl orestisfl enabled auto-merge (squash) September 1, 2025 13:37
@orestisfl orestisfl merged commit e9f1a82 into elastic:main Sep 1, 2025
9 checks passed
@orestisfl orestisfl deleted the otel-no-ctx branch September 1, 2025 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OTel: Connect logs to traces
2 participants