-
Notifications
You must be signed in to change notification settings - Fork 36
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
rossabaker
wants to merge
20
commits into
main
Choose a base branch
from
context-storage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
ac4175c
Experimental IOLocalContextStorage
rossabaker 682abb6
Try typelevel/cats-effect#3636
rossabaker 5f460ee
Apply suggestions from code review
rossabaker e522ac8
Get latest cats-effect branch
rossabaker d9a1079
Fine, have your headers
rossabaker c4410ab
Unused import
rossabaker 6b542aa
Get testkit out of main dependenecies
rossabaker b6a75a0
Attmept at a ContextStorageProvider SPI
rossabaker 978ccef
Use syncStep to initialize the local context
rossabaker 9913704
Update Cats Effect snapshot
rossabaker b6c2b0f
Remove direct reference to IOLocalContextStorageProvider
rossabaker 8c31136
Unbox IOLocal
rossabaker 6e09fe2
Merge branch 'main' into context-storage
armanbilge fe7259e
Fix compile, update to CE snapshot
armanbilge d10303b
Merge branch 'main' into context-storage
NthPortal e60f793
Finish implementing `IOLocalContextStorage`
NthPortal 39ba754
fixup! Finish implementing `IOLocalContextStorage`
NthPortal 61d5af7
Merge branch 'upstream-main' into context-storage
iRevive 713736b
Update cats-effect, fix implementation
iRevive b9b8fd9
Fix CI workflow
iRevive File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,39 @@ | ||
/* | ||
* 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 io.opentelemetry.api.trace.{Span => JSpan} | ||
import org.typelevel.otel4s.context.LocalProvider | ||
import org.typelevel.otel4s.oteljava.IOLocalContextStorage | ||
import org.typelevel.otel4s.oteljava.OtelJava | ||
import org.typelevel.otel4s.oteljava.context.Context | ||
|
||
object ContextStorageExample extends IOApp.Simple { | ||
|
||
def run: IO[Unit] = { | ||
implicit val provider: LocalProvider[IO, Context] = IOLocalContextStorage.localProvider[IO] | ||
OtelJava.autoConfigured[IO]().use { otelJava => | ||
otelJava.tracerProvider.tracer("com.service").get.flatMap { tracer => | ||
tracer.span("test").use { span => // start 'test' span using otel4s | ||
val jSpanContext = JSpan.current().getSpanContext // get a span from a ThreadLocal var | ||
IO.println(s"jCtx: ${jSpanContext}, Otel4s ctx: ${span.context}") | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
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
1 change: 1 addition & 0 deletions
1
...rage/src/main/resources/META-INF/services/io.opentelemetry.context.ContextStorageProvider
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 @@ | ||
org.typelevel.otel4s.oteljava.IOLocalContextStorageProvider |
98 changes: 98 additions & 0 deletions
98
.../context-storage/src/main/scala/org/typelevel/otel4s/oteljava/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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* 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.oteljava | ||
|
||
import cats.effect.IOLocal | ||
import cats.effect.LiftIO | ||
import cats.effect.MonadCancelThrow | ||
import cats.mtl.Local | ||
import io.opentelemetry.context.{Context => JContext} | ||
import io.opentelemetry.context.ContextStorage | ||
import io.opentelemetry.context.Scope | ||
import org.typelevel.otel4s.context.LocalProvider | ||
import org.typelevel.otel4s.instances.local._ | ||
import org.typelevel.otel4s.oteljava.context.Context | ||
import org.typelevel.otel4s.oteljava.context.LocalContext | ||
|
||
/** A `ContextStorage` backed by an [[cats.effect.IOLocal `IOLocal`]] of a | ||
* [[org.typelevel.otel4s.oteljava.context.Context `Context`]] that also provides [[cats.mtl.Local `Local`]] instances | ||
* that reflect the state of the backing `IOLocal`. Usage of `Local` and `ContextStorage` methods will be consistent | ||
* and stay in sync as long as effects are threaded properly. | ||
*/ | ||
class IOLocalContextStorage(_ioLocal: () => IOLocal[Context]) extends ContextStorage { | ||
private[this] implicit lazy val ioLocal: IOLocal[Context] = _ioLocal() | ||
private[this] lazy val unsafeThreadLocal = ioLocal.unsafeThreadLocal() | ||
|
||
@inline private[this] def unsafeCurrent: Context = | ||
unsafeThreadLocal.get() | ||
|
||
override def attach(toAttach: JContext): Scope = { | ||
val previous = unsafeCurrent | ||
unsafeThreadLocal.set(Context.wrap(toAttach)) | ||
() => unsafeThreadLocal.set(previous) | ||
} | ||
|
||
override def current(): JContext = | ||
unsafeCurrent.underlying | ||
|
||
/** @return | ||
* a [[cats.mtl.Local `Local`]] of a [[org.typelevel.otel4s.oteljava.context.Context `Context`]] that reflects the | ||
* state of the backing `IOLocal` | ||
*/ | ||
def local[F[_]: MonadCancelThrow: LiftIO]: LocalContext[F] = implicitly | ||
} | ||
|
||
object IOLocalContextStorage { | ||
|
||
/** Returns a [[cats.mtl.Local `Local`]] of a [[org.typelevel.otel4s.oteljava.context.Context `Context`]] if an | ||
* [[`IOLocalContextStorage`]] is configured to be used as the `ContextStorage` for the Java otel library. | ||
* | ||
* Raises an exception if an [[`IOLocalContextStorage`]] is __not__ configured to be used as the `ContextStorage` for | ||
* the Java otel library, or if [[cats.effect.IOLocal `IOLocal`]] propagation is not enabled. | ||
* | ||
* @example | ||
* {{{ | ||
* implicit val localProvider: LocalProvider[IO, Context] = IOLocalContextStorage.localProvider | ||
* OtelJava.autoConfigured[IO].use { otel4s => | ||
* ... | ||
* } | ||
* }}} | ||
*/ | ||
def localProvider[F[_]: LiftIO](implicit F: MonadCancelThrow[F]): LocalProvider[F, Context] = | ||
new LocalProvider[F, Context] { | ||
def local: F[Local[F, Context]] = | ||
ContextStorage.get() match { | ||
case storage: IOLocalContextStorage => | ||
if (IOLocal.isPropagating) { | ||
F.pure(storage.local) | ||
} else { | ||
F.raiseError( | ||
new IllegalStateException( | ||
"IOLocal propagation must be enabled with: -Dcats.effect.ioLocalPropagation=true" | ||
) | ||
) | ||
} | ||
case _ => | ||
F.raiseError( | ||
new IllegalStateException( | ||
"IOLocalContextStorage is not configured for use as the ContextStorageProvider" | ||
) | ||
) | ||
} | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...-storage/src/main/scala/org/typelevel/otel4s/oteljava/IOLocalContextStorageProvider.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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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.oteljava | ||
|
||
import cats.effect.IOLocal | ||
import cats.effect.SyncIO | ||
import cats.syntax.all._ | ||
import io.opentelemetry.context.ContextStorage | ||
import io.opentelemetry.context.ContextStorageProvider | ||
import org.typelevel.otel4s.oteljava.context.Context | ||
|
||
object IOLocalContextStorageProvider { | ||
private lazy val localContext: IOLocal[Context] = | ||
IOLocal[Context](Context.root) | ||
.syncStep(100) | ||
.flatMap( | ||
_.leftMap(_ => | ||
new Error( | ||
"Failed to initialize the local context of the IOLocalContextStorageProvider." | ||
) | ||
).liftTo[SyncIO] | ||
) | ||
.unsafeRunSync() | ||
} | ||
|
||
/** SPI implementation for [[`IOLocalContextStorage`]]. */ | ||
class IOLocalContextStorageProvider extends ContextStorageProvider { | ||
def get(): ContextStorage = | ||
new IOLocalContextStorage(() => IOLocalContextStorageProvider.localContext) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be removed now that tests exist?