Skip to content

Commit

Permalink
Make xtraceoptions_response_key an apm_constant
Browse files Browse the repository at this point in the history
  • Loading branch information
tammy-baylis-swi committed Dec 6, 2022
1 parent 7b8a993 commit ec3cf3a
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 27 deletions.
1 change: 1 addition & 0 deletions solarwinds_apm/apm_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 2 additions & 4 deletions solarwinds_apm/response_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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(
Expand Down
9 changes: 6 additions & 3 deletions solarwinds_apm/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
),
Expand Down Expand Up @@ -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
),
Expand All @@ -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])
Expand Down
5 changes: 0 additions & 5 deletions solarwinds_apm/traceoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
6 changes: 2 additions & 4 deletions solarwinds_apm/w3c_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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()
)
return trace_state.delete(INTL_SWO_X_OPTIONS_RESPONSE_KEY)
12 changes: 1 addition & 11 deletions tests/unit/test_response_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit ec3cf3a

Please sign in to comment.