Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using TextFormatPropagator to_carrier() then from_carrier() corrupts deserialized SpanContext #1120

Open
bennairn opened this issue Apr 20, 2022 · 1 comment
Labels

Comments

@bennairn
Copy link

Describe your environment.
python 3.9.5
Ubuntu 20.04 via WSL
PyCharm 2021.3.3 (Professional Edition)
opencensus==0.8.0

Steps to reproduce.

from opencensus.trace.tracer import Tracer
from opencensus.trace.propagation.text_format import TextFormatPropagator

tracer = Tracer()
propagator = TextFormatPropagator()

# Serialize the SpanContext to send somewhere else
carrier = propagator.to_carrier(tracer.span_context, {})

# Deserialize the SpanContext, this changes the trace_options.trace_options_byte from "1" (string) to True (bool)
span_2 = propagator.from_carrier(carrier)

# Next line raises ValueError: invalid literal for int() with base10: 'Tru1'
tracer_2 = Tracer(span_context=span_2)

What is the expected behavior?
from_carrier() should set trace_options_byte to "1" not True, allowing tracer_2 to be instantiated

What is the actual behavior?
ValueError - from opencensus/trace/trace_options.py line 65 in get_enabled:
enabled = bool(int(self.trace_options_byte) & _ENABLED_BITMASK)

Additional context.
The example above is minimal, the real implementation creates a Tracer with an Exporter to Azure. The serialization is done in one service, and we'd like to propagate the span context to other downstream services. That's where the deserialization and creation of a second tracer from that SpanContext would happen.

Some of our services are separated via Azure EventGrid, so it's preferable to pass the serialized SpanContext as part of our payload rather than as part of the headers as it's more difficult for us to make changes to EventGrid headers than to just modify an existing payload. If there's an alternative recommended way to use opencensus for this we're not wedded to using TextFormatPropagator (especially if this behaviour is expected and we're just using it incorrectly).

Here's the Traceback for the ValueError:

Traceback (most recent call last):
  File "/mnt/c/Program Files/JetBrains/PyCharm 2020.2.3/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
  File "/home/ben/dev/<my-project>/venv/lib/python3.9/site-packages/opencensus/trace/tracer.py", line 63, in __init__
    self.tracer = self.get_tracer()
  File "/home/ben/dev/<my-project>/venv/lib/python3.9/site-packages/opencensus/trace/tracer.py", line 81, in get_tracer
    self.span_context.trace_options.set_enabled(True)
  File "/home/ben/dev/<my-project>/venv/lib/python3.9/site-packages/opencensus/trace/trace_options.py", line 78, in set_enabled
    self.enabled = self.get_enabled()
  File "/home/ben/dev/<my-project>/venv/lib/python3.9/site-packages/opencensus/trace/trace_options.py", line 65, in get_enabled
    enabled = bool(int(self.trace_options_byte) & _ENABLED_BITMASK)
ValueError: invalid literal for int() with base 10: 'Tru1'
@bennairn bennairn added the bug label Apr 20, 2022
@bennairn
Copy link
Author

This PR appears to address it: #714
although it is unclear (to me) when this is planned to be released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant