Skip to content

Commit

Permalink
Update opentelemetry.md docs
Browse files Browse the repository at this point in the history
  • Loading branch information
grouzen committed Nov 16, 2023
1 parent 23b8abf commit fd53a65
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions docs/opentelemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import zio.telemetry.opentelemetry.example.JaegerTracer
import io.opentelemetry.api.trace.{ SpanKind, StatusCode }
import zio._

val instrumentationScopeName = "com.example.MyApp"
val resourceName = "example-app"

val statusMapper = StatusMapper.failureThrowable(_ => StatusCode.UNSET)

val app = ZIO.serviceWithZIO[Tracing] { tracing =>
Expand All @@ -44,7 +47,7 @@ val app = ZIO.serviceWithZIO[Tracing] { tracing =>
// create a root span out of `zio`
zio @@ tracing.aspects.root("root span", SpanKind.INTERNAL, statusMapper)

}.provide(Tracing.live, ContextStorage.fiberRef, JaegerTracer.live)
}.provide(Tracing.live, ContextStorage.fiberRef, JaegerTracer.live(resourceName, instrumentationScopeName))
```

### Baggage
Expand Down Expand Up @@ -77,12 +80,13 @@ import zio._

upstream *> downstream

}.provide(Baggage.live, ContextStorage.fiberRef)
}.provide(Baggage.live, ContextStorage.fiberRef)
```

### Logging

To send Log signals, you need to provide an instance of `LoggerProvider` and add a `ZLogger` implementation that is able
To send Log signals, you need to provide an instance of `LoggerProvider`
(for this example we use `SeqLoggerProvider.live` from `opentelemetry-example` module) and add a `ZLogger` implementation that is able
to emit correlated log records to an OpenTelemetry Collector by providing `Logging.live` layer.

```scala
Expand All @@ -92,14 +96,22 @@ import zio.telemetry.opentelemetry.tracing.Tracing
import zio.telemetry.opentelemetry.example.JaegerTracer
import zio._

val instrumentationScopeName = "com.example.MyApp"
val resourceName = "example-app"

val app = ZIO.serviceWithZIO[Tracing] { tracing =>
ZIO.logDebug("not correlated message with 'my-app1' instrumentation scope")
.provideLayer(Logging.live("my-app1", LogLevel.Debug))

tracing.root("root span")(
ZIO.logInfo("correlated message with 'my-app2' instrumentation scope")
).provideLayer(Logging.live("my-app2"))
}.provide(Tracing.live, JaegerTracer.live, ContextStorage.fiberRef)
}.provide(
Tracing.live,
JaegerTracer.live(resourceName, instrumentationScopeName),
SeqLoggerProvider.live(resourceName),
ContextStorage.fiberRef
)
```

### Context Propagation
Expand All @@ -125,7 +137,7 @@ ZIO.serviceWithZIO[Tracing] { tracing =>

upstream *> downstream

}.provide(Tracing.live, ContextStorage.fiberRef, JaegerTracer.live)
}.provide(Tracing.live, ContextStorage.fiberRef, JaegerTracer.live(resourceName, instrumentationScopeName))
```

### Usage with OpenTelemetry automatic instrumentation
Expand All @@ -148,13 +160,15 @@ import zio.telemetry.opentelemetry.example.JaegerTracer
import io.opentelemetry.api.trace.{SpanKind, StatusCode}
import zio._

val instrumentationScopeName = "com.example.MyApp"

val statusMapper = StatusMapper.failureNoException(_ => StatusCode.UNSET)

val app = ZIO.serviceWithZIO[Tracing] { tracing =>
ZIO.logInfo("Hello") @@ tracing.aspects.root("root span", SpanKind.INTERNAL, statusMapper)
}.provide(
Tracing.live,
ContextStorage.openTelemetryContext,
ZLayer.fromZIO(ZIO.attempt(GlobalOpenTelemetry.getTracer("hello")))
ZLayer.fromZIO(ZIO.attempt(GlobalOpenTelemetry.getTracer(instrumentationScopeName)))
)
```

0 comments on commit fd53a65

Please sign in to comment.