Skip to content
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

Show diff to closest metric match #18975

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions datadog_checks_base/changelog.d/18975.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Show diff to closest metric match when metric test fails
29 changes: 29 additions & 0 deletions datadog_checks_base/datadog_checks/base/stubs/similar.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
MAX_SIMILAR_TO_DISPLAY = 15


def tags_list_to_dict(tags):
return {tag.split(':', 1)[0]: (tag.split(':', 1)[1] if ":" in tag else '') for tag in tags}


def build_similar_elements_msg(expected, submitted_elements):
"""
Return formatted similar elements (metrics, service checks) received compared to submitted elements
Expand All @@ -22,9 +26,34 @@ def build_similar_elements_msg(expected, submitted_elements):
metric_stub.tags.sort()
similar_metrics_to_print.append("{:.2f} {}".format(score, metric_stub))

closest_diff = []
if len(similar_metrics) > 0:
[_, closest] = similar_metrics[0]
closest_dict = closest._asdict()
expected_dict = expected._asdict()
for key in closest_dict:
expected_value = expected_dict[key]
closest_value = closest_dict[key]

if expected_value is not None and expected_value != closest_value:
if key == "tags":
expected_tags_dict = tags_list_to_dict(expected_value)
closest_tags_dict = tags_list_to_dict(closest_value)
for tag in expected_tags_dict:
if closest_tags_dict[tag] != expected_tags_dict[tag]:
closest_diff.append(
f" Expected tag {tag}:{expected_tags_dict[tag]}\n"
+ f" Found {tag}:{closest_tags_dict[tag]}"
)
else:
closest_diff.append(f" Expected {key}: {expected_value}\n Found {closest_value}")

return (
"Expected:\n"
+ " {}\n".format(expected)
+ "Difference to closest:\n"
+ "\n".join(closest_diff)
+ "\n\n"
+ "Similar submitted:\n"
+ "Score Most similar\n"
+ "\n".join(similar_metrics_to_print)
Expand Down
4 changes: 4 additions & 0 deletions datadog_checks_base/tests/stubs/test_aggregator_similar.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def test_build_similar_elements_msg(self, aggregator):
expected_msg = '''
Expected:
MetricStub(name='test.similar_metric', type=None, value=None, tags=None, hostname=None, device=None, flush_first_value=None)
Difference to closest:
Expected name: test.similar_metric
Found test.most_similar_metric

Similar submitted:
Score Most similar
0.88 MetricStub(name='test.most_similar_metric', type=0, value=0.0, tags=[], hostname='', device=None, flush_first_value=False)
Expand Down
Loading