Skip to content

Commit

Permalink
Use cats-effect 3.6.0-RC1
Browse files Browse the repository at this point in the history
  • Loading branch information
iRevive committed Dec 28, 2024
1 parent b9b8fd9 commit ea9f53b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
13 changes: 6 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ ThisBuild / mergifyPrRules ++= Seq(
)

val CatsVersion = "2.11.0"
val CatsEffectVersion = "3.6-28f8f29"
val CatsEffectVersion = "3.6.0-RC1"
val CatsMtlVersion = "1.4.0"
val FS2Version = "3.11.0"
val MUnitVersion = "1.0.0"
Expand Down Expand Up @@ -713,8 +713,7 @@ lazy val `oteljava-context-storage` = project
"org.typelevel" %%% "cats-effect-testkit" % CatsEffectVersion % Test,
),
Test / javaOptions ++= Seq(
"-Dotel.java.global-autoconfigure.enabled=true",
"-Dcats.effect.ioLocalPropagation=true",
"-Dcats.effect.trackFiberContext=true",
),
Test / fork := true,
)
Expand All @@ -728,7 +727,6 @@ lazy val oteljava = project
`oteljava-metrics-testkit` % Test,
`oteljava-trace` % "compile->compile;test->test",
`oteljava-trace-testkit` % Test,
`oteljava-context-storage`
)
.settings(
name := "otel4s-oteljava",
Expand Down Expand Up @@ -853,7 +851,7 @@ lazy val benchmarks = project
lazy val examples = project
.enablePlugins(NoPublishPlugin, JavaAgent)
.in(file("examples"))
.dependsOn(core.jvm, oteljava, sdk.jvm, `sdk-exporter`.jvm, `sdk-exporter-prometheus`.jvm)
.dependsOn(core.jvm, oteljava, `oteljava-context-storage`, sdk.jvm, `sdk-exporter`.jvm, `sdk-exporter-prometheus`.jvm)
.settings(
name := "otel4s-examples",
libraryDependencies ++= Seq(
Expand All @@ -865,10 +863,10 @@ lazy val examples = project
"io.opentelemetry" % "opentelemetry-extension-trace-propagators" % OpenTelemetryVersion % Runtime,
"io.opentelemetry.instrumentation" % "opentelemetry-instrumentation-annotations" % OpenTelemetryInstrumentationVersion
),
javaAgents += "io.opentelemetry.javaagent" % "opentelemetry-javaagent" % OpenTelemetryInstrumentationVersion % Runtime,
//javaAgents += "io.opentelemetry.javaagent" % "opentelemetry-javaagent" % OpenTelemetryInstrumentationVersion % Runtime,
run / fork := true,
javaOptions += "-Dotel.java.global-autoconfigure.enabled=true",
javaOptions += "-Dcats.effect.ioLocalPropagation=true",
javaOptions += "-Dcats.effect.trackFiberContext=true",
envVars ++= Map(
"OTEL_PROPAGATORS" -> "b3multi",
"OTEL_SERVICE_NAME" -> "Trace Example"
Expand All @@ -881,6 +879,7 @@ lazy val docs = project
.enablePlugins(TypelevelSitePlugin)
.dependsOn(
oteljava,
`oteljava-context-storage`,
`oteljava-testkit`,
sdk.jvm,
`sdk-exporter`.jvm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.typelevel.otel4s.oteljava
import cats.effect.IOLocal
import cats.effect.LiftIO
import cats.effect.MonadCancelThrow
import cats.effect.unsafe.IORuntime
import cats.mtl.Local
import io.opentelemetry.context.{Context => JContext}
import io.opentelemetry.context.ContextStorage
Expand All @@ -35,7 +36,20 @@ import org.typelevel.otel4s.oteljava.context.LocalContext
*/
class IOLocalContextStorage(_ioLocal: () => IOLocal[Context]) extends ContextStorage {
private[this] implicit lazy val ioLocal: IOLocal[Context] = _ioLocal()
private[this] lazy val unsafeThreadLocal = ioLocal.unsafeThreadLocal()
private[this] lazy val unsafeThreadLocal: ThreadLocal[Context] = {
val fiberLocal = ioLocal.unsafeThreadLocal()

new ThreadLocal[Context] {
override def initialValue(): Context =
Context.root

override def get(): Context =
if (IORuntime.isUnderFiberContext()) fiberLocal.get() else super.get()

override def set(value: Context): Unit =
if (IORuntime.isUnderFiberContext()) fiberLocal.set(value) else super.set(value)
}
}

@inline private[this] def unsafeCurrent: Context =
unsafeThreadLocal.get()
Expand Down Expand Up @@ -82,7 +96,7 @@ object IOLocalContextStorage {
} else {
F.raiseError(
new IllegalStateException(
"IOLocal propagation must be enabled with: -Dcats.effect.ioLocalPropagation=true"
"IOLocal propagation must be enabled with: -Dcats.effect.trackFiberContext=true"
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ class IOLocalContextStorageSuite extends CatsEffectSuite {
}
}

// see https://discord.com/channels/632277896739946517/839263556754472990/1317163027451088926
test("works as a Java-only ContextStorage".ignore) {
test("works as a Java-only ContextStorage") {
usingModifiedCtx(_.`with`(key1, "1")) {
assertEquals(Option(jCurrent.get(key1)), Some("1"))
assertEquals(Option(jCurrent.get(key2)), None)
Expand Down

0 comments on commit ea9f53b

Please sign in to comment.