Skip to content

Commit

Permalink
Improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
grouzen committed May 20, 2023
1 parent 5bb78fe commit f871fb7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/opentelemetry-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
2 changes: 2 additions & 0 deletions docs/opentelemetry-instrumentation-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
23 changes: 16 additions & 7 deletions docs/opentelemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
)
```
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

}

0 comments on commit f871fb7

Please sign in to comment.