Skip to content

Commit

Permalink
Update zio-opentelemetry to 3.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
guizmaii committed Nov 17, 2024
1 parent 449b646 commit 9549221
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 72 deletions.
14 changes: 7 additions & 7 deletions ZIO_OTLP_DATADOG_PROVIDER_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ libraryDependencies += "com.guizmaii" %% "zio-opentelemetry-datadog-tracing-prov
### Documentation

This library provides you two things:
1. The [TracingProvider](zio-opentelemetry-datadog-tracing-provider/src/main/scala/com/guizmaii/datadog/zio/tracing/provider/TracingProvider.scala) object to help you easily get a zio-opentelemetry `Tracing` instance configured to send traces to Datadog via the Datadog APM you configured with the sbt plugin.
2. The [TracingConfig](zio-opentelemetry-datadog-tracing-provider/src/main/scala/com/guizmaii/datadog/zio/tracing/provider/TracingConfig.scala) object to help you easily enable or disable zio-opentelemetry `Tracing` via an environment variable, `ZIO_OPENTELEMETRY_DATADOG_ENABLED`.
1. The [OpenTelemetryProvider](zio-opentelemetry-datadog-tracing-provider/src/main/scala/com/guizmaii/datadog/zio/tracing/provider/OpenTelemetryProvider.scala) object to help you easily get a zio-opentelemetry `Tracing` instance configured to send traces to Datadog via the Datadog APM you configured with the sbt plugin.
2. The [OpenTelemetryConfig](zio-opentelemetry-datadog-tracing-provider/src/main/scala/com/guizmaii/datadog/zio/tracing/provider/OpenTelemetryConfig.scala) object to help you easily enable or disable opentelemetry via an environment variable: `ZIO_OPENTELEMETRY_DATADOG_ENABLED`.

### TracingProvider
### OpenTelemetryProvider

#### `TracingProvider.live` layer
#### `OpenTelemetryProvider.autoInstrumentation` layer

This layer is the one you'll want to use in your app.
It'll provide you a `Tracing` instance that'll send traces to Datadog via the Datadog APM you configured with the sbt plugin.
This layer is the one you'll want to use in your app in production.
It'll provide you a `Tracing` instance that'll automatically send the traces to Datadog via the Datadog APM you configured with the sbt plugin.

You have an example of how to use it in [examples/my-traced-zio-project-example/src/main/scala/org/example/datadog/otlp/Main](examples/my-traced-zio-project-example/src/main/scala/org/example/datadog/otlp/Main.scala).

#### `TracingProvider.noOp` layer
#### `OpenTelemetryProvider.noOp` layer

This layer is useful when you want to disable tracing in your app.
It's for example useful in your tests.
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ lazy val `zio-opentelemetry-datadog-tracing-provider` =
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % "2.1.12" % "provided",
"dev.zio" %% "zio-config" % "4.0.2" % "provided",
"dev.zio" %% "zio-opentelemetry" % "2.0.3",
"dev.zio" %% "zio-opentelemetry" % "3.0.1",
"io.opentelemetry" % "opentelemetry-api" % "1.44.1",
),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.example.datadog.otlp

import com.guizmaii.datadog.zio.tracing.provider.{TracingConfig, TracingProvider}
import com.guizmaii.datadog.zio.tracing.provider.{OpenTelemetryConfig, OpenTelemetryProvider}
import zio._

object Main extends ZIOAppDefault {
Expand All @@ -12,7 +12,10 @@ object Main extends ZIOAppDefault {
} yield ()
).provide(
MyService.live,
TracingProvider.live(appName = "my-app", appVersion = Some("1.0.0")),
TracingConfig.fromSystemEnv,
OpenTelemetryProvider.autoInstrumentation(
instrumentationScopeName = "my-app",
instrumentationVersion = Some("1.0.0"),
),
OpenTelemetryConfig.fromSystemEnv,
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.example.datadog.otlp

import zio.telemetry.opentelemetry.Tracing
import zio.{Task, URLayer, ZEnvironment, ZIO, ZLayer}
import zio.telemetry.opentelemetry.tracing.Tracing
import zio.{Task, URLayer, ZIO, ZLayer}

trait MyService {
def doSomething: Task[Int]
Expand All @@ -18,20 +18,18 @@ final class MyServiceLive extends MyService {
* 1. if someone add a method in your service, he/she will be forced to implement it here too so that everything keeps being traced
* 2. it separates the tracing logic from the business logic
*/
final class MyServiceTraced(tracing: ZEnvironment[Tracing])(delegator: MyService) extends MyService {
import zio.telemetry.opentelemetry.TracingSyntax._
final class MyServiceTraced(tracing: Tracing)(delegator: MyService) extends MyService {
import tracing.aspects._

override def doSomething: Task[Int] =
delegator.doSomething
.span("MyService::doSomething")
.provideEnvironment(tracing)
delegator.doSomething @@ span("MyService::doSomething")
}

object MyService {
val live: URLayer[Tracing, MyService] =
ZLayer {
for {
tracing <- ZIO.environment[Tracing]
tracing <- ZIO.service[Tracing]
live = new MyServiceLive
} yield new MyServiceTraced(tracing)(live)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.guizmaii.datadog.zio.tracing.provider

import zio.{ZLayer, Config, ConfigProvider}
import zio.Config._
import zio.{Config, ConfigProvider, ZLayer}

sealed trait TracingConfig extends Product with Serializable
object TracingConfig {
case object Opentelemetry extends TracingConfig
case object Disabled extends TracingConfig
sealed trait OpenTelemetryConfig extends Product with Serializable
object OpenTelemetryConfig {
case object Opentelemetry extends OpenTelemetryConfig
case object Disabled extends OpenTelemetryConfig

val config: Config[TracingConfig] =
val config: Config[OpenTelemetryConfig] =
boolean("ZIO_OPENTELEMETRY_DATADOG_ENABLED")
.withDefault(false)
.map(enabled => if (enabled) Opentelemetry else Disabled)

/**
* Provides a way to enable or disable the OpenTelemetry Tracing via the `ZIO_OPENTELEMETRY_DATADOG_ENABLED` environment variable.
*/
def fromSystemEnv: ZLayer[Any, Config.Error, TracingConfig] =
def fromSystemEnv: ZLayer[Any, Config.Error, OpenTelemetryConfig] =
ZLayer.fromZIO(ConfigProvider.envProvider.load(config))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.guizmaii.datadog.zio.tracing.provider

import zio.telemetry.opentelemetry.OpenTelemetry
import zio.telemetry.opentelemetry.tracing.Tracing
import zio.{ULayer, ZIO, ZLayer}

object OpenTelemetryProvider {

/**
* Useful for tests
*/
def noOp: ULayer[Tracing] =
ZLayer.make[Tracing](
OpenTelemetry.noop,
OpenTelemetry.contextZIO,
OpenTelemetry.tracing("no-op"),
)

/**
* Provides a zio-opentelemetry `Tracing` instance using the OpenTelemetry `Tracer` configured by the Datadog APM Agent
*
* Don't forget to enable the OpenTelemetry tracing in the Datadog APM Agent.
* You can enable it by setting the `DD_TRACE_OTEL_ENABLED` environment variable to `true`
* or by setting the `dd.trace.otel.enabled` property to `true`.
*
* See related Datadog documentation:
* - https://docs.datadoghq.com/tracing/trace_collection/custom_instrumentation/java/otel/
*
* See zio-opentelemetry documentation:
* - https://zio.dev/zio-telemetry/opentelemetry/#usage-with-opentelemetry-automatic-instrumentation
*
* Exposes the same parameters as `OpenTelemetry.tracing`
*/
def autoInstrumentation(
instrumentationScopeName: String,
instrumentationVersion: Option[String] = None,
schemaUrl: Option[String] = None,
logAnnotated: Boolean = false,
): ZLayer[OpenTelemetryConfig, Throwable, Tracing] =
ZLayer.fromZIO {
for {
config <- ZIO.service[OpenTelemetryConfig]
_ <- ZIO.logInfo(s"OpenTelemetry Tracing config: $config")
} yield config match {
case OpenTelemetryConfig.Disabled => noOp
case OpenTelemetryConfig.Opentelemetry =>
ZLayer.make[Tracing](
OpenTelemetry.global,
OpenTelemetry.contextJVM,
OpenTelemetry.tracing(
instrumentationScopeName = instrumentationScopeName,
instrumentationVersion = instrumentationVersion,
schemaUrl = schemaUrl,
logAnnotated = logAnnotated,
),
)
}
}.flatten
}

This file was deleted.

0 comments on commit 9549221

Please sign in to comment.