question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make GlobalTracer uses more threadsafe

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:24 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
sjoerdtalsmacommented, Sep 25, 2017

Having the get unsynchronized is more important I guess.

0reactions
pavolloffaycommented, Jul 13, 2018

I will close it and open a new one for adding a registerIfAbsent(Tracer tracer)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java Multithreading - Threadsafe Counter - Stack Overflow
Starvation describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress. This ......
Read more >
What is Thread-Safety and How to Achieve it - Baeldung
The ReentrantLock constructor takes an optional fairness boolean parameter. When set to true, and multiple threads are trying to acquire a lock, ...
Read more >
io.opentracing:opentracing-noop 0.20.9 on Maven - Libraries.io
It is a best practice to set the GlobalTracer, even if also making use of cleaner, more modern ... For any thread, at...
Read more >
Thread Safety in Java | DigitalOcean
Java provides multi-threaded environment support using Java Threads, we know that multiple threads created from same Object share object ...
Read more >
opentracing/opentracing-cpp - Gitter
I'll work on a prototyping it but I still need to get legal approval for the ... the documentation that the globalTracer is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found