opencensus-python
opencensus-python copied to clipboard
[RFC] Update Flask plugin to allow existing Tracer instance re-use
Background, Details
Right now Flask middleware / plugin creates new Tracer instance for each request.
This it not great when you are using the same tracer singleton instance across the whole application / service and when using other plugins (such as https://github.com/census-instrumentation/opencensus-python/blob/master/contrib/opencensus-ext-google-cloud-clientlibs/) which allow you to specify which tracer instance to use (e.g. using config_integration.trace_integrations([...], tracer=my_singleton_tracer_instance)).
Because existing singleton tracer instance can't re re-used, this means libraries and code which use singleton tracer instance can't be part of the root span created by the Flask middleware.
Before my change:


As you can see, span created by opencensus-ext-google-cloud-clientlibs which uses application / service global Tracer instance is not part of the request span (but I want it to be).
After my change:

I pass existing singleton tracer instance to Flask middleware and as such, span is correctly part of the root request span,
Proposed Solution
To solve for that, I propose adding new optional tracer argument to the constructor.
When this argument is provided, sampler, exporter and propagator arguments are redundant and mutually exclusive (if agreed on this approach, I can update docs and code to throw if mutually exclusive arguments are provided).
TODO
- [x] Update docs
- [x] Add tests
- [x] Update code to throw if mutually exclusive arguments are provided
This looks like a good solution to me too pending the TODOs in the description.
I pushed a change which adds docstring, test cases and throws inside a constructor if mutually exclusive arguments are provided.
I believe the tests / cover target should pass now.
@Kami would you help to update the CHANGELOG.md file given this is an interface change?
FYI - we plan to release around end of this month.