-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add support for Span Events #200
Draft
marcotc
wants to merge
1
commit into
master
Choose a base branch
from
marcotc/add_agent_span_events_support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ | |
log = logging.getLogger(__name__) | ||
|
||
|
||
DEFAULT_SNAPSHOT_IGNORES = "span_id,trace_id,parent_id,duration,start,metrics.system.pid,metrics.system.process_id,metrics.process_id,metrics._dd.tracer_kr,meta.runtime-id,span_links.trace_id_high,meta.pathway.hash,meta._dd.p.tid" | ||
DEFAULT_SNAPSHOT_IGNORES = "span_id,trace_id,parent_id,duration,start,metrics.system.pid,metrics.system.process_id,metrics.process_id,metrics._dd.tracer_kr,meta.runtime-id,span_links.trace_id_high,span_events.time_unix_nano,meta.pathway.hash,meta._dd.p.tid" | ||
|
||
|
||
def _key_match(d1: Dict[str, Any], d2: Dict[str, Any], key: str) -> bool: | ||
|
@@ -198,13 +198,13 @@ def _diff_spans( | |
results = [] | ||
s1_no_tags = cast( | ||
Dict[str, TopLevelSpanValue], | ||
{k: v for k, v in s1.items() if k not in ("meta", "metrics", "span_links")}, | ||
{k: v for k, v in s1.items() if k not in ("meta", "metrics", "span_links", "span_events")}, | ||
) | ||
s2_no_tags = cast( | ||
Dict[str, TopLevelSpanValue], | ||
{k: v for k, v in s2.items() if k not in ("meta", "metrics", "span_links")}, | ||
{k: v for k, v in s2.items() if k not in ("meta", "metrics", "span_links", "span_events")}, | ||
) | ||
for d1, d2, ignored in [ | ||
for d1, d2, ign in [ | ||
(s1_no_tags, s2_no_tags, ignored), | ||
(s1["meta"], s2["meta"], set(i[5:] for i in ignored if i.startswith("meta."))), | ||
( | ||
|
@@ -216,7 +216,7 @@ def _diff_spans( | |
d1 = cast(Dict[str, Any], d1) | ||
d2 = cast(Dict[str, Any], d2) | ||
diffs = [] | ||
for k in (set(d1.keys()) | set(d2.keys())) - ignored: | ||
for k in (set(d1.keys()) | set(d2.keys())) - ign: | ||
if not _key_match(d1, d2, k): | ||
diffs.append(k) | ||
results.append(diffs) | ||
|
@@ -235,7 +235,7 @@ def _diff_spans( | |
{k: v for k, v in l2.items() if k != "attributes"}, | ||
) | ||
link_diff = [] | ||
for d1, d2, ignored in [ | ||
for d1, d2, ign in [ | ||
(l1_no_tags, l2_no_tags, set(i[11:] for i in ignored if i.startswith("span_links."))), | ||
( | ||
l1.get("attributes") or {}, | ||
|
@@ -246,14 +246,47 @@ def _diff_spans( | |
d1 = cast(Dict[str, Any], d1) | ||
d2 = cast(Dict[str, Any], d2) | ||
diffs = [] | ||
for k in (set(d1.keys()) | set(d2.keys())) - ignored: | ||
for k in (set(d1.keys()) | set(d2.keys())) - ign: | ||
if not _key_match(d1, d2, k): | ||
diffs.append(k) | ||
link_diff.append(diffs) | ||
|
||
link_diffs.append(link_diff) | ||
results.append(link_diffs) # type: ignore | ||
|
||
event_diffs = [] | ||
if len(s1.get("span_events") or []) != len(s2.get("span_events") or []): | ||
results[0].append("span_events") | ||
else: | ||
for e1, e2 in zip(s1.get("span_events") or [], s2.get("span_events") or []): | ||
l1_no_tags = cast( | ||
Dict[str, TopLevelSpanValue], | ||
{k: v for k, v in e1.items() if k != "attributes"}, | ||
) | ||
l2_no_tags = cast( | ||
Dict[str, TopLevelSpanValue], | ||
{k: v for k, v in e2.items() if k != "attributes"}, | ||
) | ||
event_diff = [] | ||
for d1, d2, ign in [ | ||
(l1_no_tags, l2_no_tags, set(i[12:] for i in ignored if i.startswith("span_events."))), | ||
( | ||
e1.get("attributes") or {}, | ||
e2.get("attributes") or {}, | ||
set(i[23:] for i in ignored if i.startswith("span_events.attributes.")), | ||
), | ||
]: | ||
d1 = cast(Dict[str, Any], d1) | ||
d2 = cast(Dict[str, Any], d2) | ||
diffs = [] | ||
for k in (set(d1.keys()) | set(d2.keys())) - ign: | ||
if not _key_match(d1, d2, k): | ||
diffs.append(k) | ||
event_diff.append(diffs) | ||
|
||
event_diffs.append(event_diff) | ||
results.append(event_diffs) # type: ignore | ||
|
||
return cast(Tuple[List[str], List[str], List[str], List[Tuple[List[str], List[str]]]], tuple(results)) | ||
|
||
|
||
|
@@ -279,7 +312,9 @@ def _compare_traces(expected: Trace, received: Trace, ignored: Set[str]) -> None | |
) as frame: | ||
frame.add_item(f"Expected span:\n{pprint.pformat(s_exp)}") | ||
frame.add_item(f"Received span:\n{pprint.pformat(s_rec)}") | ||
top_level_diffs, meta_diffs, metrics_diffs, span_link_diffs = _diff_spans(s_exp, s_rec, ignored) | ||
top_level_diffs, meta_diffs, metrics_diffs, span_link_diffs, span_event_diffs = _diff_spans( | ||
s_exp, s_rec, ignored | ||
) | ||
|
||
for diffs, diff_type, d_exp, d_rec in [ | ||
(top_level_diffs, "span", s_exp, s_rec), | ||
|
@@ -295,7 +330,7 @@ def _compare_traces(expected: Trace, received: Trace, ignored: Set[str]) -> None | |
raise AssertionError( | ||
f"Span{' ' + diff_type if diff_type != 'span' else ''} value '{diff_key}' in expected span but is not in the received span." | ||
) | ||
elif diff_key == "span_links": | ||
elif diff_key in ["span_links", "span_events"]: | ||
raise AssertionError( | ||
f"{diff_type} mismatch on '{diff_key}': got {len(d_rec[diff_key])} values for {diff_key} which does not match expected {len(d_exp[diff_key])}." # type: ignore | ||
) | ||
|
@@ -328,6 +363,30 @@ def _compare_traces(expected: Trace, received: Trace, ignored: Set[str]) -> None | |
f"Span link {diff_type} mismatch on '{diff_key}': got '{d_rec[diff_key]}' which does not match expected '{d_exp[diff_key]}'." | ||
) | ||
|
||
for i, (event_level_diffs, attribute_diffs) in enumerate(span_event_diffs): | ||
for diffs, diff_type, d_exp, d_rec in [ | ||
(event_level_diffs, f"{i}", s_exp["span_events"][i], s_rec["span_events"][i]), | ||
( | ||
attribute_diffs, | ||
f"{i} attributes", | ||
s_exp["span_events"][i].get("attributes") or {}, | ||
s_rec["span_events"][i].get("attributes") or {}, | ||
), | ||
]: | ||
for diff_key in diffs: | ||
if diff_key not in d_exp: | ||
raise AssertionError( | ||
f"Span event {diff_type} value '{diff_key}' in received span event but is not in the expected span event." | ||
) | ||
elif diff_key not in d_rec: | ||
raise AssertionError( | ||
f"Span event {diff_type} value '{diff_key}' in expected span event but is not in the received span event." | ||
) | ||
else: | ||
raise AssertionError( | ||
f"Span event {diff_type} mismatch on '{diff_key}': got '{d_rec[diff_key]}' which does not match expected '{d_exp[diff_key]}'." | ||
) | ||
|
||
|
||
class SnapshotFailure(Exception): | ||
pass | ||
|
@@ -367,6 +426,7 @@ def _ordered_span(s: Span) -> OrderedDictType[str, TopLevelSpanValue]: | |
"meta", | ||
"metrics", | ||
"span_links", | ||
"span_events", | ||
] | ||
for k in order: | ||
if k in s: | ||
|
@@ -385,6 +445,11 @@ def _ordered_span(s: Span) -> OrderedDictType[str, TopLevelSpanValue]: | |
if "attributes" in link: | ||
link["attributes"] = OrderedDict(sorted(link["attributes"].items(), key=operator.itemgetter(0))) | ||
|
||
if "span_events" in d: | ||
for event in d["span_events"]: | ||
if "attributes" in event: | ||
event["attributes"] = OrderedDict(sorted(event["attributes"].items(), key=operator.itemgetter(0))) | ||
|
||
for k in ["meta", "metrics"]: | ||
if k in d and len(d[k]) == 0: | ||
del d[k] | ||
|
@@ -414,6 +479,15 @@ def _snapshot_trace_str(trace: Trace, removed: Optional[List[str]] = None) -> st | |
if "span_links" in span: | ||
for link in span["span_links"]: | ||
link.pop(key[11:], None) # type: ignore | ||
elif key.startswith("span_events.attributes."): | ||
if "span_events" in span: | ||
for event in span["span_events"]: | ||
if "attributes" in event: | ||
event["attributes"].pop(key[23:], None) | ||
marcotc marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+483
to
+486
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Code Quality Violationtoo many nesting levels (...read more)Avoid to nest too many loops together. Having too many loops make your code harder to understand. Learn More |
||
elif key.startswith("span_events."): | ||
if "span_events" in span: | ||
for event in span["span_events"]: | ||
event.pop(key[12:], None) | ||
marcotc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else: | ||
span.pop(key, None) # type: ignore | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
features: | ||
- | | ||
Add agent support for Span Events. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Code Quality Violation
too many nesting levels (...read more)
Avoid to nest too many loops together. Having too many loops make your code harder to understand.
Prefer to organize your code in functions and unit of code you can clearly understand.
Learn More