-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac4175c
commit 682abb6
Showing
4 changed files
with
52 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () | ||
} |
34 changes: 11 additions & 23 deletions
34
java/common/src/main/scala/org/typelevel/otel4s/java/IOLocalContextStorage.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
43 changes: 0 additions & 43 deletions
43
java/common/src/test/scala/org/typelevel/otel4s/java/ContextStorageSuite.scala
This file was deleted.
Oops, something went wrong.