opentracing / opentracing-java

OpenTracing API for Java. 🛑 This library is DEPRECATED! https://github.com/opentracing/specification/issues/163
http://opentracing.io
Apache License 2.0
1.68k stars 344 forks source link

Access Custom Tracer's fields through GlobalTracer.get() #365

Open Ravi18Patel opened 4 years ago

Ravi18Patel commented 4 years ago

I have Created my custom tracer and added some methods to it. Now If I register a tracer on GlobalTracer and try to retrieve it using Globaltracer.get() I am getting an instance of GlobalTracer. The tracer which I have passed is stored in tracer field of GlobalTracer but I don't see any api to access that tracer. How can I access methods written in my custom tracer?

sjoerdtalsma commented 4 years ago

The GlobalTracer -by design- only exposes the OpenTracing API. This has the advantage that you're guaranteed of it functioning correctly and discourages people working around it by typecasting.

Technically it also helps reason about the state in an uninitialized application but that is secondary.

Unfortunately this also means that you cannot simply cast the globaltracer to any specific implementation; if you need a specific implementation you should create your own construct to access it.

Technically some kind of 'unwrap' may be possible, but I don't know how other contributors feel about that. Documenting such unwrap method is harder than it looks as it may return unpredictable results during GlobalTracer registration.

whiskeysierra commented 4 years ago

My two cents: Don't use GlobalTracer at all. Accessing it using dependency injection makes for so much cleaner and more sane code.

sjoerdtalsma commented 4 years ago

My two cents: Don't use GlobalTracer at all. Accessing it using dependency injection makes for so much cleaner and more sane code.

Yes, I completely forgot go mention this ⬆️ ; the GlobalTracer is intended for libraries that have no dependency injection; so by all means set it at application initialisation, but try to avoid using it yourself if at all possible.

Ravi18Patel commented 4 years ago

The documentation says that Global tracer forwards all of its methods to registered tracer. But I don't see any use of that by preventing users from using the registered tracer. Wouldn't it be better if there is one more method for eg. getRegisteredTracer() which gives functionality to access the registered tracer through global tracer.