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

Add IOLocalContextStorage #214

Merged
merged 30 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ac4175c
Experimental IOLocalContextStorage
rossabaker May 12, 2023
682abb6
Try typelevel/cats-effect#3636
rossabaker May 18, 2023
5f460ee
Apply suggestions from code review
rossabaker May 18, 2023
e522ac8
Get latest cats-effect branch
rossabaker May 18, 2023
d9a1079
Fine, have your headers
rossabaker May 18, 2023
c4410ab
Unused import
rossabaker May 19, 2023
6b542aa
Get testkit out of main dependenecies
rossabaker May 19, 2023
b6a75a0
Attmept at a ContextStorageProvider SPI
rossabaker May 19, 2023
978ccef
Use syncStep to initialize the local context
rossabaker May 19, 2023
9913704
Update Cats Effect snapshot
rossabaker May 22, 2023
b6c2b0f
Remove direct reference to IOLocalContextStorageProvider
rossabaker May 22, 2023
8c31136
Unbox IOLocal
rossabaker May 23, 2023
6e09fe2
Merge branch 'main' into context-storage
armanbilge Jun 28, 2023
fe7259e
Fix compile, update to CE snapshot
armanbilge Jun 28, 2023
d10303b
Merge branch 'main' into context-storage
NthPortal Oct 4, 2023
e60f793
Finish implementing `IOLocalContextStorage`
NthPortal Aug 31, 2023
39ba754
fixup! Finish implementing `IOLocalContextStorage`
NthPortal Oct 4, 2023
61d5af7
Merge branch 'upstream-main' into context-storage
iRevive Dec 13, 2024
713736b
Update cats-effect, fix implementation
iRevive Dec 13, 2024
b9b8fd9
Fix CI workflow
iRevive Dec 13, 2024
653ebf0
Use cats-effect 3.6.0-RC1
iRevive Dec 28, 2024
5cf5a4e
Fix docs
iRevive Dec 28, 2024
5ad342c
Merge branch 'upstream-main' into context-storage
iRevive Dec 31, 2024
44ddc9a
Add minimal documentation
iRevive Dec 31, 2024
05b886d
Run scalafix
iRevive Dec 31, 2024
45c08f6
Update doc example
iRevive Dec 31, 2024
b54341a
Regenerate ci.yml
iRevive Dec 31, 2024
333fd4e
Merge branch 'refs/heads/upstream-main' into context-storage
iRevive Dec 31, 2024
f1f0925
Regenerate ci.yml
iRevive Dec 31, 2024
5cd592f
Fix compilation issue
iRevive Dec 31, 2024
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
11 changes: 6 additions & 5 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-34-2cf72a5"
val CatsMtlVersion = "1.3.1"
val FS2Version = "3.6.1"
val MUnitVersion = "1.0.0-M7"
Expand Down Expand Up @@ -150,11 +150,11 @@ lazy val testkit = crossProject(JVMPlatform)
lazy val `java-common` = project
.in(file("java/common"))
.enablePlugins(BuildInfoPlugin)
.dependsOn(`core-common`.jvm, `testkit-common`.jvm)
.dependsOn(`core-common`.jvm, `testkit-common`.jvm % Test)
.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 All @@ -168,7 +168,7 @@ lazy val `java-common` = project

lazy val `java-metrics` = project
.in(file("java/metrics"))
.dependsOn(`java-common`, `core-metrics`.jvm, `testkit-metrics`.jvm)
.dependsOn(`java-common`, `core-metrics`.jvm, `testkit-metrics`.jvm % Test)
.settings(munitDependencies)
.settings(
name := "otel4s-java-metrics",
Expand Down Expand Up @@ -222,7 +222,7 @@ lazy val benchmarks = project
.enablePlugins(NoPublishPlugin)
.enablePlugins(JmhPlugin)
.in(file("benchmarks"))
.dependsOn(core.jvm, java)
.dependsOn(core.jvm, java, `testkit-metrics`.jvm)
.settings(
name := "otel4s-benchmarks"
)
Expand All @@ -241,6 +241,7 @@ lazy val examples = project
),
run / fork := true,
javaOptions += "-Dotel.java.global-autoconfigure.enabled=true",
javaOptions += "-Dcats.effect.tracing.dumpLocals=true",
envVars ++= Map(
"OTEL_PROPAGATORS" -> "b3multi",
"OTEL_SERVICE_NAME" -> "Trace Example"
Expand Down
48 changes: 48 additions & 0 deletions examples/src/main/scala/ContextStorageExample.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import cats.effect.IO
import cats.effect.IOApp
import cats.effect.Resource
import io.opentelemetry.context.Context
import io.opentelemetry.context.ContextKey
import io.opentelemetry.context.ContextStorage

import java.util.logging._

object ContextStorageExample extends IOApp.Simple {
NthPortal marked this conversation as resolved.
Show resolved Hide resolved

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)
}
_ <- IO.println(ContextStorage.get.getClass())
_ <- Resource
.make(IO(Context.root().`with`(key, "hello").makeCurrent()))(scope =>
IO(scope.close())
)
.surround(printKey)
_ <- printKey
} yield ()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.typelevel.otel4s.java.IOLocalContextStorageProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.typelevel.otel4s.java

import cats.Eval
import cats.effect.IOLocal
import cats.effect.unsafe.IOLocals
import io.opentelemetry.context.Context
import io.opentelemetry.context.ContextStorage
import io.opentelemetry.context.Scope

class IOLocalContextStorage(
ioLocal: Eval[IOLocal[Context]]
) extends ContextStorage {

rossabaker marked this conversation as resolved.
Show resolved Hide resolved
override def attach(toAttach: Context): Scope = {
val previous = current()
IOLocals.set(ioLocal.value, toAttach)
new Scope {
def close() = IOLocals.set(ioLocal.value, previous)
}
}

override def current(): Context =
IOLocals.get(ioLocal.value)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.typelevel.otel4s.java

import cats.Eval
import cats.effect.IOLocal
import cats.effect.SyncIO
import cats.syntax.all._
import io.opentelemetry.context.Context
import io.opentelemetry.context.ContextStorage
import io.opentelemetry.context.ContextStorageProvider

object IOLocalContextStorageProvider {
val localContext: IOLocal[Context] =
Copy link
Contributor

Choose a reason for hiding this comment

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

any reason not to make this lazy?

IOLocal[Context](Context.root())
.syncStep(100)
Copy link
Member Author

Choose a reason for hiding this comment

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

I think 1 is sufficient, but this would be a stupid reason to have a fatal error, so I was generous.

.flatMap(
_.leftMap(_ =>
new Error(
"Failed to initialize the local context of the IOLocalContextStorageProvider."
)
).liftTo[SyncIO]
)
.unsafeRunSync()
}

class IOLocalContextStorageProvider extends ContextStorageProvider {
def get(): ContextStorage =
new IOLocalContextStorage(
Eval.later(IOLocalContextStorageProvider.localContext)
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 can't access Context.root() before the storage is initialized. Yikes!

)
}