Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade zio to 2.0.0-RC3. need updated zio-http dep #509

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions opencensus/src/main/scala/zio/telemetry/opencensus/Live.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zio.telemetry.opencensus

import zio._
import zio.managed._

import io.opencensus.trace.AttributeValue
import io.opencensus.trace.BlankSpan
Expand Down Expand Up @@ -76,7 +77,7 @@ class Live(tracer: Tracer, root: FiberRef[Span]) extends Tracing.Service {
): 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 ()
Expand All @@ -87,27 +88,27 @@ class Live(tracer: Tracer, root: FiberRef[Span]) extends Tracing.Service {
kind: Span.Kind
): UManaged[Span] =
ZManaged.acquireReleaseWith(
UIO(
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(
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],
Expand All @@ -126,13 +127,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](
Expand All @@ -142,6 +143,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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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 zio.{ ExitCode, Task, URIO, ZEnv, ZIO, ZIOAppDefault, ZLayer }
import sttp.client3.asynchttpclient.zio.AsyncHttpClientZioBackend
import sttp.model.Uri
import zhttp.service.{ EventLoopGroup, Server, ServerChannelFactory }
Expand All @@ -31,7 +31,7 @@ object ProxyServer extends ZIOAppDefault {
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

/**
Expand All @@ -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))

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ 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 zio.managed._

import scala.jdk.CollectionConverters._

Expand Down Expand Up @@ -52,7 +52,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))
}

/**
Expand Down Expand Up @@ -157,7 +157,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()
Expand All @@ -172,7 +172,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()
Expand Down Expand Up @@ -339,7 +339,7 @@ object Tracing {
for {
nanoSeconds <- currentNanos.toManaged
span <- ZManaged.acquireReleaseWith(
UIO(
ZIO.succeed(
tracer
.spanBuilder(spanName)
.setNoParent()
Expand All @@ -354,7 +354,7 @@ object Tracing {
for {
nanoSeconds <- currentNanos.toManaged
span <- ZManaged.acquireReleaseWith(
UIO(
ZIO.succeed(
tracer
.spanBuilder(spanName)
.setParent(parent)
Expand All @@ -369,7 +369,7 @@ object Tracing {
for {
nanoSeconds <- currentNanos
span <-
UIO(
ZIO.succeed(
tracer
.spanBuilder(spanName)
.setParent(parent)
Expand All @@ -387,7 +387,7 @@ 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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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),
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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.{ Clock, UIO, ZIO, ZLayer }

import scala.collection.mutable
import scala.jdk.CollectionConverters._
Expand All @@ -21,7 +21,7 @@ object ProxyApp {
_ <- 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")
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.opentracing.propagation.Format
import io.opentracing.{ Span, SpanContext, Tracer }
import io.opentracing.noop.NoopTracerFactory
import zio._
import zio.managed._

import scala.jdk.CollectionConverters._

Expand Down Expand Up @@ -35,7 +36,7 @@ object OpenTracing {
def managed(tracer0: Tracer, rootOperation: String): URManaged[Clock, OpenTracing.Service] =
ZManaged.acquireReleaseWith(
for {
span <- UIO(tracer0.buildSpan(rootOperation).start())
span <- ZIO.succeed(tracer0.buildSpan(rootOperation).start())
ref <- FiberRef.make(span)
clock <- ZIO.service[Clock]
micros = clock.currentTime(TimeUnit.MICROSECONDS)
Expand All @@ -46,8 +47,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)
Expand All @@ -60,7 +61,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
Expand All @@ -74,7 +75,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)))
Expand All @@ -90,7 +91,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],
Expand All @@ -101,13 +102,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)))
Expand Down
Loading