Skip to content

Commit

Permalink
Quelle horreur
Browse files Browse the repository at this point in the history
  • Loading branch information
rossabaker committed Feb 3, 2023
1 parent e3ea876 commit 5a7a771
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
32 changes: 15 additions & 17 deletions examples/src/main/scala/TracingExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import cats.effect.MonadCancelThrow
import cats.effect.Resource
import cats.effect.std.Console
import cats.syntax.all._
import fs2.Stream
import io.opentelemetry.api.GlobalOpenTelemetry
import org.typelevel.otel4s.Attribute
import org.typelevel.otel4s.java.OtelJava
Expand Down Expand Up @@ -55,24 +54,23 @@ object TracingExample extends IOApp.Simple {
.evalMap(OtelJava.forSync[IO])
.evalMap(_.tracerProvider.tracer("Example").get)

def run: IO[Unit] = {
def run: IO[Unit] =
tracerResource.use { implicit tracer: Tracer[IO] =>
val resource: Resource[IO, Unit] =
Resource.make(IO.sleep(50.millis))(_ => IO.sleep(100.millis))

def stream(name: String) =
Stream
.resource(tracer.spanBuilder(name).start >> resource)
.flatMap(_ => Stream(1, 2, 3))
.evalMap(Work[IO].doWork)

tracer
.span("root")
.span("outer")
.surround(
(stream("uninterrupted") ++ stream(
"interrupted"
).interruptScope).compile.drain
IO.both(
tracer
.span("left")
.surround(
tracer.span("left-worker").use_.replicateA(10)
),
tracer
.span("right")
.surround(
tracer.span("right-worker").use_.replicateA(10)
)
)
)
}
}
}.void
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import cats.effect.LiftIO
import cats.effect.Ref
import cats.effect.Resource
import cats.effect.Sync
import cats.syntax.flatMap._
import cats.syntax.functor._
import cats.syntax.all._
import io.opentelemetry.api.trace.{Span => JSpan}
import io.opentelemetry.context.{Context => JContext}
import org.typelevel.otel4s.trace.SpanContext
Expand Down Expand Up @@ -84,12 +83,21 @@ private[java] object TraceScope {
def noopScope: Resource[F, Unit] =
createScope(Scope.Noop)

private def createScope(scope: Scope): Resource[F, Unit] =
Resource
.make(local.get.to[F].flatMap(_.getAndSet(scope)))(p =>
local.get.to[F].flatMap(_.set(p))
)
.void
private def createScope(scope: Scope): Resource[F, Unit] = {
val acquire =
local.get.to[F].product(Ref.of[F, Scope](scopeRoot)).flatTap {
case (_, nextRef) =>
local.set(nextRef).to[F]
}
def release(oldRef: (Ref[F, Scope], Any)): F[Unit] =
local.set(oldRef._1).to[F]
Resource.make(acquire)(release) >>
Resource
.make(local.get.to[F].flatMap(_.getAndSet(scope)))(p =>
local.get.to[F].flatMap(_.set(p))
)
.void
}

private def nextScope(scope: Scope, span: JSpan): Scope =
scope match {
Expand Down

0 comments on commit 5a7a771

Please sign in to comment.