Make GlobalTracer uses more threadsafe
See original GitHub issueI have a multithreaded app which causes some unpredictable behaviors when trying to get a globally registered tracer. A specific complication appears to have some relationship to byteman when using byteman rules. I have found that sometimes I call GlobalTracer.get()
and receive a noop, while other times I get a tracer initialized from some byteman execution. I attempted to resolve this by conditionally registering the tracer myself:
Tracer tracer;
if (GlobalTracer.isRegistered()) {
tracer = GlobalTracer.get();
} else {
tracer = TracerResolver.resolveTracer();
GlobalTracer.register(tracer);
}
However, this was affected by a race condition somewhere. I was able to resolve it by synchronizing on the GlobalTracer.class
:
Tracer tracer;
synchronized (GlobalTracer.class) {
if (GlobalTracer.isRegistered()) {
tracer = GlobalTracer.get();
} else {
tracer = TracerResolver.resolveTracer();
GlobalTracer.register(tracer);
}
}
but this seems like something that would be nice to incorporate into the API itself. Note that this still causes the agent to log an exception:
! java.lang.IllegalStateException: There is already a current global Tracer registered.
! at io.opentracing.util.GlobalTracer.register(GlobalTracer.java:92)
! at io.opentracing.contrib.agent.OpenTracingHelper.initTracer(OpenTracingHelper.java:80)
but it appears to work anyway.
I’ll submit a method proposal for streamlining this shortly.
Issue Analytics
- State:
- Created 6 years ago
- Comments:24 (16 by maintainers)
Top GitHub Comments
Having the
get
unsynchronized is more important I guess.I will close it and open a new one for adding a
registerIfAbsent(Tracer tracer)