From 4b52f036b16dec3aeae34378794cde76a66d2a25 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Wed, 6 Apr 2022 14:59:51 +0200 Subject: [PATCH] ZIO-2.0.0-RC4 (#516) * chore: upgrade zio to 2.0.0-RC3. need updated zio-http dep * chore: upgrade zio to 2.0.0-RC4, use Scope instead of ZManaged now --- .../scala/zio/telemetry/opencensus/Live.scala | 82 +++++++------- .../zio/telemetry/opencensus/Tracing.scala | 10 +- .../opentelemetry/example/BackendServer.scala | 27 ++--- .../opentelemetry/example/JaegerTracer.scala | 10 +- .../opentelemetry/example/ProxyServer.scala | 43 ++++---- .../opentelemetry/example/http/ProxyApp.scala | 4 +- .../opentelemetry/ContextPropagation.scala | 4 +- .../zio/telemetry/opentelemetry/Tracing.scala | 100 ++++++++++-------- .../opentelemetry/TracingSyntax.scala | 3 +- .../telemetry/opentelemetry/TracingTest.scala | 16 +-- .../opentracing/example/BackendServer.scala | 34 +++--- .../opentracing/example/JaegerTracer.scala | 4 +- .../opentracing/example/ProxyServer.scala | 36 ++++--- .../opentracing/example/http/BackendApp.scala | 4 +- .../opentracing/example/http/ProxyApp.scala | 8 +- .../telemetry/opentracing/OpenTracing.scala | 29 ++--- .../opentracing/OpenTracingTest.scala | 22 ++-- project/Dependencies.scala | 10 +- 18 files changed, 233 insertions(+), 213 deletions(-) diff --git a/opencensus/src/main/scala/zio/telemetry/opencensus/Live.scala b/opencensus/src/main/scala/zio/telemetry/opencensus/Live.scala index d7f15bfa..80887008 100644 --- a/opencensus/src/main/scala/zio/telemetry/opencensus/Live.scala +++ b/opencensus/src/main/scala/zio/telemetry/opencensus/Live.scala @@ -12,11 +12,11 @@ import io.opencensus.trace.Tracer object Live { val live: URLayer[Tracer, Tracing.Service] = - ZLayer.fromManaged(for { - tracer <- ZManaged.service[Tracer] - tracing = FiberRef.make[Span](BlankSpan.INSTANCE).map(new Live(tracer, _)) - managed <- ZManaged.acquireReleaseWith(tracing)(_.end) - } yield managed) + ZLayer.scoped(for { + tracer <- ZIO.service[Tracer] + tracing = FiberRef.make[Span](BlankSpan.INSTANCE).map(new Live(tracer, _)) + live <- ZIO.acquireRelease(tracing)(_.end) + } yield live) } class Live(tracer: Tracer, root: FiberRef[Span]) extends Tracing.Service { @@ -30,15 +30,13 @@ class Live(tracer: Tracer, root: FiberRef[Span]) extends Tracing.Service { toErrorStatus: ErrorMapper[E], attributes: Map[String, AttributeValue] )(effect: ZIO[R, E, A]): ZIO[R, E, A] = - for { - parent <- currentSpan_.get - res <- createSpan(parent, name, kind).use( - finalizeSpanUsingEffect( - putAttributes(attributes) *> effect, - toErrorStatus - ) - ) - } yield res + ZIO.scoped[R] { + for { + parent <- currentSpan_.get + span <- createSpan(parent, name, kind) + res <- finalizeSpanUsingEffect(putAttributes(attributes) *> effect, toErrorStatus)(span) + } yield res + } def root[R, E, A]( name: String, @@ -46,14 +44,14 @@ class Live(tracer: Tracer, root: FiberRef[Span]) extends Tracing.Service { toErrorStatus: ErrorMapper[E], attributes: Map[String, AttributeValue] )(effect: ZIO[R, E, A]): ZIO[R, E, A] = - for { - res <- createSpan(BlankSpan.INSTANCE, name, kind).use( - finalizeSpanUsingEffect( - putAttributes(attributes) *> effect, - toErrorStatus - ) - ) - } yield res + ZIO.scoped[R] { + createSpan(BlankSpan.INSTANCE, name, kind).flatMap(span => + finalizeSpanUsingEffect( + putAttributes(attributes) *> effect, + toErrorStatus + )(span) + ) + } def fromRemoteSpan[R, E, A]( remote: SpanContext, @@ -62,21 +60,21 @@ class Live(tracer: Tracer, root: FiberRef[Span]) extends Tracing.Service { toErrorStatus: ErrorMapper[E], attributes: Map[String, AttributeValue] )(effect: ZIO[R, E, A]): ZIO[R, E, A] = - for { - res <- createSpanFromRemote(remote, name, kind).use( - finalizeSpanUsingEffect( - putAttributes(attributes) *> effect, - toErrorStatus - ) - ) - } yield res + ZIO.scoped[R] { + createSpanFromRemote(remote, name, kind).flatMap(span => + finalizeSpanUsingEffect( + putAttributes(attributes) *> effect, + toErrorStatus + )(span) + ) + } def putAttributes( attributes: Map[String, AttributeValue] ): ZIO[Any, Nothing, Unit] = for { current <- currentSpan_.get - _ <- UIO(attributes.foreach { case ((k, v)) => + _ <- ZIO.succeed(attributes.foreach { case ((k, v)) => current.putAttribute(k, v) }) } yield () @@ -85,29 +83,29 @@ class Live(tracer: Tracer, root: FiberRef[Span]) extends Tracing.Service { parent: Span, name: String, kind: Span.Kind - ): UManaged[Span] = - ZManaged.acquireReleaseWith( - UIO( + ): URIO[Scope, Span] = + ZIO.acquireRelease( + ZIO.succeed( tracer .spanBuilderWithExplicitParent(name, parent) .setSpanKind(kind) .startSpan() ) - )(span => UIO(span.end)) + )(span => ZIO.succeed(span.end())) private def createSpanFromRemote( parent: SpanContext, name: String, kind: Span.Kind - ): UManaged[Span] = - ZManaged.acquireReleaseWith( - UIO( + ): URIO[Scope, Span] = + ZIO.acquireRelease( + ZIO.succeed( tracer .spanBuilderWithRemoteParent(name, parent) .setSpanKind(kind) .startSpan() ) - )(span => UIO(span.end)) + )(span => ZIO.succeed(span.end())) private def finalizeSpanUsingEffect[R, E, A]( effect: ZIO[R, E, A], @@ -126,13 +124,13 @@ class Live(tracer: Tracer, root: FiberRef[Span]) extends Tracing.Service { ): UIO[Unit] = for { current <- currentSpan - _ <- URIO(format.inject(current.getContext(), carrier, setter)) + _ <- ZIO.succeed(format.inject(current.getContext, carrier, setter)) } yield () private[opencensus] def end: UIO[Unit] = for { span <- currentSpan_.get - _ <- UIO(span.end()) + _ <- ZIO.succeed(span.end()) } yield () private def setErrorStatus[E]( @@ -142,6 +140,6 @@ class Live(tracer: Tracer, root: FiberRef[Span]) extends Tracing.Service { ): UIO[Unit] = { val errorStatus = cause.failureOption.flatMap(toErrorStatus.lift).getOrElse(Status.UNKNOWN) - UIO(span.setStatus(errorStatus)) + ZIO.succeed(span.setStatus(errorStatus)) } } diff --git a/opencensus/src/main/scala/zio/telemetry/opencensus/Tracing.scala b/opencensus/src/main/scala/zio/telemetry/opencensus/Tracing.scala index db273d47..985b3bf2 100644 --- a/opencensus/src/main/scala/zio/telemetry/opencensus/Tracing.scala +++ b/opencensus/src/main/scala/zio/telemetry/opencensus/Tracing.scala @@ -94,10 +94,12 @@ object Tracing { toErrorStatus: ErrorMapper[E] = defaultMapper[E], attributes: Map[String, AttributeValue] = Map() )(effect: ZIO[R, E, A]): ZIO[R with Service, E, A] = - Task(format.extract(carrier, getter)).foldZIO( - _ => root(name, kind, toErrorStatus)(effect), - remote => fromRemoteSpan(remote, name, kind, toErrorStatus, attributes)(effect) - ) + ZIO + .attempt(format.extract(carrier, getter)) + .foldZIO( + _ => root(name, kind, toErrorStatus)(effect), + remote => fromRemoteSpan(remote, name, kind, toErrorStatus, attributes)(effect) + ) def inject[C, R, E, A]( format: TextFormat, diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/BackendServer.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/BackendServer.scala index 4b0bc288..ef0a33c2 100644 --- a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/BackendServer.scala +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/BackendServer.scala @@ -2,33 +2,36 @@ package zio.telemetry.opentelemetry.example import sttp.model.Uri import zhttp.service.server.ServerChannelFactory -import zhttp.service.{ EventLoopGroup, Server, ServerChannelFactory } +import zhttp.service.{EventLoopGroup, Server, ServerChannelFactory} import zio.Console.printLine import zio.config.getConfig -import zio.config.magnolia.{ descriptor, Descriptor } +import zio.config.magnolia.{Descriptor, descriptor} import zio.config.typesafe.TypesafeConfig import zio.telemetry.opentelemetry.Tracing import zio.telemetry.opentelemetry.example.config.AppConfig import zio.telemetry.opentelemetry.example.http.BackendApp -import zio.{ ZEnv, ZIO, ZIOAppDefault, ZLayer } +import zio.{ZIO, ZIOAppDefault} object BackendServer extends ZIOAppDefault { implicit val sttpUriDescriptor: Descriptor[Uri] = Descriptor[String].transformOrFailLeft(Uri.parse)(_.toString) + type AppEnv = Tracing.Service with ServerChannelFactory with EventLoopGroup with AppConfig + val server = - getConfig[AppConfig].flatMap { conf => - val port = conf.backend.host.port.getOrElse(9000) - (Server.port(port) ++ Server.app(BackendApp.routes)).make.use(_ => - printLine(s"BackendServer started on port $port") *> ZIO.never - ) + ZIO.scoped[AppEnv] { + for { + conf <- getConfig[AppConfig] + port = conf.backend.host.port.getOrElse(9000) + server = Server.port(port) ++ Server.app(BackendApp.routes) + _ <- server.make + _ <- printLine(s"BackendServer started on port $port") *> ZIO.never + } yield () } val configLayer = TypesafeConfig.fromResourcePath(descriptor[AppConfig]) - - val appLayer: ZLayer[ZEnv with AppConfig, Throwable, Tracing.Service with ServerChannelFactory with EventLoopGroup] = - (JaegerTracer.live >>> Tracing.live) ++ ServerChannelFactory.auto ++ EventLoopGroup.auto(0) + val appLayer = (JaegerTracer.live >>> Tracing.live) ++ ServerChannelFactory.auto ++ EventLoopGroup.auto(0) override def run = - server.provideSomeLayer(configLayer >+> appLayer).exitCode + ZIO.provideLayer(configLayer >+> appLayer)(server.exitCode) } diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/JaegerTracer.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/JaegerTracer.scala index 3472c69c..7de8b962 100644 --- a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/JaegerTracer.scala +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/JaegerTracer.scala @@ -15,11 +15,11 @@ object JaegerTracer { .service[AppConfig] .flatMap(c => (for { - spanExporter <- Task(JaegerGrpcSpanExporter.builder().setEndpoint(c.get.tracer.host).build()) - spanProcessor <- UIO(SimpleSpanProcessor.create(spanExporter)) - tracerProvider <- UIO(SdkTracerProvider.builder().addSpanProcessor(spanProcessor).build()) - openTelemetry <- UIO(OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).build()) - tracer <- UIO(openTelemetry.getTracer("zio.telemetry.opentelemetry.example.JaegerTracer")) + spanExporter <- ZIO.attempt(JaegerGrpcSpanExporter.builder().setEndpoint(c.get.tracer.host).build()) + spanProcessor <- ZIO.succeed(SimpleSpanProcessor.create(spanExporter)) + tracerProvider <- ZIO.succeed(SdkTracerProvider.builder().addSpanProcessor(spanProcessor).build()) + openTelemetry <- ZIO.succeed(OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).build()) + tracer <- ZIO.succeed(openTelemetry.getTracer("zio.telemetry.opentelemetry.example.JaegerTracer")) } yield tracer).toLayer ) diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/ProxyServer.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/ProxyServer.scala index 94d0dd32..56eeae59 100644 --- a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/ProxyServer.scala +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/ProxyServer.scala @@ -3,45 +3,46 @@ package zio.telemetry.opentelemetry.example import sttp.capabilities.WebSockets import sttp.capabilities.zio.ZioStreams import sttp.client3.SttpBackend -import zio.config.getConfig -import zio.config.typesafe.TypesafeConfig -import zio.config.magnolia.{ descriptor, Descriptor } -import zio.telemetry.opentelemetry.Tracing -import zio.telemetry.opentelemetry.example.config.AppConfig -import zio.telemetry.opentelemetry.example.http.{ Client, ProxyApp } -import zio.{ ExitCode, Task, URIO, ZEnv, ZIO, ZIOAppDefault, ZLayer, ZManaged } import sttp.client3.asynchttpclient.zio.AsyncHttpClientZioBackend import sttp.model.Uri -import zhttp.service.{ EventLoopGroup, Server, ServerChannelFactory } import zhttp.service.server.ServerChannelFactory +import zhttp.service.{EventLoopGroup, Server, ServerChannelFactory} import zio.Console.printLine +import zio.config.getConfig +import zio.config.magnolia._ +import zio.config.typesafe.TypesafeConfig +import zio.telemetry.opentelemetry.Tracing +import zio.telemetry.opentelemetry.example.config.AppConfig +import zio.telemetry.opentelemetry.example.http.{Client, ProxyApp} +import zio.{Task, ZIO, ZIOAppDefault, ZLayer} object ProxyServer extends ZIOAppDefault { implicit val sttpUriDescriptor: Descriptor[Uri] = Descriptor[String].transformOrFailLeft(Uri.parse)(_.toString) + type AppEnv = AppConfig with Client.Service with Tracing.Service with ServerChannelFactory with EventLoopGroup + val server = - getConfig[AppConfig].flatMap { conf => - val port = conf.proxy.host.port.getOrElse(8080) - (Server.port(port) ++ Server.app(ProxyApp.routes)).make.use(_ => - printLine(s"ProxyServer started on port $port") *> ZIO.never - ) + ZIO.scoped[AppEnv] { + for { + conf <- getConfig[AppConfig] + port = conf.proxy.host.port.getOrElse(8080) + server = Server.port(port) ++ Server.app(ProxyApp.routes) + _ <- server.make + _ <- printLine(s"ProxyServer started on port $port") *> ZIO.never + } yield () } val configLayer = TypesafeConfig.fromResourcePath(descriptor[AppConfig]) val httpBackend: ZLayer[Any, Throwable, SttpBackend[Task, ZioStreams with WebSockets]] = - ZManaged.acquireReleaseWith(AsyncHttpClientZioBackend())(_.close().ignore).toLayer + ZLayer.fromAcquireRelease(AsyncHttpClientZioBackend())(_.close().ignore) val sttp: ZLayer[AppConfig, Throwable, Client.Service] = httpBackend >>> Client.live - val appEnv: ZLayer[ - ZEnv with AppConfig, - Throwable, - Client.Service with Tracing.Service with ServerChannelFactory with EventLoopGroup - ] = + val appEnv = (JaegerTracer.live >>> Tracing.live) ++ sttp ++ ServerChannelFactory.auto ++ EventLoopGroup.auto(0) - override def run: URIO[ZEnv, ExitCode] = - server.provideSomeLayer(configLayer >+> appEnv).exitCode + override def run = + ZIO.provideLayer(configLayer >+> appEnv)(server.exitCode) } diff --git a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/ProxyApp.scala b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/ProxyApp.scala index 53e1c756..f17c6b30 100644 --- a/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/ProxyApp.scala +++ b/opentelemetry-example/src/main/scala/zio/telemetry/opentelemetry/example/http/ProxyApp.scala @@ -3,7 +3,7 @@ package zio.telemetry.opentelemetry.example.http import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator import io.opentelemetry.api.trace.{ SpanKind, StatusCode } import io.opentelemetry.context.propagation.{ TextMapPropagator, TextMapSetter } -import zio.UIO +import zio.ZIO import zio.telemetry.opentelemetry.Tracing.root import zio.telemetry.opentelemetry.Tracing import zhttp.http.{ !!, ->, /, Http, HttpApp, Method, Response } @@ -22,7 +22,7 @@ object ProxyApp { case Method.GET -> !! / "statuses" => root("/statuses", SpanKind.SERVER, errorMapper) { for { - carrier <- UIO(mutable.Map[String, String]().empty) + carrier <- ZIO.succeed(mutable.Map[String, String]().empty) _ <- Tracing.setAttribute("http.method", "get") _ <- Tracing.addEvent("proxy-event") _ <- Tracing.inject(propagator, carrier, setter) diff --git a/opentelemetry/src/main/scala/zio/telemetry/opentelemetry/ContextPropagation.scala b/opentelemetry/src/main/scala/zio/telemetry/opentelemetry/ContextPropagation.scala index d9b0d0e9..41198f9e 100644 --- a/opentelemetry/src/main/scala/zio/telemetry/opentelemetry/ContextPropagation.scala +++ b/opentelemetry/src/main/scala/zio/telemetry/opentelemetry/ContextPropagation.scala @@ -15,7 +15,7 @@ private[opentelemetry] object ContextPropagation { getter: TextMapGetter[C] ): UIO[Context] = ZIO.uninterruptible { - UIO(propagator.extract(Context.root(), carrier, getter)) + ZIO.succeed(propagator.extract(Context.root(), carrier, getter)) } /** @@ -27,6 +27,6 @@ private[opentelemetry] object ContextPropagation { carrier: C, setter: TextMapSetter[C] ): URIO[Tracing.Service, Unit] = - UIO(propagator.inject(context, carrier, setter)) + ZIO.succeed(propagator.inject(context, carrier, setter)) } diff --git a/opentelemetry/src/main/scala/zio/telemetry/opentelemetry/Tracing.scala b/opentelemetry/src/main/scala/zio/telemetry/opentelemetry/Tracing.scala index 1008ba8a..d363cfe1 100644 --- a/opentelemetry/src/main/scala/zio/telemetry/opentelemetry/Tracing.scala +++ b/opentelemetry/src/main/scala/zio/telemetry/opentelemetry/Tracing.scala @@ -5,23 +5,32 @@ import io.opentelemetry.api.baggage.Baggage import java.util.concurrent.TimeUnit import scala.concurrent.ExecutionContext import io.opentelemetry.api.common.{ AttributeKey, Attributes } -import io.opentelemetry.api.trace.{ Span, SpanKind, StatusCode, Tracer } +import io.opentelemetry.api.trace.{ Span, SpanContext, SpanKind, StatusCode, Tracer } import io.opentelemetry.context.Context import io.opentelemetry.context.propagation.{ TextMapGetter, TextMapPropagator, TextMapSetter } import zio.telemetry.opentelemetry.ContextPropagation.{ extractContext, injectContext } import zio._ -import io.opentelemetry.api.trace.SpanContext import scala.jdk.CollectionConverters._ object Tracing { trait Service { private[opentelemetry] def currentNanos: UIO[Long] + private[opentelemetry] val currentContext: FiberRef[Context] - private[opentelemetry] def createRoot(spanName: String, spanKind: SpanKind): UManaged[Context] - private[opentelemetry] def createChildOf(parent: Context, spanName: String, spanKind: SpanKind): UManaged[Context] + + private[opentelemetry] def createRoot(spanName: String, spanKind: SpanKind): URIO[Scope, Context] + + private[opentelemetry] def createChildOf( + parent: Context, + spanName: String, + spanKind: SpanKind + ): URIO[Scope, Context] + private[opentelemetry] def createChildOfUnsafe(parent: Context, spanName: String, spanKind: SpanKind): UIO[Context] + private[opentelemetry] def getTracer: UIO[Tracer] + private[opentelemetry] def end: UIO[Any] } @@ -31,11 +40,11 @@ object Tracing { private def currentContext: URIO[Service, FiberRef[Context]] = ZIO.service[Service].map(_.currentContext) - private def createRoot(spanName: String, spanKind: SpanKind): URManaged[Service, Context] = - ZManaged.service[Service].flatMap(_.createRoot(spanName, spanKind)) + private def createRoot(spanName: String, spanKind: SpanKind): URIO[Scope with Service, Context] = + ZIO.serviceWithZIO[Service](_.createRoot(spanName, spanKind)) - private def createChildOf(parent: Context, spanName: String, spanKind: SpanKind): URManaged[Service, Context] = - ZManaged.service[Service].flatMap(_.createChildOf(parent, spanName, spanKind)) + private def createChildOf(parent: Context, spanName: String, spanKind: SpanKind): URIO[Scope with Service, Context] = + ZIO.serviceWithZIO[Service](_.createChildOf(parent, spanName, spanKind)) private def createChildOfUnsafe(parent: Context, spanName: String, spanKind: SpanKind): URIO[Service, Context] = ZIO.serviceWithZIO[Service](_.createChildOfUnsafe(parent, spanName, spanKind)) @@ -52,7 +61,7 @@ object Tracing { toErrorStatus: PartialFunction[E, StatusCode] ): UIO[Span] = { val errorStatus: StatusCode = cause.failureOption.flatMap(toErrorStatus.lift).getOrElse(StatusCode.UNSET) - UIO(span.setStatus(errorStatus, cause.prettyPrint)) + ZIO.succeed(span.setStatus(errorStatus, cause.prettyPrint)) } /** @@ -83,10 +92,13 @@ object Tracing { spanKind: SpanKind, toErrorStatus: PartialFunction[E, StatusCode] )(effect: ZIO[R, E, A]): ZIO[R with Service, E, A] = - for { - context <- extractContext(propagator, carrier, getter) - r <- createChildOf(context, spanName, spanKind).use(finalizeSpanUsingEffect(effect, _, toErrorStatus)) - } yield r + ZIO.scoped[R with Service] { + for { + context <- extractContext(propagator, carrier, getter) + ctx <- createChildOf(context, spanName, spanKind) + r <- finalizeSpanUsingEffect(effect, ctx, toErrorStatus) + } yield r + } /** * Extracts the span from carrier `C` and unsafely set its child span with name 'spanName' as the current span. You @@ -116,7 +128,9 @@ object Tracing { spanKind: SpanKind, toErrorStatus: PartialFunction[E, StatusCode] )(effect: ZIO[R, E, A]): ZIO[R with Service, E, A] = - createRoot(spanName, spanKind).use(finalizeSpanUsingEffect(effect, _, toErrorStatus)) + ZIO.scoped[R with Service] { + createRoot(spanName, spanKind).flatMap(finalizeSpanUsingEffect(effect, _, toErrorStatus)) + } /** * Sets the current span to be the child of the current span with name 'spanName' Ends the span when the effect @@ -127,10 +141,13 @@ object Tracing { spanKind: SpanKind, toErrorStatus: PartialFunction[E, StatusCode] )(effect: ZIO[R, E, A]): ZIO[R with Service, E, A] = - for { - old <- getCurrentContext - r <- createChildOf(old, spanName, spanKind).use(finalizeSpanUsingEffect(effect, _, toErrorStatus)) - } yield r + ZIO.scoped[R with Service] { + for { + old <- getCurrentContext + ctx <- createChildOf(old, spanName, spanKind) + r <- finalizeSpanUsingEffect(effect, ctx, toErrorStatus) + } yield r + } /** * Unsafely sets the current span to be the child of the current span with name 'spanName' You need to manually call @@ -157,7 +174,7 @@ object Tracing { def scopedEffect[R, A](effect: => A): ZIO[Service, Throwable, A] = for { currentContext <- getCurrentContext - eff <- Task.attempt { + eff <- ZIO.attempt { val scope = currentContext.makeCurrent() try effect finally scope.close() @@ -172,7 +189,7 @@ object Tracing { def scopedEffectTotal[R, A](effect: => A): ZIO[Service, Nothing, A] = for { currentContext <- getCurrentContext - eff <- Task.succeed { + eff <- ZIO.succeed { val scope = currentContext.makeCurrent() try effect finally scope.close() @@ -221,15 +238,10 @@ object Tracing { spanKind: SpanKind, toErrorStatus: PartialFunction[E, StatusCode] )(effect: ZIO[R, E, A]): ZIO[R with Service, E, A] = - for { - r <- createChildOf( - Context.root().`with`(span), - spanName, - spanKind - ).use( - finalizeSpanUsingEffect(effect, _, toErrorStatus) - ) - } yield r + ZIO.scoped[R with Service] { + createChildOf(Context.root().`with`(span), spanName, spanKind) + .flatMap(finalizeSpanUsingEffect(effect, _, toErrorStatus)) + } /** * Adds an event to the current span @@ -329,17 +341,17 @@ object Tracing { def getCurrentSpanContext: URIO[Service, SpanContext] = getCurrentSpan.map(_.getSpanContext()) - def managed(tracer: Tracer): URManaged[Clock, Service] = { + def scoped(tracer: Tracer): URIO[Scope, Service] = { class Live(defaultContext: FiberRef[Context], clock: Clock) extends Service { private def endSpan(span: Span): UIO[Unit] = currentNanos.map(span.end(_, TimeUnit.NANOSECONDS)) def currentNanos: UIO[Long] = clock.currentTime(TimeUnit.NANOSECONDS) - def createRoot(spanName: String, spanKind: SpanKind): UManaged[Context] = + def createRoot(spanName: String, spanKind: SpanKind): URIO[Scope, Context] = for { - nanoSeconds <- currentNanos.toManaged - span <- ZManaged.acquireReleaseWith( - UIO( + nanoSeconds <- currentNanos + span <- ZIO.acquireRelease( + ZIO.succeed( tracer .spanBuilder(spanName) .setNoParent() @@ -350,11 +362,11 @@ object Tracing { )(endSpan) } yield span.storeInContext(Context.root()) - def createChildOf(parent: Context, spanName: String, spanKind: SpanKind): UManaged[Context] = + def createChildOf(parent: Context, spanName: String, spanKind: SpanKind): URIO[Scope, Context] = for { - nanoSeconds <- currentNanos.toManaged - span <- ZManaged.acquireReleaseWith( - UIO( + nanoSeconds <- currentNanos + span <- ZIO.acquireRelease( + ZIO.succeed( tracer .spanBuilder(spanName) .setParent(parent) @@ -369,7 +381,7 @@ object Tracing { for { nanoSeconds <- currentNanos span <- - UIO( + ZIO.succeed( tracer .spanBuilder(spanName) .setParent(parent) @@ -387,19 +399,19 @@ object Tracing { } yield span.end(nanos, TimeUnit.NANOSECONDS) override private[opentelemetry] def getTracer: UIO[Tracer] = - UIO.succeed(tracer) + ZIO.succeed(tracer) val currentContext: FiberRef[Context] = defaultContext } - val tracing: URIO[Clock, Service] = + val tracing: URIO[Scope, Service] = for { - clock <- ZIO.service[Clock] + clock <- ZIO.clock defaultContext <- FiberRef.make[Context](Context.root()) } yield new Live(defaultContext, clock) - ZManaged.acquireReleaseWith(tracing)(_.end) + ZIO.acquireRelease(tracing)(_.end) } - def live: URLayer[Clock with Tracer, Service] = ZManaged.service[Tracer].flatMap(managed).toLayer + def live: URLayer[Tracer, Service] = ZLayer.scoped(ZIO.service[Tracer].flatMap(scoped)) } diff --git a/opentelemetry/src/main/scala/zio/telemetry/opentelemetry/TracingSyntax.scala b/opentelemetry/src/main/scala/zio/telemetry/opentelemetry/TracingSyntax.scala index fe6ad426..99c2444e 100644 --- a/opentelemetry/src/main/scala/zio/telemetry/opentelemetry/TracingSyntax.scala +++ b/opentelemetry/src/main/scala/zio/telemetry/opentelemetry/TracingSyntax.scala @@ -4,7 +4,6 @@ import io.opentelemetry.api.common.{ AttributeKey, Attributes } import io.opentelemetry.context.propagation.{ TextMapGetter, TextMapPropagator } import io.opentelemetry.api.trace.{ Span, SpanKind, StatusCode } import zio.ZIO -import zio.Clock object TracingSyntax { @@ -49,7 +48,7 @@ object TracingSyntax { ): ZIO[R with Tracing.Service, E, A] = Tracing.inSpan(span, spanName, spanKind, toErrorStatus)(effect) - def addEvent(name: String): ZIO[Tracing.Service with Clock with R, E, A] = + def addEvent(name: String): ZIO[Tracing.Service with R, E, A] = effect <* Tracing.addEvent(name) def addEventWithAttributes( diff --git a/opentelemetry/src/test/scala/zio/telemetry/opentelemetry/TracingTest.scala b/opentelemetry/src/test/scala/zio/telemetry/opentelemetry/TracingTest.scala index b11b62a8..2e03b39e 100644 --- a/opentelemetry/src/test/scala/zio/telemetry/opentelemetry/TracingTest.scala +++ b/opentelemetry/src/test/scala/zio/telemetry/opentelemetry/TracingTest.scala @@ -27,9 +27,9 @@ import zio.test.ZIOSpecDefault object TracingTest extends ZIOSpecDefault { val inMemoryTracer: UIO[(InMemorySpanExporter, Tracer)] = for { - spanExporter <- UIO(InMemorySpanExporter.create()) - spanProcessor <- UIO(SimpleSpanProcessor.create(spanExporter)) - tracerProvider <- UIO(SdkTracerProvider.builder().addSpanProcessor(spanProcessor).build()) + spanExporter <- ZIO.succeed(InMemorySpanExporter.create()) + spanProcessor <- ZIO.succeed(SimpleSpanProcessor.create(spanExporter)) + tracerProvider <- ZIO.succeed(SdkTracerProvider.builder().addSpanProcessor(spanProcessor).build()) tracer = tracerProvider.get("TracingTest") } yield (spanExporter, tracer) @@ -38,8 +38,8 @@ object TracingTest extends ZIOSpecDefault { ZEnvironment(inMemoryTracing).add(tracer) }) - val tracingMockLayer: URLayer[Clock, InMemorySpanExporter with Tracing.Service with Tracer] = - (inMemoryTracerLayer ++ Clock.any) >>> (Tracing.live ++ inMemoryTracerLayer) + val tracingMockLayer: ULayer[InMemorySpanExporter with Tracing.Service with Tracer] = + inMemoryTracerLayer >>> (Tracing.live ++ inMemoryTracerLayer) def getFinishedSpans = ZIO @@ -50,8 +50,7 @@ object TracingTest extends ZIOSpecDefault { suite("zio opentelemetry")( test("acquire/release the service") { for { - _ <- Tracing.live.build - .useDiscard(UIO.unit) + _ <- ZIO.scoped(Tracing.live.build) finishedSpans <- getFinishedSpans } yield assert(finishedSpans)(hasSize(equalTo(0))) }.provideCustomLayer(inMemoryTracerLayer), @@ -176,7 +175,8 @@ object TracingTest extends ZIOSpecDefault { }, test("inSpan") { for { - (_, tracer) <- inMemoryTracer + res <- inMemoryTracer + (_, tracer) = res externallyProvidedRootSpan = tracer.spanBuilder("external").startSpan() scope = externallyProvidedRootSpan.makeCurrent() _ <- UIO.unit.inSpan(externallyProvidedRootSpan, "zio-otel-child") diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/BackendServer.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/BackendServer.scala index 65664fc5..bbd6c9e6 100644 --- a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/BackendServer.scala +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/BackendServer.scala @@ -1,32 +1,34 @@ package zio.telemetry.opentracing.example import zhttp.service.server.ServerChannelFactory -import zhttp.service.{ EventLoopGroup, Server, ServerChannelFactory } +import zhttp.service.{EventLoopGroup, Server, ServerChannelFactory} import zio.Console.printLine import zio.config.getConfig -import zio.config.magnolia.DeriveConfigDescriptor.descriptor +import zio.config.magnolia._ import zio.config.typesafe.TypesafeConfig import zio.telemetry.opentracing.example.JaegerTracer.makeService import zio.telemetry.opentracing.example.config.AppConfig import zio.telemetry.opentracing.example.http.BackendApp -import zio.{ ExitCode, ZEnv, ZIO, ZIOAppDefault, ZLayer } +import zio.{ZIO, ZIOAppDefault} object BackendServer extends ZIOAppDefault { - private val configLayer = TypesafeConfig.fromResourcePath(descriptor[AppConfig]) + type AppEnv = AppConfig with EventLoopGroup with ServerChannelFactory - private val appEnv: ZLayer[ZEnv, Throwable, AppConfig with EventLoopGroup with ServerChannelFactory] = - configLayer ++ ServerChannelFactory.auto ++ EventLoopGroup.auto(0) + val server: ZIO[AppEnv, Throwable, Unit] = + ZIO.scoped[AppEnv] { + for { + conf <- getConfig[AppConfig] + tracingService = makeService(conf.tracer.host, "zio-backend") + server = Server.port(conf.backend.port) ++ Server.app(BackendApp.status(tracingService)) + _ <- server.make + _ <- printLine(s"BackendServer started at ${conf.backend.port}") *> ZIO.never + } yield () + } - override def run: ZIO[ZEnv, Nothing, ExitCode] = { - val exit = for { - conf <- getConfig[AppConfig] - tracingService = makeService(conf.tracer.host, "zio-backend") - exitCode <- (Server.port(conf.backend.port) ++ Server.app(BackendApp.status(tracingService))).make - .use(_ => printLine(s"BackendServer started at ${conf.backend.port}") *> ZIO.never) - .exitCode - } yield exitCode + val configLayer = TypesafeConfig.fromResourcePath(descriptor[AppConfig]) + val appLayer = ServerChannelFactory.auto ++ EventLoopGroup.auto(0) - exit.provideSomeLayer(appEnv) orElse ZIO.succeed(ExitCode.failure) - } + override def run = + ZIO.provideLayer(configLayer >+> appLayer)(server.exitCode) } diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/JaegerTracer.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/JaegerTracer.scala index c79df2e8..1c9f0042 100644 --- a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/JaegerTracer.scala +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/JaegerTracer.scala @@ -4,14 +4,14 @@ import io.jaegertracing.Configuration import io.jaegertracing.internal.samplers.ConstSampler import io.jaegertracing.zipkin.ZipkinV2Reporter import org.apache.http.client.utils.URIBuilder -import zio.{ Clock, ZLayer } +import zio.ZLayer import zio.telemetry.opentracing.OpenTracing import zipkin2.reporter.AsyncReporter import zipkin2.reporter.okhttp3.OkHttpSender object JaegerTracer { - def makeService(host: String, serviceName: String): ZLayer[Clock, Throwable, OpenTracing.Service] = { + def makeService(host: String, serviceName: String): ZLayer[Any, Throwable, OpenTracing.Service] = { val url = new URIBuilder().setScheme("http").setHost(host).setPath("/api/v2/spans").build.toString val senderBuilder = OkHttpSender.newBuilder.compressionEnabled(true).endpoint(url) diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/ProxyServer.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/ProxyServer.scala index 11fb4bb3..3b2a17f7 100644 --- a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/ProxyServer.scala +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/ProxyServer.scala @@ -2,35 +2,37 @@ package zio.telemetry.opentracing.example import sttp.model.Uri import zhttp.service.server.ServerChannelFactory -import zhttp.service.{ EventLoopGroup, Server, ServerChannelFactory } +import zhttp.service.{EventLoopGroup, Server, ServerChannelFactory} import zio.Console.printLine import zio.config.getConfig -import zio.config.magnolia.DeriveConfigDescriptor.descriptor +import zio.config.magnolia._ import zio.config.typesafe.TypesafeConfig import zio.telemetry.opentracing.example.JaegerTracer.makeService import zio.telemetry.opentracing.example.config.AppConfig import zio.telemetry.opentracing.example.http.ProxyApp -import zio.{ ExitCode, ZEnv, ZIO, ZIOAppDefault, ZLayer } +import zio.{ZIO, ZIOAppDefault} object ProxyServer extends ZIOAppDefault { private val configLayer = TypesafeConfig.fromResourcePath(descriptor[AppConfig]) - private val appEnv: ZLayer[ZEnv, Throwable, AppConfig with EventLoopGroup with ServerChannelFactory] = + type AppEnv = AppConfig with EventLoopGroup with ServerChannelFactory + + private val appEnv = configLayer ++ ServerChannelFactory.auto ++ EventLoopGroup.auto(0) - override def run: ZIO[ZEnv, Nothing, ExitCode] = { - val exit = - getConfig[AppConfig].flatMap { conf => - val service = makeService(conf.tracer.host, "zio-proxy") - for { - backendUrl <- ZIO.fromEither(Uri.safeApply(conf.backend.host, conf.backend.port)) - result <- (Server.port(conf.proxy.port) ++ Server.app(ProxyApp.statuses(backendUrl, service))).make - .use(_ => printLine(s"ProxyServer started on ${conf.proxy.port}") *> ZIO.never) - .exitCode - } yield result - } + val server: ZIO[AppEnv, Throwable, Unit] = + ZIO.scoped[AppEnv] { + for { + conf <- getConfig[AppConfig] + backendUrl <- ZIO.fromEither(Uri.safeApply(conf.backend.host, conf.backend.port)).mapError(new IllegalArgumentException(_)) + service = makeService(conf.tracer.host, "zio-proxy") + server = Server.port(conf.proxy.port) ++ Server.app(ProxyApp.statuses(backendUrl, service)) + _ <- server.make + _ <- printLine(s"ProxyServer started on ${conf.proxy.port}") *> ZIO.never + } yield () + } - exit.provideSomeLayer(appEnv) orElse ZIO.succeed(ExitCode.failure) - } + override def run = + ZIO.provideLayer(appEnv)(server.exitCode) } diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/BackendApp.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/BackendApp.scala index 5edd9e52..1788062c 100644 --- a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/BackendApp.scala +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/BackendApp.scala @@ -6,12 +6,12 @@ import zhttp.http.{ ->, /, Http, HttpApp, Method, Path, Response } import zio.json.EncoderOps import zio.telemetry.opentracing._ import zio.telemetry.opentracing.example.http.{ Status => ServiceStatus } -import zio.{ Clock, ZIO, ZLayer } +import zio.{ ZIO, ZLayer } import scala.jdk.CollectionConverters._ object BackendApp { - def status(service: ZLayer[Clock, Throwable, OpenTracing.Service]): HttpApp[Clock, Throwable] = + def status(service: ZLayer[Any, Throwable, OpenTracing.Service]): HttpApp[Any, Throwable] = Http.collectZIO { case request @ Method.GET -> Path.End / "status" => val headers = request.headers.toList.toMap ZIO.unit diff --git a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/ProxyApp.scala b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/ProxyApp.scala index d3a02eff..de3aa52a 100644 --- a/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/ProxyApp.scala +++ b/opentracing-example/src/main/scala/zio/telemetry/opentracing/example/http/ProxyApp.scala @@ -8,20 +8,20 @@ import sttp.model.Uri import zhttp.http.{ ->, /, Http, HttpApp, Method, Path, Response } import zio.json.EncoderOps import zio.telemetry.opentracing.OpenTracing -import zio.{ Clock, UIO, ZLayer } +import zio.{ UIO, ZIO, ZLayer } import scala.collection.mutable import scala.jdk.CollectionConverters._ object ProxyApp { - def statuses(backendUri: Uri, service: ZLayer[Clock, Throwable, OpenTracing.Service]): HttpApp[Clock, Throwable] = + def statuses(backendUri: Uri, service: ZLayer[Any, Throwable, OpenTracing.Service]): HttpApp[Any, Throwable] = Http.collectZIO { case Method.GET -> Path.End / "statuses" => val zio = for { _ <- OpenTracing.tag(Tags.SPAN_KIND.getKey, Tags.SPAN_KIND_CLIENT) _ <- OpenTracing.tag(Tags.HTTP_METHOD.getKey, GET.method) _ <- OpenTracing.setBaggageItem("proxy-baggage-item-key", "proxy-baggage-item-value") - buffer <- UIO.succeed(new TextMapAdapter(mutable.Map.empty[String, String].asJava)) + buffer <- ZIO.succeed(new TextMapAdapter(mutable.Map.empty[String, String].asJava)) _ <- OpenTracing.inject(HttpHeadersFormat, buffer) headers <- extractHeaders(buffer) up = Status.up("proxy") @@ -41,7 +41,7 @@ object ProxyApp { private def extractHeaders(adapter: TextMapAdapter): UIO[Map[String, String]] = { val m = mutable.Map.empty[String, String] - UIO(adapter.forEach { entry => + ZIO.succeed(adapter.forEach { entry => m.put(entry.getKey, entry.getValue) () }).as(m.toMap) diff --git a/opentracing/src/main/scala/zio/telemetry/opentracing/OpenTracing.scala b/opentracing/src/main/scala/zio/telemetry/opentracing/OpenTracing.scala index 55e0e4b4..73c16e0a 100644 --- a/opentracing/src/main/scala/zio/telemetry/opentracing/OpenTracing.scala +++ b/opentracing/src/main/scala/zio/telemetry/opentracing/OpenTracing.scala @@ -27,17 +27,17 @@ object OpenTracing { def tag[R, E, A](zio: ZIO[R, E, A], key: String, value: Boolean): ZIO[R, E, A] } - lazy val noop: URLayer[Clock, OpenTracing.Service] = live(NoopTracerFactory.create()) + lazy val noop: ULayer[OpenTracing.Service] = live(NoopTracerFactory.create()) - def live(tracer: Tracer, rootOperation: String = "ROOT"): URLayer[Clock, OpenTracing.Service] = - ZLayer.fromManaged(managed(tracer, rootOperation)) + def live(tracer: Tracer, rootOperation: String = "ROOT"): ULayer[OpenTracing.Service] = + ZLayer.scoped(scoped(tracer, rootOperation)) - def managed(tracer0: Tracer, rootOperation: String): URManaged[Clock, OpenTracing.Service] = - ZManaged.acquireReleaseWith( + def scoped(tracer0: Tracer, rootOperation: String): URIO[Scope, OpenTracing.Service] = + ZIO.acquireRelease( for { - span <- UIO(tracer0.buildSpan(rootOperation).start()) + span <- ZIO.succeed(tracer0.buildSpan(rootOperation).start()) ref <- FiberRef.make(span) - clock <- ZIO.service[Clock] + clock <- ZIO.clock micros = clock.currentTime(TimeUnit.MICROSECONDS) } yield new OpenTracing.Service { self => val tracer: Tracer = tracer0 @@ -46,8 +46,8 @@ object OpenTracing { def error(span: Span, cause: Cause[_], tagError: Boolean, logError: Boolean): UIO[Unit] = for { - _ <- UIO(span.setTag("error", true)).when(tagError) - _ <- UIO(span.log(Map("error.object" -> cause, "stack" -> cause.prettyPrint).asJava)).when(logError) + _ <- ZIO.succeed(span.setTag("error", true)).when(tagError) + _ <- ZIO.succeed(span.log(Map("error.object" -> cause, "stack" -> cause.prettyPrint).asJava)).when(logError) } yield () def finish(span: Span): UIO[Unit] = micros.map(span.finish) @@ -60,7 +60,7 @@ object OpenTracing { def root[R, E, A](zio: ZIO[R, E, A], operation: String, tagError: Boolean, logError: Boolean): ZIO[R, E, A] = for { - root <- UIO(tracer.buildSpan(operation).start()) + root <- ZIO.succeed(tracer.buildSpan(operation).start()) current <- currentSpan.get _ <- currentSpan.set(root) res <- zio @@ -74,7 +74,7 @@ object OpenTracing { def span[R, E, A](zio: ZIO[R, E, A], operation: String, tagError: Boolean, logError: Boolean): ZIO[R, E, A] = for { current <- currentSpan.get - child <- UIO(tracer.buildSpan(operation).asChildOf(current).start()) + child <- ZIO.succeed(tracer.buildSpan(operation).asChildOf(current).start()) _ <- currentSpan.set(child) res <- zio .catchAllCause(c => error(child, c, tagError, logError) *> IO.done(Exit.Failure(c))) @@ -90,7 +90,7 @@ object OpenTracing { def tag[R, E, A](zio: ZIO[R, E, A], key: String, value: Boolean): ZIO[R, E, A] = zio <* currentSpan.get.map(_.setTag(key, value)) } - )(_.currentSpan.get.flatMap(span => UIO(span.finish()))) + )(_.currentSpan.get.flatMap(span => ZIO.succeed(span.finish()))) def spanFrom[R, R1 <: R with OpenTracing.Service, E, Span, C <: AnyRef]( format: Format[C], @@ -101,13 +101,14 @@ object OpenTracing { logError: Boolean = true ): ZIO[R1, E, Span] = ZIO.service[OpenTracing.Service].flatMap { service => - Task(service.tracer.extract(format, carrier)) + ZIO + .attempt(service.tracer.extract(format, carrier)) .foldZIO( _ => zio, spanCtx => for { current <- service.currentSpan.get - span <- UIO(service.tracer.buildSpan(operation).asChildOf(spanCtx).start()) + span <- ZIO.succeed(service.tracer.buildSpan(operation).asChildOf(spanCtx).start()) _ <- service.currentSpan.set(span) res <- zio .catchAllCause(c => service.error(span, c, tagError, logError) *> IO.done(Exit.Failure(c))) diff --git a/opentracing/src/test/scala/zio/telemetry/opentracing/OpenTracingTest.scala b/opentracing/src/test/scala/zio/telemetry/opentracing/OpenTracingTest.scala index 5f264992..bf360a2b 100644 --- a/opentracing/src/test/scala/zio/telemetry/opentracing/OpenTracingTest.scala +++ b/opentracing/src/test/scala/zio/telemetry/opentracing/OpenTracingTest.scala @@ -3,36 +3,36 @@ package zio.telemetry.opentracing import io.opentracing.mock.{ MockSpan, MockTracer } import io.opentracing.propagation.{ BinaryAdapters, Format, TextMapAdapter } import zio._ -import zio.Clock - import zio.test._ import zio.test.Assertion._ +import zio.test.ZIOSpecDefault import scala.collection.mutable import scala.jdk.CollectionConverters._ import java.nio.ByteBuffer -import zio.test.ZIOSpecDefault object OpenTracingTest extends ZIOSpecDefault { val mockTracer: Layer[Nothing, MockTracer] = - ZLayer.fromZIO(UIO(new MockTracer)) + ZLayer.fromZIO(ZIO.succeed(new MockTracer)) - val testService: URLayer[MockTracer with Clock, OpenTracing.Service] = - ZManaged.service[MockTracer].flatMap(OpenTracing.managed(_, "ROOT")).toLayer + val testService: URLayer[MockTracer, OpenTracing.Service] = + ZLayer.scoped(ZIO.service[MockTracer].flatMap(OpenTracing.scoped(_, "ROOT"))) - val customLayer = mockTracer ++ ((mockTracer ++ Clock.any) >>> testService) + val customLayer = mockTracer ++ (mockTracer >>> testService) def spec = suite("zio opentracing")( test("managedService") { val tracer = new MockTracer - OpenTracing - .live(tracer, "ROOT") - .build - .useDiscard(UIO.unit) + ZIO + .scoped( + OpenTracing + .live(tracer, "ROOT") + .build + ) .as( assert(tracer.finishedSpans.asScala)(hasSize(equalTo(1))) && assert(tracer.finishedSpans().get(0))( hasField[MockSpan, String]( diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 57b350b9..d4cfc821 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -3,15 +3,15 @@ import sbt._ object Dependencies { object Versions { val jaeger = "1.6.0" - val sttp3 = "3.4.1" + val sttp3 = "3.5.1" val opentracing = "0.33.0" val opentelemetry = "1.11.0" val opencensus = "0.31.0" val zipkin = "2.16.3" - val zio = "2.0.0-RC2" - val zioHttp = "2.0.0-RC3" - val zioJson = "0.3.0-RC3" - val zioConfig = "3.0.0-RC2" + val zio = "2.0.0-RC4" + val zioHttp = "2.0.0-RC6" + val zioJson = "0.3.0-RC6" + val zioConfig = "3.0.0-RC7" } lazy val zio = Seq(