diff --git a/docs/opentelemetry-example.md b/docs/opentelemetry-example.md index 892f7b2e..a963cc3c 100644 --- a/docs/opentelemetry-example.md +++ b/docs/opentelemetry-example.md @@ -3,7 +3,7 @@ id: opentelemetry-example title: "OpenTelemetry Example" --- -You can find the source code [here](https://github.com/zio/zio-telemetry/tree/series/2.x/opentracing-example). +You can find the source code [here](https://github.com/zio/zio-telemetry/tree/series/2.x/opentelemtry-example). For an explanation in more detail, check the [OpenTracing Example](opentracing-example.md). diff --git a/docs/opentelemetry-instrumentation-example.md b/docs/opentelemetry-instrumentation-example.md index 0c11405c..93e8d1ea 100644 --- a/docs/opentelemetry-instrumentation-example.md +++ b/docs/opentelemetry-instrumentation-example.md @@ -3,6 +3,8 @@ id: opentelemetry-instrumentation-example title: "OpenTelemetry Automatic Instrumentation Example" --- +You can find the source code [here](https://github.com/zio/zio-telemetry/tree/series/2.x/opentelemetry-instrumentation-example). + Firstly, download OpenTelemetry JVM agent JAR: ```bash OTEL_AGENT_PATH=$(cs fetch --classpath "io.opentelemetry.javaagent:opentelemetry-javaagent:latest.release") diff --git a/docs/opentelemetry.md b/docs/opentelemetry.md index 8c954a33..1a7c48af 100644 --- a/docs/opentelemetry.md +++ b/docs/opentelemetry.md @@ -66,19 +66,22 @@ import zio._ ZIO.serviceWithZIO[Baggage] { baggage => val carrier = OutgoingContextCarrier.default() - for { + val upstream = for { // add new key/value into the baggage of current tracing context _ <- baggage.set("zio", "telemetry") // import current baggage data into carrier so it can be used by downstream consumer _ <- baggage.inject(BaggagePropagator.default, carrier) } yield () - for { + val downstream = for { // extract current baggage data from the carrier _ <- baggage.extract(BaggagePropagator.default, IncomingContextCarrier.default(carrier.kernel)) // get value from the extracted baggage data <- baggage.get("zio") } yield data + + upstream *> downstream + }.provide(Baggage.live, ContextStorage.fiberRef) ``` @@ -99,9 +102,15 @@ ZIO.serviceWithZIO[Tracing] { tracing => val propagator = TraceContextPropagator.default val kernel = mutable.Map().empty - tracing.inject(propagator, OutgoingContextCarrier.default(kernel)) @@ root("span of upstream service") *> + val upstream = + tracing.inject(propagator, OutgoingContextCarrier.default(kernel)) @@ root("span of upstream service") + + val downstream = extractSpan(propagator, IncomingContextCarrier.default(kernel), "span of downstream service") -} + + upstream *> downstream + +}.provide(Tracing.live, ContextStorage.fiberRef, JaegerTracer.live) ``` ### Usage with OpenTelemetry automatic instrumentation @@ -115,7 +124,7 @@ Since [version 1.25.0](https://github.com/open-telemetry/opentelemetry-java-inst OpenTelemetry JVM agent supports ZIO. To enable interoperability between automatic instrumentation and `zio-opentelemetry`, `Tracing` has to be created -using `ContextStorage` backed by OpenTelemetry's `Context`. +using `ContextStorage` backed by OpenTelemetry's `Context` and `Tracer` provided by globally registered `TracerProvider`. ```scala import zio.telemetry.opentelemetry.tracing.Tracing @@ -132,7 +141,7 @@ val app = ZIO.logInfo("Hello") @@ root("root span", SpanKind.INTERNAL, errorMapper) }.provide( Tracing.live, - ContextStorage.openTelemetryContext, // <<< - JaegerTracer.live + ContextStorage.openTelemetryContext, // <<< ContextStorage + ZLayer.fromZIO(ZIO.attempt(GlobalOpenTelemetry.getTracer("hello"))) // <<< Tracer ) ``` diff --git a/opentelemetry-instrumentation-example/src/main/scala/zio/telemetry/opentelemetry/instrumentation/example/ClientApp.scala b/opentelemetry-instrumentation-example/src/main/scala/zio/telemetry/opentelemetry/instrumentation/example/ClientApp.scala index 3be78977..be17d994 100644 --- a/opentelemetry-instrumentation-example/src/main/scala/zio/telemetry/opentelemetry/instrumentation/example/ClientApp.scala +++ b/opentelemetry-instrumentation-example/src/main/scala/zio/telemetry/opentelemetry/instrumentation/example/ClientApp.scala @@ -5,11 +5,13 @@ import zio.config.magnolia.descriptor import zio.config.typesafe.TypesafeConfig import zio.telemetry.opentelemetry.instrumentation.example.config.AppConfig import zio._ +import zio.config.ReadError import zio.telemetry.opentelemetry.instrumentation.example.http.HttpClient object ClientApp extends ZIOAppDefault { - private val configLayer = TypesafeConfig.fromResourcePath(descriptor[AppConfig]) + private val configLayer: Layer[ReadError[String], AppConfig] = + TypesafeConfig.fromResourcePath(descriptor[AppConfig]) private val httpBackendLayer: TaskLayer[Backend] = ZLayer.scoped { diff --git a/opentelemetry-instrumentation-example/src/main/scala/zio/telemetry/opentelemetry/instrumentation/example/ServerApp.scala b/opentelemetry-instrumentation-example/src/main/scala/zio/telemetry/opentelemetry/instrumentation/example/ServerApp.scala index 119030b6..9044d07e 100644 --- a/opentelemetry-instrumentation-example/src/main/scala/zio/telemetry/opentelemetry/instrumentation/example/ServerApp.scala +++ b/opentelemetry-instrumentation-example/src/main/scala/zio/telemetry/opentelemetry/instrumentation/example/ServerApp.scala @@ -1,7 +1,9 @@ package zio.telemetry.opentelemetry.instrumentation.example import io.opentelemetry.api.GlobalOpenTelemetry +import io.opentelemetry.api.trace.Tracer import zio._ +import zio.config.ReadError import zio.config.typesafe.TypesafeConfig import zio.config.magnolia._ import zio.telemetry.opentelemetry.context.ContextStorage @@ -11,10 +13,13 @@ import zio.telemetry.opentelemetry.tracing.Tracing object ServerApp extends ZIOAppDefault { - private val configLayer = TypesafeConfig.fromResourcePath(descriptor[AppConfig]) + private val configLayer: Layer[ReadError[String], AppConfig] = + TypesafeConfig.fromResourcePath(descriptor[AppConfig]) - private val globalTracerLayer = - ZLayer.succeed(GlobalOpenTelemetry.getTracer("zio.telemetry.opentelemetry.instrumentation.example.ServerApp")) + private val globalTracerLayer: TaskLayer[Tracer] = + ZLayer.fromZIO( + ZIO.attempt(GlobalOpenTelemetry.getTracer("zio.telemetry.opentelemetry.instrumentation.example.ServerApp")) + ) override def run: Task[ExitCode] = ZIO diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 1f99c8a1..8bc4910e 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -92,10 +92,7 @@ object Dependencies { ) lazy val opentelemetryInstrumentationExample = example ++ Seq( - Orgs.softwaremillSttpClient3 %% "zio" % ExampleVersions.sttp3, - Orgs.opentelemetry % "opentelemetry-exporter-jaeger" % Versions.opentelemetry, - Orgs.opentelemetry % "opentelemetry-sdk" % Versions.opentelemetry, - Orgs.grpc % "grpc-netty-shaded" % ExampleVersions.grpcNetty + Orgs.softwaremillSttpClient3 %% "zio" % ExampleVersions.sttp3 ) }