ScopedValue polyfill follow-up
Davor Hrg
hrgdavor at gmail.com
Sun Feb 9 11:58:47 UTC 2025
Hi,
I made a "polyfill" for what I plan to use to prepare for ScopedValue.
Here is some feedback, from my initial playing with it.
OT Context can be used as autocloseable in try with resources.
ScopedValue is from what I can see meant to be wrapped in Runnable or
Callable.
My concern may be misguided, but I do want to ask if there is any overhead
to worry about with creating a lambda to create a scope,
runWhere(CTX, value,()->{
//do something
});
versus try with resources
try (Scope ignored = Context.current().with(CTX.KEY, value).makeCurrent()) {
//do something
}
...........
If anyone is interested, I am sharing sample code below. Also any
suggestions are welcome, especially if I am doing something wrong.
Here is a bit of my initial testing to make it match ScopedValue API,
made to easily refactor into ScopedValue with some search-replace.
--------------------------------------------------------------------------------------
public class MainContext {
static ScopedValueOT<String> CTX = ScopedValueOT.newInstance("CTX");
public static void main(String[] args) {
echo("opentelemetry Context ScopedValueOT");
echo("");
runWhere(CTX, "first value", MainContext::printContext);
Thread.startVirtualThread(()->runWhere(CTX, "virtThread
value",MainContext::printContext));
runWhere(CTX, "outer",()->{
printContext();
where(CTX, "inner value").run(MainContext::printContext);
printContext();
});
where(CTX, "second value").run(MainContext::printContext);
}
public static void printContext() {
echo("CTX: "+CTX.get());
}
static void echo(String str) {
System.out.println(str);
}
}
--------------------------------------------------------------------------------------
Here is polyfill:
--------------------------------------------------------------------------------------
import java.util.NoSuchElementException;
import java.util.concurrent.Callable;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.context.Scope;
class ScopedValueOT<T> {
private final ContextKey<T> KEY;
public ScopedValueOT(String name) {
KEY = ContextKey.named(name);
}
public T get() {
T out = Context.current().get(KEY);
if(out == null) throw new NoSuchElementException();
return out;
}
public static <T> ScopedValueOT<T> newInstance(String name) {
return new ScopedValueOT<T>(name);
}
public static <T> ScopedValueOT<T>.Wrapper where(ScopedValueOT<T> SV, T
value) {
return SV.new Wrapper(value);
}
public static <T> void runWhere(ScopedValueOT<T> sv, T value, Runnable
r) {
try (Scope ignored = Context.current().with(sv.KEY,
value).makeCurrent()) {
r.run();
}
}
public static <T, R> R callWhere(ScopedValueOT<T> sv, T value,
Callable<? extends R> op) throws Exception {
try (Scope ignored = Context.current().with(sv.KEY,
value).makeCurrent()) {
return op.call();
}
}
public class Wrapper{
private T value;
public Wrapper(T value) {
this.value = value;
}
public void run(Runnable r) {
runWhere(ScopedValueOT.this, value, r);
}
}
}
--------------------------------------------------------------------------------------
br,
Davor Hrg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20250209/9426139e/attachment.htm>
More information about the loom-dev
mailing list