opentracing-cpp icon indicating copy to clipboard operation
opentracing-cpp copied to clipboard

Method to reset the global tracer?

Open ecourreges-orange opened this issue 5 years ago • 5 comments

Hello, Is it a conscious choice not to have provided a ResetGlobal method or equivalent on the tracer? I suppose the shared_ptr is cleaned up at global shutdown of an application, but that limits the opportunities for "controlled" init/cleanup of the singleton.

Would this be the preferred way to do it with the current opentracing code?

void closeOpenTracer() 
{ 
    opentracing::Tracer::Global()->Close();
    // How do we delete the global tracer pointer?
    // There is no public method to do so
    //std::shared_ptr<opentracing::Tracer> t;
    // Reinit with an empty shared pointer ?
    //opentracing::Tracer::InitGlobal(t);
}

Thank you, Regards, Emmanuel.

ecourreges-orange avatar Oct 08 '19 07:10 ecourreges-orange

opentracing::Tracer::InitGlobal({});

However: Tracer::IsGlobalTracerRegistered() will incorrectly return true after this. Should probably be fixed (and simplified).

alnr avatar Oct 08 '19 16:10 alnr

Or use InitGlobal(nullptr)

I think the intention of IsGobalTracerRegistered was to query whether a global tracer was ever registered, but maybe @MikeGoldsmith can clarify?

rnburn avatar Oct 08 '19 17:10 rnburn

The intention was to enable an application, or other library, to detect if a tracer has explicitly registered. Shutdown / cleanup wasn't part of the original design.

I think having a ResetGlobal function to cleanup the registered tracer that also sets IsGlobalTracerRegistered to false makes sense.

MikeGoldsmith avatar Oct 09 '19 09:10 MikeGoldsmith

Wouldn't returning a null pointer (default-constructed shared_ptr) have exactly the same semantics?

alnr avatar Oct 09 '19 10:10 alnr

IsGlobalTracerRegistered uses an internal bool to determine if a tracer has been registered and is set when InitGlobal is called. Calling InitGlobal(nullptr) would not reset the flag.

We have a bool property to determine if a tracer was registered because we want to know if the app intentionally registers a tracer, and is not just a null or default noop tracer.

Adding ResetGlobal is explicit where it sets the tracer to a nullptr and resets the flag to false and to be used during app cleanup.

MikeGoldsmith avatar Oct 09 '19 12:10 MikeGoldsmith