From 600e5b2d210c49daa67caf62eadd4f4dd523c14a Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Wed, 27 Apr 2022 10:37:06 -0700 Subject: [PATCH 1/2] Custom Propagator gets tracestate from carrier in case of upstream propagators --- opentelemetry_distro_solarwinds/propagator.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/opentelemetry_distro_solarwinds/propagator.py b/opentelemetry_distro_solarwinds/propagator.py index b11d50ee5..b4054a880 100644 --- a/opentelemetry_distro_solarwinds/propagator.py +++ b/opentelemetry_distro_solarwinds/propagator.py @@ -74,14 +74,16 @@ def inject( context: typing.Optional[Context] = None, setter: textmap.Setter = textmap.default_setter, ) -> None: - """Injects sw tracestate and trace options from SpanContext into carrier for HTTP request""" + """Injects sw tracestate from carrier into carrier for HTTP request, to get + tracestate injected by previous propagators""" span = trace.get_current_span(context) span_context = span.get_span_context() - trace_state = span_context.trace_state sw_value = W3CTransformer.sw_from_context(span_context) + trace_state_header = carrier.get(self._TRACESTATE_HEADER_NAME, None) - # Prepare carrier with context's or new tracestate - if trace_state: + # Prepare carrier with carrier's or new tracestate + if trace_state_header: + trace_state = TraceState.from_header(trace_state_header) # Check if trace_state already contains sw KV if SW_TRACESTATE_KEY in trace_state.keys(): # If so, modify current span_id and trace_flags, and move to beginning of list From ede5686fa2ef84a59cc8ab68f8c8fda9d089b3de Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Wed, 27 Apr 2022 17:42:07 -0700 Subject: [PATCH 2/2] Call TraceState.from_header correctly --- opentelemetry_distro_solarwinds/propagator.py | 2 +- opentelemetry_distro_solarwinds/sampler.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/opentelemetry_distro_solarwinds/propagator.py b/opentelemetry_distro_solarwinds/propagator.py index b4054a880..46b578dd1 100644 --- a/opentelemetry_distro_solarwinds/propagator.py +++ b/opentelemetry_distro_solarwinds/propagator.py @@ -83,7 +83,7 @@ def inject( # Prepare carrier with carrier's or new tracestate if trace_state_header: - trace_state = TraceState.from_header(trace_state_header) + trace_state = TraceState.from_header([trace_state_header]) # Check if trace_state already contains sw KV if SW_TRACESTATE_KEY in trace_state.keys(): # If so, modify current span_id and trace_flags, and move to beginning of list diff --git a/opentelemetry_distro_solarwinds/sampler.py b/opentelemetry_distro_solarwinds/sampler.py index a4610d28e..378e2ab49 100644 --- a/opentelemetry_distro_solarwinds/sampler.py +++ b/opentelemetry_distro_solarwinds/sampler.py @@ -348,9 +348,9 @@ def calculate_attributes( trace_state_no_response = self.remove_response_from_sw(trace_state) else: # Must retain all potential tracestate pairs for attributes - attr_trace_state = TraceState.from_header( + attr_trace_state = TraceState.from_header([ tracestate_capture - ) + ]) new_attr_trace_state = attr_trace_state.update( SW_TRACESTATE_KEY, W3CTransformer.sw_from_span_and_decision(