From ec3cf3a83bea0593c216633843c54f67f0ebb905 Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Tue, 6 Dec 2022 10:16:56 -0800 Subject: [PATCH] Make xtraceoptions_response_key an apm_constant --- solarwinds_apm/apm_constants.py | 1 + solarwinds_apm/response_propagator.py | 6 ++---- solarwinds_apm/sampler.py | 9 ++++++--- solarwinds_apm/traceoptions.py | 5 ----- solarwinds_apm/w3c_transformer.py | 6 ++---- tests/unit/test_response_propagator.py | 12 +----------- 6 files changed, 12 insertions(+), 27 deletions(-) diff --git a/solarwinds_apm/apm_constants.py b/solarwinds_apm/apm_constants.py index b0bd9126..10fedd76 100644 --- a/solarwinds_apm/apm_constants.py +++ b/solarwinds_apm/apm_constants.py @@ -6,6 +6,7 @@ INTL_SWO_EQUALS_W3C_SANITIZED = "####" INTL_SWO_TRACESTATE_KEY = "sw" INTL_SWO_X_OPTIONS_KEY = "sw_xtraceoptions" +INTL_SWO_X_OPTIONS_RESPONSE_KEY = "xtrace_options_response" INTL_SWO_SIGNATURE_KEY = "sw_signature" INTL_SWO_DEFAULT_TRACES_EXPORTER = "solarwinds_exporter" INTL_SWO_TRACECONTEXT_PROPAGATOR = "tracecontext" diff --git a/solarwinds_apm/response_propagator.py b/solarwinds_apm/response_propagator.py index dec42b31..424bbd3c 100644 --- a/solarwinds_apm/response_propagator.py +++ b/solarwinds_apm/response_propagator.py @@ -12,8 +12,8 @@ INTL_SWO_COMMA_W3C_SANITIZED, INTL_SWO_EQUALS, INTL_SWO_EQUALS_W3C_SANITIZED, + INTL_SWO_X_OPTIONS_RESPONSE_KEY, ) -from solarwinds_apm.traceoptions import XTraceOptions from solarwinds_apm.w3c_transformer import W3CTransformer logger = logging.getLogger(__name__) @@ -72,9 +72,7 @@ def recover_response_from_tracestate(self, tracestate: TraceState) -> str: EQUALS_W3C_SANITIZED becomes EQUALS COMMA_W3C_SANITIZED becomes COMMA """ - sanitized = tracestate.get( - XTraceOptions.get_sw_xtraceoptions_response_key(), None - ) + sanitized = tracestate.get(INTL_SWO_X_OPTIONS_RESPONSE_KEY, None) if not sanitized: return "" return sanitized.replace( diff --git a/solarwinds_apm/sampler.py b/solarwinds_apm/sampler.py index 285eaa72..26f2f533 100644 --- a/solarwinds_apm/sampler.py +++ b/solarwinds_apm/sampler.py @@ -24,6 +24,7 @@ INTL_SWO_COMMA_W3C_SANITIZED, INTL_SWO_EQUALS_W3C_SANITIZED, INTL_SWO_TRACESTATE_KEY, + INTL_SWO_X_OPTIONS_RESPONSE_KEY, ) from solarwinds_apm.apm_noop import Context as NoopContext from solarwinds_apm.extension.oboe import Context @@ -271,7 +272,7 @@ def create_new_trace_state( ) if xtraceoptions and xtraceoptions.options_header: trace_state = trace_state.add( - XTraceOptions.get_sw_xtraceoptions_response_key(), + INTL_SWO_X_OPTIONS_RESPONSE_KEY, self.create_xtraceoptions_response_value( decision, parent_span_context, xtraceoptions ), @@ -315,7 +316,7 @@ def calculate_trace_state( # Not a propagated header, so always an add if xtraceoptions and xtraceoptions.trigger_trace: trace_state = trace_state.add( - XTraceOptions.get_sw_xtraceoptions_response_key(), + INTL_SWO_X_OPTIONS_RESPONSE_KEY, self.create_xtraceoptions_response_value( decision, parent_span_context, xtraceoptions ), @@ -336,7 +337,9 @@ def add_tracestate_capture_to_attributes_dict( self._SW_TRACESTATE_CAPTURE_KEY, None ) if not tracestate_capture: - trace_state_no_response = W3CTransformer.remove_response_from_sw(trace_state) + trace_state_no_response = W3CTransformer.remove_response_from_sw( + trace_state + ) else: # Must retain all potential tracestate pairs for attributes attr_trace_state = TraceState.from_header([tracestate_capture]) diff --git a/solarwinds_apm/traceoptions.py b/solarwinds_apm/traceoptions.py index 9ca4f07f..6e92f66c 100644 --- a/solarwinds_apm/traceoptions.py +++ b/solarwinds_apm/traceoptions.py @@ -15,7 +15,6 @@ class XTraceOptions: """Formats X-Trace-Options and signature for trigger tracing""" - _SW_XTRACEOPTIONS_RESPONSE_KEY = "xtrace_options_response" _XTRACEOPTIONS_CUSTOM = r"^custom-[^\s]*$" _XTRACEOPTIONS_CUSTOM_RE = re.compile(_XTRACEOPTIONS_CUSTOM) @@ -116,7 +115,3 @@ def __init__( options_signature = context.get(INTL_SWO_SIGNATURE_KEY, None) if options_signature: self.signature = options_signature - - @classmethod - def get_sw_xtraceoptions_response_key(cls) -> str: - return cls._SW_XTRACEOPTIONS_RESPONSE_KEY diff --git a/solarwinds_apm/w3c_transformer.py b/solarwinds_apm/w3c_transformer.py index 03d3a49d..78ec0f97 100644 --- a/solarwinds_apm/w3c_transformer.py +++ b/solarwinds_apm/w3c_transformer.py @@ -6,7 +6,7 @@ from opentelemetry.sdk.trace import SpanContext from opentelemetry.trace.span import TraceState -from solarwinds_apm.traceoptions import XTraceOptions +from solarwinds_apm.apm_constants import INTL_SWO_X_OPTIONS_RESPONSE_KEY logger = logging.getLogger(__name__) @@ -80,6 +80,4 @@ def sw_from_span_and_decision(cls, span_id: int, decision: str) -> str: @classmethod def remove_response_from_sw(cls, trace_state: TraceState) -> TraceState: """Remove xtraceoptions response from tracestate""" - return trace_state.delete( - XTraceOptions.get_sw_xtraceoptions_response_key() - ) \ No newline at end of file + return trace_state.delete(INTL_SWO_X_OPTIONS_RESPONSE_KEY) diff --git a/tests/unit/test_response_propagator.py b/tests/unit/test_response_propagator.py index a4bd62a9..44107ab0 100644 --- a/tests/unit/test_response_propagator.py +++ b/tests/unit/test_response_propagator.py @@ -112,19 +112,9 @@ def test_inject_valid_span_context_with_xtraceoptions(self, mocker): ]) def test_recover_response_from_tracestate(self, mocker): - mock_get_key = mocker.Mock() - mock_get_key.configure_mock(return_value="foo") - mock_xtraceoptions_cls = mocker.patch( - "solarwinds_apm.response_propagator.XTraceOptions" - ) - mock_xtraceoptions_cls.configure_mock( - **{ - "get_sw_xtraceoptions_response_key": mock_get_key - } - ) result = SolarWindsTraceResponsePropagator().recover_response_from_tracestate( { - "foo": "bar####baz....qux####quux" + "xtrace_options_response": "bar####baz....qux####quux" } ) assert result == "bar=baz,qux=quux"