<div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Hi,</div><div><br></div>I made a "polyfill" for what I plan to use to prepare for ScopedValue.<div><br></div><div>Here is some feedback, from my initial playing with it.</div><div><br></div><div>OT Context can be used as autocloseable in try with resources.</div><div>ScopedValue is from what I can see meant to be wrapped in Runnable or Callable.</div><div><br></div><div>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, </div><div><br></div><div dir="ltr">runWhere(CTX, value,()->{</div> //do something<br><div><span style="padding:0px 0px 0px 2px"><span style="padding:0px 0px 0px 2px"><span style="color:rgb(0,0,0);font-family:Consolas;font-size:10pt;white-space:pre-wrap">});</span></span><span style="color:rgb(0,0,0);font-family:Consolas;font-size:10pt;white-space:pre-wrap"></span></span></div><div><span style="padding:0px 0px 0px 2px"><span style="padding:0px 0px 0px 2px"><span style="color:rgb(0,0,0);font-family:Consolas;font-size:10pt;white-space:pre-wrap"><br></span></span></span></div><div>versus try with resources</div><div><br></div><div>try (Scope ignored = Context.current().with(CTX.KEY, value).makeCurrent()) {<br> //do something<br>}</div><div><br></div><div>...........</div><div><br></div><div><br></div><div>If anyone is interested, I am sharing sample code below. Also any suggestions are welcome, especially if I am doing something wrong.</div><div><br></div><div><br></div><div>Here is a bit of my initial testing to make it match ScopedValue API,</div><div>made to easily refactor into ScopedValue with some search-replace.</div><div>--------------------------------------------------------------------------------------</div><div>public class MainContext {<br><br> static ScopedValueOT<String> CTX = ScopedValueOT.newInstance("CTX");<br><br> public static void main(String[] args) {<br> echo("opentelemetry Context ScopedValueOT"); <br> echo("");<br> runWhere(CTX, "first value", MainContext::printContext);<br> <br> Thread.startVirtualThread(()->runWhere(CTX, "virtThread value",MainContext::printContext));<br><br> runWhere(CTX, "outer",()->{<br> printContext();<br> <br> where(CTX, "inner value").run(MainContext::printContext);<br><br> printContext();<br> });<br> <br> where(CTX, "second value").run(MainContext::printContext);<br> }<br><br> public static void printContext() {<br> echo("CTX: "+CTX.get());<br> }<br><br> static void echo(String str) {<br> System.out.println(str);<br> }<br>}</div><div>--------------------------------------------------------------------------------------</div><div><br></div><div><br></div><div>Here is polyfill:</div><div>--------------------------------------------------------------------------------------</div><div><br>import java.util.NoSuchElementException;<br>import java.util.concurrent.Callable;<br><br>import io.opentelemetry.context.Context;<br>import io.opentelemetry.context.ContextKey;<br>import io.opentelemetry.context.Scope;<br><br>class ScopedValueOT<T> {<br> private final ContextKey<T> KEY;<br><br> public ScopedValueOT(String name) {<br> KEY = ContextKey.named(name);<br> }<br><br> public T get() {<br> T out = Context.current().get(KEY);<br> if(out == null) throw new NoSuchElementException();<br> return out;<br> }<br><br> public static <T> ScopedValueOT<T> newInstance(String name) {<br> return new ScopedValueOT<T>(name);<br> }<br><br> public static <T> ScopedValueOT<T>.Wrapper where(ScopedValueOT<T> SV, T value) {<br> return SV.new Wrapper(value);<br> }<br><br> public static <T> void runWhere(ScopedValueOT<T> sv, T value, Runnable r) {<br> try (Scope ignored = Context.current().with(sv.KEY, value).makeCurrent()) {<br> r.run();<br> }<br> }<br><br> public static <T, R> R callWhere(ScopedValueOT<T> sv, T value, Callable<? extends R> op) throws Exception {<br> try (Scope ignored = Context.current().with(sv.KEY, value).makeCurrent()) {<br> return op.call();<br> }<br> }<br><br> public class Wrapper{<br> private T value;<br><br> public Wrapper(T value) {<br> this.value = value;<br> }<br> public void run(Runnable r) {<br> runWhere(ScopedValueOT.this, value, r);<br> }<br> }<br>}</div><div>--------------------------------------------------------------------------------------</div><div><br></div><div> br,</div><div>Davor Hrg</div></div>
</div>
</div>