From e17ec1673b6faff6e106bc4b9737793166a13d9f Mon Sep 17 00:00:00 2001 From: Jules Ivanic Date: Mon, 1 Jul 2024 22:31:15 +1000 Subject: [PATCH] Remove `ZLayer` macros usages which can be problematic (#136) --- build.sbt | 5 ++++- .../zio/tracing/provider/TracingConfig.scala | 8 ++++---- .../zio/tracing/provider/TracingProvider.scala | 17 ++++------------- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/build.sbt b/build.sbt index ea58dc1..ccc11fc 100644 --- a/build.sbt +++ b/build.sbt @@ -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" diff --git a/zio-opentelemetry-datadog-tracing-provider/src/main/scala/com/guizmaii/datadog/zio/tracing/provider/TracingConfig.scala b/zio-opentelemetry-datadog-tracing-provider/src/main/scala/com/guizmaii/datadog/zio/tracing/provider/TracingConfig.scala index dad87ab..aecfe56 100644 --- a/zio-opentelemetry-datadog-tracing-provider/src/main/scala/com/guizmaii/datadog/zio/tracing/provider/TracingConfig.scala +++ b/zio-opentelemetry-datadog-tracing-provider/src/main/scala/com/guizmaii/datadog/zio/tracing/provider/TracingConfig.scala @@ -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) } diff --git a/zio-opentelemetry-datadog-tracing-provider/src/main/scala/com/guizmaii/datadog/zio/tracing/provider/TracingProvider.scala b/zio-opentelemetry-datadog-tracing-provider/src/main/scala/com/guizmaii/datadog/zio/tracing/provider/TracingProvider.scala index ee75390..33e7ddf 100644 --- a/zio-opentelemetry-datadog-tracing-provider/src/main/scala/com/guizmaii/datadog/zio/tracing/provider/TracingProvider.scala +++ b/zio-opentelemetry-datadog-tracing-provider/src/main/scala/com/guizmaii/datadog/zio/tracing/provider/TracingProvider.scala @@ -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 @@ -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 = @@ -46,10 +40,7 @@ object TracingProvider { } } - ZLayer.make[Tracing]( - Tracing.propagating, - ZLayer(tracer), - ) + ZLayer(tracer) >>> Tracing.propagating } }.flatten }