Skip to content
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

Remove PropagatingSupervisor #681

Merged
merged 2 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 23 additions & 23 deletions docs/opentelemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,35 +104,35 @@ ZIO.serviceWithZIO[Tracing] { tracing =>
}
```

### [Experimental] Usage with OpenTelemetry automatic instrumentation
### Usage with OpenTelemetry automatic instrumentation

OpenTelemetry provides
a [JVM agent for automatic instrumentation](https://opentelemetry.io/docs/instrumentation/java/automatic/) which
supports
many [popular Java libraries](https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/supported-libraries.md)
.
many [popular Java libraries](https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/supported-libraries.md).

This automatic instrumentation relies on the default OpenTelemetry context storage which is based on `ThreadLocal`. So
it doesn't work with ZIO out of the box.
Since [version 1.25.0](https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/tag/v1.25.0)
OpenTelemetry JVM agent supports ZIO.

`zio-opentelemetry` provides an experimental version of `Tracing` which bidirectionally propagates tracing context
between ZIO and non-ZIO code, enabling interoperability with _most_ libraries that use the default OpenTelemetry context
storage.
To enable interoperability between automatic instrumentation and `zio-opentelemetry`, `Tracing` has to be created
using `ContextStorage` backed by OpenTelemetry's `Context`.

To enable this experimental propagation, you will need to create `Tracing` using `Tracing.propagating` constructor (
instead of `Tracing.live`).

Please note that whether context propagation will work correctly depends on which specific ZIO wrappers around non-ZIO
libraries you are using. So please, test your specific setup.

It was reported that it works with:

* `zhttp`
* `sttp` with Java 11+ HTTP client backend
* `zio-kafka`
* `doobie`
* `redis4cats`
```scala
import zio.telemetry.opentelemetry.tracing.Tracing
import zio.telemetry.opentelemetry.context.ContextStorage
import zio.telemetry.opentelemetry.example.JaegerTracer
import io.opentelemetry.api.trace.{SpanKind, StatusCode}
import zio._

It was reported that it does not work with:
val errorMapper = ErrorMapper[Throwable] { case _ => StatusCode.UNSET }

* `sttp` with `armeria` backend
val app =
ZIO.serviceWithZIO[Tracing] { tracing =>
import tracing.aspects._
ZIO.logInfo("Hello") @@ root("root span", SpanKind.INTERNAL, errorMapper)
}.provide(
Tracing.live,
ContextStorage.openTelemetryContext, // <<<
JaegerTracer.live
)
```
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ object ContextStorage {
)

/**
* Uses OpenTelemetry's default context storage which is backed by a [[java.lang.ThreadLocal]]. This makes sense only
* if `zio.telemetry.opentelemetry.internal.PropagatingSupervisor` is used.
* Uses OpenTelemetry's context storage which is backed by a [[java.lang.ThreadLocal]]. This makes sense only if
* [[https://github.com/open-telemetry/opentelemetry-java-instrumentation OTEL instrumentation agent]] is used.
*/
val threadLocal: ULayer[ContextStorage] =
val openTelemetryContext: ULayer[ContextStorage] =
ZLayer.succeed {
new ContextStorage {
override def get(implicit trace: Trace): UIO[Context] = ZIO.succeed(Context.current())
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import io.opentelemetry.api.trace._
import io.opentelemetry.context.Context
import zio._
import zio.telemetry.opentelemetry.context.{ ContextStorage, IncomingContextCarrier, OutgoingContextCarrier }
import zio.telemetry.opentelemetry.internal.PropagatingSupervisor
import zio.telemetry.opentelemetry.tracing.propagation.TraceContextPropagator

import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -479,17 +478,6 @@ object Tracing {
} yield tracing
}

/**
* Tracing context will be bidirectionally propagated between ZIO and non-ZIO code.
*
* Since context propagation adds a performance overhead, it is recommended to use [[Tracing.live]] in most cases.
*
* [[Tracing.propagating]] should be used in combination with automatic instrumentation via OpenTelemetry JVM agent
* only.
*/
def propagating: URLayer[Tracer with ContextStorage, Tracing] =
Runtime.addSupervisor(new PropagatingSupervisor) ++ live

def scoped(tracer: Tracer, ctxStorage: ContextStorage): URIO[Scope, Tracing] = {
val acquire =
ZIO.succeed {
Expand Down