Skip to content

Commit 1d03df8

Browse files
authored
Also apply the regex replaces during the comparison step (#210)
* Also apply the regex replaces during the comparison step Yep, the generation step worked fine. Tested it and got the expected json dumps. Then compared it and it did not match anything. Fix the missing step. * And fix max-split in _parse_map
1 parent 85f2421 commit 1d03df8

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

ddapm_test_agent/agent.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def _parse_map(s: str) -> Dict[str, str]:
9898
>>> _parse_map("a:b,b:c,c:d")
9999
{'a': 'b', 'b': 'c', 'c': 'd'}
100100
"""
101-
return dict([s.strip().split(":", 2) for s in s.split(",") if s.strip()])
101+
return dict([s.strip().split(":", 1) for s in s.split(",") if s.strip()])
102102

103103

104104
def _session_token(request: Request) -> Optional[str]:
@@ -811,7 +811,7 @@ async def handle_snapshot(self, request: Request) -> web.Response:
811811
(f"{{{key}}}", re.compile(regex))
812812
for (key, regex) in (default_attribute_regex_replaces | regex_overrides).items()
813813
)
814-
log.info("using regex placeholders %r", span_removes)
814+
log.info("using regex placeholders %r", attribute_regex_replaces)
815815

816816
if "span_id" in span_removes:
817817
raise AssertionError("Cannot remove 'span_id' from spans")
@@ -856,6 +856,7 @@ async def handle_snapshot(self, request: Request) -> web.Response:
856856
expected_traces=raw_snapshot,
857857
received_traces=received_traces,
858858
ignored=span_ignores,
859+
attribute_regex_replaces=attribute_regex_replaces,
859860
)
860861
elif received_traces:
861862
# Create a new snapshot for the data received

ddapm_test_agent/trace_snapshot.py

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,22 @@ def _trace_similarity(t1: Trace, t2: Trace) -> int:
8484
return score
8585

8686

87-
def _normalize_traces(traces: List[Trace]) -> List[Trace]:
87+
def _walk_span_attributes_with_regex_replaces(
88+
dictionary: Dict[str, Any], attribute_regex_replaces: Dict[str, Pattern[str]]
89+
) -> None:
90+
for key, val in dictionary.items():
91+
if isinstance(val, str):
92+
for placeholder, pattern in attribute_regex_replaces.items():
93+
dictionary[key] = pattern.sub(placeholder, dictionary[key])
94+
elif isinstance(val, Dict):
95+
_walk_span_attributes_with_regex_replaces(val, attribute_regex_replaces)
96+
elif isinstance(val, List):
97+
for v in val:
98+
if isinstance(v, Dict):
99+
_walk_span_attributes_with_regex_replaces(v, attribute_regex_replaces)
100+
101+
102+
def _normalize_traces(traces: List[Trace], attribute_regex_replaces: Dict[str, Pattern[str]]) -> List[Trace]:
88103
normed_traces = []
89104
trace_id_map: Dict[TraceId, Tuple[int, Dict[SpanId, int]]] = {}
90105

@@ -115,6 +130,9 @@ def _normalize_traces(traces: List[Trace]) -> List[Trace]:
115130
span["metrics"] = {}
116131
span_id += 1
117132

133+
if attribute_regex_replaces: # only walk if we actually have something to replace
134+
_walk_span_attributes_with_regex_replaces(cast(Dict[str, Any], span), attribute_regex_replaces)
135+
118136
normed_traces.append(normed_trace)
119137

120138
if old_trace_id != 0:
@@ -401,9 +419,14 @@ def check(self, *args, **kwargs):
401419
pass
402420

403421

404-
def snapshot(expected_traces: List[Trace], received_traces: List[Trace], ignored: List[str]) -> None:
405-
normed_expected = _normalize_traces(expected_traces)
406-
normed_received = _normalize_traces(received_traces)
422+
def snapshot(
423+
expected_traces: List[Trace],
424+
received_traces: List[Trace],
425+
ignored: List[str],
426+
attribute_regex_replaces: Dict[str, Pattern[str]],
427+
) -> None:
428+
normed_expected = _normalize_traces(expected_traces, {})
429+
normed_received = _normalize_traces(received_traces, attribute_regex_replaces)
407430
with CheckTrace.add_frame(
408431
f"compare of {len(normed_expected)} expected trace(s) to {len(normed_received)} received trace(s)"
409432
):
@@ -460,22 +483,7 @@ def _ordered_span(s: Span) -> OrderedDictType[str, TopLevelSpanValue]:
460483
return d # type: ignore
461484

462485

463-
def _walk_span_attributes_with_regex_replaces(
464-
dictionary: Dict[str, Any], attribute_regex_replaces: Dict[str, Pattern[str]]
465-
) -> None:
466-
for key, val in dictionary.items():
467-
if isinstance(val, str):
468-
for placeholder, pattern in attribute_regex_replaces.items():
469-
dictionary[key] = pattern.sub(placeholder, dictionary[key])
470-
elif isinstance(val, Dict):
471-
_walk_span_attributes_with_regex_replaces(val, attribute_regex_replaces)
472-
elif isinstance(val, List):
473-
for v in val:
474-
if isinstance(v, Dict):
475-
_walk_span_attributes_with_regex_replaces(v, attribute_regex_replaces)
476-
477-
478-
def _snapshot_trace_str(trace: Trace, removed: List[str], attribute_regex_replaces: Dict[str, Pattern[str]]) -> str:
486+
def _snapshot_trace_str(trace: Trace, removed: List[str]) -> str:
479487
cmap = child_map(trace)
480488
stack: List[Tuple[int, Span]] = [(0, root_span(trace))]
481489
s = "[\n"
@@ -509,9 +517,6 @@ def _snapshot_trace_str(trace: Trace, removed: List[str], attribute_regex_replac
509517
else:
510518
span.pop(key, None) # type: ignore
511519

512-
if attribute_regex_replaces: # only walk if we actually have something to replace
513-
_walk_span_attributes_with_regex_replaces(cast(Dict[str, Any], span), attribute_regex_replaces)
514-
515520
for i, child in enumerate(reversed(cmap[span["span_id"]])):
516521
if i == 0:
517522
stack.insert(0, (prefix + 3, child))
@@ -525,10 +530,10 @@ def _snapshot_trace_str(trace: Trace, removed: List[str], attribute_regex_replac
525530
return s
526531

527532

528-
def _snapshot_json(traces: List[Trace], removed: List[str], attribute_regex_replaces: Dict[str, Pattern[str]]) -> str:
533+
def _snapshot_json(traces: List[Trace], removed: List[str]) -> str:
529534
s = "["
530535
for t in traces:
531-
s += _snapshot_trace_str(t, removed, attribute_regex_replaces)
536+
s += _snapshot_trace_str(t, removed)
532537
if t != traces[-1]:
533538
s += ",\n"
534539
s += "]\n"
@@ -540,4 +545,4 @@ def generate_snapshot(
540545
removed: Optional[List[str]] = None,
541546
attribute_regex_replaces: Optional[Dict[str, Pattern[str]]] = None,
542547
) -> str:
543-
return _snapshot_json(_normalize_traces(received_traces), removed or [], attribute_regex_replaces or {})
548+
return _snapshot_json(_normalize_traces(received_traces, attribute_regex_replaces or {}), removed or [])
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
Actually make the SNAPSHOT_REGEX_PLACEHOLDERS feature also work during comparison, and not only generation.

0 commit comments

Comments
 (0)