trace: inconsistent NullContextManager causes runtime crashes for none tracer enabled code
See original GitHub issueThere will be many libraries that we’ll trace, users will use normally without enabling tracing (i.e. using the NoopTracer), for the purposes of distribution. What I mean by this is that we should be able to instrument a library, let users important it, with or without a tracer enabled. Currently without a tracer enabled and trying to use properties of a normal span, I get some super confusing runtime crash
AttributeError: 'NullContextManager' object has no attribute 'children'
of which I had to manually go debugging to realize that in the case where no tracer has been enabled, we fake a span on the NullContextManager. However, this breaks any library out there that uses span annotations, attributes etc.
My use case is that some 4 months ago I trace instrumented Pinterest’s PyMemcache at https://github.com/opencensus-integrations/pymemcache.
Sample repro code
import time
from opencensus.trace import execution_context
from opencensus.trace.tracer import Tracer
from opencensus.trace.samplers import always_on
def main():
sampler = always_on.AlwaysOnSampler()
if 0:
tracer = Tracer(sampler=sampler)
else:
tracer = execution_context.get_opencensus_tracer() or Tracer()
with tracer.span(name='root') as root_span:
root_span.add_attribute('day', 'Friday')
with tracer.span(name='child') as child_span:
child_span.add_annotation('Something in the way')
_ = 0
if __name__ == '__main__':
main()
Results
- On
if 0
, the library crashes
$ python main.py
Traceback (most recent call last):
File "main.py", line 21, in <module>
main()
File "main.py", line 15, in main
root_span.add_attribute('day', 'Friday')
AttributeError: 'NullContextManager' object has no attribute 'add_attribute'
- On
if 1
i.e.
--- main.py 2018-08-24 02:21:58.000000000 -0700
+++ pass.py 2018-08-24 02:21:51.000000000 -0700
@@ -6,7 +6,7 @@
def main():
sampler = always_on.AlwaysOnSampler()
- if 0:
+ if 1:
tracer = Tracer(sampler=sampler)
else:
tracer = execution_context.get_opencensus_tracer() or Tracer()
[_SpanData(name='child', context=SpanContext(trace_id=d7c355ea21844ddf99364b95b55b85c9, span_id=ad1e55c67ea9415f, trace_options=TraceOptions(enabled=True), tracestate=None), span_id='8fba949544aa4b6a', parent_span_id='ad1e55c67ea9415f', attributes={}, start_time='2018-08-24T09:21:33.718154Z', end_time='2018-08-24T09:21:33.718165Z', child_span_count=0, stack_trace=None, time_events=[<opencensus.trace.time_event.TimeEvent object at 0x10736ac10>], links=[], status=None, same_process_as_parent_span=None, span_kind=0)]
[_SpanData(name='root', context=SpanContext(trace_id=d7c355ea21844ddf99364b95b55b85c9, span_id=None, trace_options=TraceOptions(enabled=True), tracestate=None), span_id='ad1e55c67ea9415f', parent_span_id=None, attributes={'day': 'Friday'}, start_time='2018-08-24T09:21:33.718090Z', end_time='2018-08-24T09:21:33.718256Z', child_span_count=0, stack_trace=None, time_events=[], links=[], status=None, same_process_as_parent_span=None, span_kind=0)]
it passes which is the case that we should always have.
I believe that this is a critical bug as any Python library out there that is instrumented with OpenCensus but doesn’t have a tracer enabled is going to crash, so basically if there are any instrumented drivers or Google Cloud libraries and the users haven’t yet turned on OpenCensus in their code, it’s titanic for them unfortunately 😦
I found this bug just now while writing the Memcache integration guide to put up on the website.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (4 by maintainers)
Thanks for the ping, I think this issue is quite critical and it is worrying that that one has been open for about 3 months.
@tgermain Thanks for reporting issue. Can you please open new issue/bug to track this. Closing this bug.