-
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
base: main
Are you sure you want to change the base?
Changes from 11 commits
ac4175c
682abb6
5f460ee
e522ac8
d9a1079
c4410ab
6b542aa
b6a75a0
978ccef
9913704
b6c2b0f
8c31136
6e09fe2
fe7259e
d10303b
e60f793
39ba754
61d5af7
713736b
b9b8fd9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
|
||
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] = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any reason not to make this |
||
IOLocal[Context](Context.root()) | ||
.syncStep(100) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't access |
||
) | ||
} |
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?