Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rossabaker committed May 18, 2023
1 parent ac4175c commit 682abb6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 68 deletions.
5 changes: 3 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ThisBuild / crossScalaVersions := Seq(Scala213, "3.2.2")
ThisBuild / scalaVersion := Scala213 // the default Scala

val CatsVersion = "2.9.0"
val CatsEffectVersion = "3.5.0"
val CatsEffectVersion = "3.5.0-30-0a69caf"
val CatsMtlVersion = "1.3.1"
val FS2Version = "3.6.1"
val MUnitVersion = "1.0.0-M7"
Expand Down Expand Up @@ -154,7 +154,7 @@ lazy val `java-common` = project
.settings(
name := "otel4s-java-common",
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-effect-kernel" % CatsEffectVersion,
"org.typelevel" %%% "cats-effect" % CatsEffectVersion,
"org.typelevel" %%% "cats-mtl" % CatsMtlVersion,
"io.opentelemetry" % "opentelemetry-sdk" % OpenTelemetryVersion,
"org.scalameta" %%% "munit" % MUnitVersion % Test
Expand Down Expand Up @@ -241,6 +241,7 @@ lazy val examples = project
),
run / fork := true,
javaOptions += "-Dotel.java.global-autoconfigure.enabled=true",
javaOptions += "-Dcats.effect.tracing.dumpLocals",
envVars ++= Map(
"OTEL_PROPAGATORS" -> "b3multi",
"OTEL_SERVICE_NAME" -> "Trace Example"
Expand Down
38 changes: 38 additions & 0 deletions examples/src/main/scala/ContextStorageExample.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import cats.effect.IO
import cats.effect.IOApp
import cats.effect.IOLocal
import cats.effect.Resource
import cats.effect.unsafe.IOLocals
import io.opentelemetry.context.Context
import io.opentelemetry.context.ContextKey
import io.opentelemetry.context.ContextStorage
import org.typelevel.otel4s.java.IOLocalContextStorage
import java.util.logging._

object ContextStorageExample extends IOApp.Simple {

val key = ContextKey.named[String]("test")

val printKey =
IO(Option(Context.current().get(key))).flatMap(v => IO.println(v))

def run =
for {
_ <- IO {
val rootLog = Logger.getLogger("")
rootLog.setLevel(Level.FINE)
rootLog.getHandlers().head.setLevel(Level.FINE)
}
ioLocal <- IOLocal(null: Context)
storage = new IOLocalContextStorage(ioLocal)
_ <- IO(ContextStorage.addWrapper(_ => storage))
ctx = Context.root()
_ = IOLocals.set(ioLocal, Context.root())
_ <- Resource
.make(IO(ctx.`with`(key, "hello").makeCurrent()))(scope =>
IO(scope.close())
)
.surround(printKey)
_ <- printKey
} yield ()
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
package org.typelevel.otel4s.java

import cats.effect.IOLocal
import cats.effect.LiftIO
import cats.effect.std.Dispatcher
import cats.effect.unsafe.IOLocals
import io.opentelemetry.context.Context
import io.opentelemetry.context.ContextStorage
import io.opentelemetry.context.Scope

class IOLocalContextStorage[F[_]: LiftIO](
dispatcher: Dispatcher[F],
class IOLocalContextStorage(
ioLocal: IOLocal[Context]
) extends ContextStorage {

override def attach(toAttach: Context): Scope =
dispatcher.unsafeRunSync(
currentOrRoot
.flatMap { old =>
ioLocal
.set(toAttach)
.as(new Scope {
def close() = dispatcher.unsafeRunSync(ioLocal.set(old).to[F])
})
}
.to[F]
)

override def current(): Context = {
dispatcher.unsafeRunSync(currentOrRoot.to[F])
override def attach(toAttach: Context): Scope = {
val previous = current()
IOLocals.set(ioLocal, toAttach)
new Scope {
def close() = IOLocals.set(ioLocal, previous)
}
}

private def currentOrRoot = ioLocal.get.map {
case null => Context.root()
case ctx => ctx
}
override def current(): Context =
IOLocals.get(ioLocal)

}

This file was deleted.

0 comments on commit 682abb6

Please sign in to comment.