Skip to content

Commit

Permalink
Remove ZLayer macros usages which can be problematic (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
guizmaii authored Jul 1, 2024
1 parent 1d9356c commit e17ec16
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ ThisBuild / developers :=
)

addCommandAlias("releaseSbtDatadog", "project sbt-datadog;clean;Test/compile;ci-release")
addCommandAlias("releaseZioOpentelemetryDatadogTracingProvider", "project zio-opentelemetry-datadog-tracing-provider;clean;+Test/compile;ci-release")
addCommandAlias(
"releaseZioOpentelemetryDatadogTracingProvider",
"project zio-opentelemetry-datadog-tracing-provider;clean;+Test/compile;ci-release",
)

val scala212 = "2.12.19"
val scala213 = "2.13.14"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import zio.config.{ConfigDescriptor, ReadError, ZConfig}

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

val config: ConfigDescriptor[TracingConfig] =
def config: ConfigDescriptor[TracingConfig] =
boolean("ZIO_OPENTELEMETRY_DATADOG_ENABLED")
.default(false)
.transformOrFailLeft(enabled => if (enabled) Right(OPENTELEMETRY) else Right(Disabled))(_ => false)
.transformOrFailLeft(enabled => if (enabled) Right(Opentelemetry) else Right(Disabled))(_ => false)

/**
* Provides a way to enable or disable the OpenTelemetry Tracing via the `ZIO_OPENTELEMETRY_DATADOG_ENABLED` environment variable.
*/
val fromSystemEnv: ZLayer[Any, ReadError[String], TracingConfig] = ZConfig.fromSystemEnv(config)
def fromSystemEnv: ZLayer[Any, ReadError[String], TracingConfig] = ZConfig.fromSystemEnv(config)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ package com.guizmaii.datadog.zio.tracing.provider
import io.opentelemetry.api.GlobalOpenTelemetry
import io.opentelemetry.api.trace.TracerProvider
import zio.telemetry.opentelemetry.Tracing
import zio.{ULayer, ZIO, ZLayer}
import zio.{TaskLayer, ZIO, ZLayer}

object TracingProvider {

/**
* Useful for tests
*/
val noOp: ULayer[Tracing] =
ZLayer
.make[Tracing](
Tracing.live,
ZLayer(ZIO.attempt(TracerProvider.noop().get("noOp"))),
)
.orDie
def noOp: TaskLayer[Tracing] = ZLayer(ZIO.attempt(TracerProvider.noop().get("noOp"))) >>> Tracing.live

/**
* Provides a zio-opentelemetry `Tracing` instance with the OpenTelemetry `Tracer` configured by the Datadog APM Agent
Expand All @@ -35,7 +29,7 @@ object TracingProvider {
_ <- ZIO.logInfo(s"OpenTelemetry Tracing config: $config - appName: $appName - appVersion: $appVersion")
} yield config match {
case TracingConfig.Disabled => noOp
case TracingConfig.OPENTELEMETRY =>
case TracingConfig.Opentelemetry =>
// Magically get the OpenTelemetry `Tracer` configured in the Datadog APM Agent
// that you configured in your application thanks the `sbt-datadog` plugin
val tracer =
Expand All @@ -46,10 +40,7 @@ object TracingProvider {
}
}

ZLayer.make[Tracing](
Tracing.propagating,
ZLayer(tracer),
)
ZLayer(tracer) >>> Tracing.propagating
}
}.flatten
}

0 comments on commit e17ec16

Please sign in to comment.