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

Experimental IOLocalContextStorage #214

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.typelevel.otel4s.java

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

class IOLocalContextStorage[F[_]: LiftIO](
dispatcher: Dispatcher[F],
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]
rossabaker marked this conversation as resolved.
Show resolved Hide resolved
)

override def current(): Context = {
dispatcher.unsafeRunSync(currentOrRoot.to[F])
}

private def currentOrRoot = ioLocal.get.map {
case null => Context.root()
case ctx => ctx
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.typelevel.otel4s.java

import cats.effect.IO
import cats.effect.IOLocal
import cats.effect.Resource
import cats.effect.std.Dispatcher
import io.opentelemetry.context.Context
import io.opentelemetry.context.ContextKey
import io.opentelemetry.context.ContextStorage
import java.util.logging._

object Run extends cats.effect.IOApp.Simple {

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

def run =
Dispatcher.parallel[IO].use { dispatcher =>
rossabaker marked this conversation as resolved.
Show resolved Hide resolved
for {
_ <- IO {
val rootLog = Logger.getLogger("")
rootLog.setLevel(Level.FINE)
rootLog.getHandlers().head.setLevel(Level.FINE)
}
ioLocal <- IOLocal(null: Context)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to add the wrapper before we refer to Context, or else we get a FINE message that we're being ignored.

storage = new IOLocalContextStorage(dispatcher, ioLocal)
_ <- IO(ContextStorage.addWrapper(_ => storage))
_ <- ioLocal.set(null)
_ <- IO.println(ContextStorage.get().getClass)
ctx = Context.root()
_ <- Resource
.make(IO(ctx.`with`(key, "hello").makeCurrent()))(scope =>
IO(scope.close())
)
.surround {
for {
key <- IO(Context.current())
_ <- IO.println(key)
} yield ()
}
_ <- IO(Option(Context.current().get(key))).flatMap(v => IO.println(v))
} yield ()
}
}