-
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.
Finish implementing
IOLocalContextStorage
Finish implementing `IOLocalContextStorage` and `IOLocalContextStorageProvider` for sharing context modifications between Java and Scala instrumentation.
- Loading branch information
Showing
9 changed files
with
387 additions
and
52 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
1 change: 0 additions & 1 deletion
1
...mmon/src/main/resources/META-INF/services/io.opentelemetry.context.ContextStorageProvider
This file was deleted.
Oops, something went wrong.
40 changes: 0 additions & 40 deletions
40
java/common/src/main/scala/org/typelevel/otel4s/java/IOLocalContextStorage.scala
This file was deleted.
Oops, something went wrong.
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.java.IOLocalContextStorageProvider |
92 changes: 92 additions & 0 deletions
92
java/context-storage/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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* 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.effect.IOLocal | ||
import cats.effect.LiftIO | ||
import cats.effect.MonadCancelThrow | ||
import cats.effect.unsafe.IOLocals | ||
import io.opentelemetry.context.{Context => JContext} | ||
import io.opentelemetry.context.ContextStorage | ||
import io.opentelemetry.context.Scope | ||
import org.typelevel.otel4s.java.context.Context | ||
import org.typelevel.otel4s.java.context.LocalContext | ||
import org.typelevel.otel4s.java.instances._ | ||
|
||
/** A `ContextStorage` backed by an [[cats.effect.IOLocal `IOLocal`]] of a | ||
* [[`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() | ||
|
||
@inline private[this] def unsafeCurrent: Context = | ||
IOLocals.get(ioLocal) | ||
|
||
override def attach(toAttach: JContext): Scope = { | ||
val previous = unsafeCurrent | ||
IOLocals.set(ioLocal, Context.wrap(toAttach)) | ||
() => IOLocals.set(ioLocal, previous) | ||
} | ||
|
||
override def current(): JContext = | ||
unsafeCurrent.underlying | ||
|
||
/** @return | ||
* a [[cats.mtl.Local `Local`]] of a [[`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 [[`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. | ||
*/ | ||
def providedLocal[F[_]: LiftIO](implicit | ||
F: MonadCancelThrow[F] | ||
): F[LocalContext[F]] = | ||
ContextStorage.get() match { | ||
case storage: IOLocalContextStorage => | ||
// TODO: check `IOLocals.arePropagating` instead once our dependencies | ||
// are updated | ||
if (java.lang.Boolean.getBoolean("cats.effect.ioLocalPropagation")) { | ||
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" | ||
) | ||
) | ||
} | ||
} |
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
Oops, something went wrong.