-
Notifications
You must be signed in to change notification settings - Fork 58
feat: LogSpanner — pluggable span dispatch bridging ZIO.logSpan with OTEL (#1022) #1138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
li-nkSN
wants to merge
1
commit into
zio:v4.0.0-rc
Choose a base branch
from
li-nkSN:feature/logspanner-1022
base: v4.0.0-rc
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...opentelemetry/core/src/main/scala/zio/telemetry/opentelemetry/core/trace/LogSpanner.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package zio.telemetry.opentelemetry.core.trace | ||
|
|
||
| import zio._ | ||
|
|
||
| /** | ||
| * A pluggable span dispatch mechanism that bridges ZIO's `logSpan` with OpenTelemetry spans. | ||
| * | ||
| * By default, `LogSpanner.span("name")` delegates to `ZIO.logSpan`, adding zero OTEL overhead. When an OTEL-backed | ||
| * `LogSpanner` is installed (via `LogSpanner.installOtel` or `LogSpanner.installHybrid`), the same call creates real | ||
| * OTEL spans instead. | ||
| * | ||
| * This allows library code to use `LogSpanner.span` without depending on `Tracer`, while application code installs the | ||
| * OTEL backend at the edge. | ||
| * | ||
| * Implements the design proposed in zio-telemetry #1022. | ||
| */ | ||
| trait LogSpanner { | ||
|
|
||
| /** | ||
| * Wraps an effect with a named span. | ||
| * | ||
| * The semantics depend on the installed backend: | ||
| * - Default: delegates to `ZIO.logSpan` (ZIO log-based spans only) | ||
| * - OTEL: creates a real OpenTelemetry span via `Tracer.span` | ||
| * - Hybrid: creates both an OTEL span and a ZIO logSpan | ||
| * | ||
| * @param name | ||
| * the span name | ||
| * @param zio | ||
| * the effect to wrap | ||
| */ | ||
| def logSpan[R, E, A](name: String)(zio: => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] | ||
| } | ||
|
|
||
| object LogSpanner { | ||
|
|
||
| /** | ||
| * Default implementation that delegates to `ZIO.logSpan`. Zero OTEL overhead — no spans are exported. | ||
| */ | ||
| private[opentelemetry] val default: LogSpanner = new LogSpanner { | ||
|
|
||
| override def logSpan[R, E, A](name: String)(zio: => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] = | ||
| ZIO.logSpan(name)(zio) | ||
| } | ||
|
|
||
| /** | ||
| * Global FiberRef storing the current LogSpanner. | ||
| * | ||
| * Uses the same `Unsafe.unsafe { FiberRef.unsafe.make }` pattern as `FiberRef.currentLogSpan` in ZIO core. This | ||
| * ensures the FiberRef is available at module initialization time without requiring a ZIO runtime. | ||
| * | ||
| * IMPORTANT: `default` must be defined before this val to avoid null initialization. | ||
| */ | ||
| private[opentelemetry] val currentLogSpanner: FiberRef[LogSpanner] = | ||
| Unsafe.unsafe { implicit unsafe => | ||
| FiberRef.unsafe.make[LogSpanner](LogSpanner.default) | ||
| } | ||
|
|
||
| /** | ||
| * Installs a `LogSpanner` that produces real OTEL spans via `Tracer.span`. | ||
| * | ||
| * Does NOT call `ZIO.logSpan` — span information is only visible through OTEL exporters. | ||
| * | ||
| * @param tracer | ||
| * the zio-telemetry Tracer to use for span creation | ||
| * @return | ||
| * a scoped effect that installs the OTEL backend and reverts on scope close | ||
| */ | ||
| def installOtel(tracer: Tracer)(implicit trace: Trace): ZIO[Scope, Nothing, Unit] = { | ||
| val logSpanner = new LogSpanner { | ||
| override def logSpan[R, E, A](name: String)(zio: => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] = | ||
| tracer.span(name)(_ => zio) | ||
| } | ||
|
|
||
| currentLogSpanner.locallyScoped(logSpanner) | ||
| } | ||
|
|
||
| /** | ||
| * Installs a `LogSpanner` that produces both OTEL spans AND ZIO logSpans. | ||
| * | ||
| * This gives dual visibility: OTEL spans for distributed tracing exporters, and ZIO logSpans for ZIO's built-in log | ||
| * output (e.g., for local development). | ||
| * | ||
| * @param tracer | ||
| * the zio-telemetry Tracer to use for span creation | ||
| * @return | ||
| * a scoped effect that installs the hybrid backend and reverts on scope close | ||
| */ | ||
| def installHybrid(tracer: Tracer)(implicit trace: Trace): ZIO[Scope, Nothing, Unit] = { | ||
| val logSpanner = new LogSpanner { | ||
| override def logSpan[R, E, A](name: String)(zio: => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] = | ||
| tracer.span(name)(_ => ZIO.logSpan(name)(zio)) | ||
| } | ||
|
|
||
| currentLogSpanner.locallyScoped(logSpanner) | ||
| } | ||
|
|
||
| /** | ||
| * A `ZIOAspect` that wraps an effect with a named span using the currently installed `LogSpanner`. | ||
| * | ||
| * This is the primary call-site API for library code that should not depend on `Tracer` or `OpenTelemetry`. | ||
| * | ||
| * Usage: | ||
| * {{{ | ||
| * myEffect @@ LogSpanner.span("operationName") | ||
| * }}} | ||
| * | ||
| * @param spanName | ||
| * the span name | ||
| * @return | ||
| * a ZIOAspect that applies the span | ||
| */ | ||
| def span(spanName: String): ZIOAspect[Nothing, Any, Nothing, Any, Nothing, Any] = | ||
| new ZIOAspect[Nothing, Any, Nothing, Any, Nothing, Any] { | ||
| override def apply[R, E, A](zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] = | ||
| currentLogSpanner.getWith(_.logSpan(spanName)(zio)) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I probably missed mentioning this one in my PR, but it can be removed since we basically moved it to
OpenTelemetry.logSpan. See the next comment below