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

Sorted metric tags to avoid duplicate prom data with gRPC requests #4006

Merged
merged 14 commits into from
May 14, 2022
2 changes: 1 addition & 1 deletion python/seldon_core/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def _merge_labels(self, worker, tags):

@staticmethod
def _generate_tags_key(tags):
return "_".join(["-".join(i) for i in tags.items()])
return "_".join(["-".join(i) for i in sorted(tags.items())])

@staticmethod
def _update_hist(x, vals, sumv):
Expand Down
16 changes: 16 additions & 0 deletions python/tests/test_runtime_metrics_tags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import logging
import os
from collections import OrderedDict

import numpy as np
import pytest
Expand Down Expand Up @@ -85,6 +86,8 @@ def verify_seldon_metrics(data, mycounter_value, histogram_entries, method):
base_tags_key = SeldonMetrics._generate_tags_key(expected_base_tags)
expected_custom_tags = {"mytag": "mytagvalue", "method": method}
custom_tags_key = SeldonMetrics._generate_tags_key(expected_custom_tags)
expected_tags_key = f"method-{method}_mytag-mytagvalue"
assert custom_tags_key == expected_tags_key
assert data["GAUGE", "runtime_gauge", base_tags_key]["value"] == 42
assert data["GAUGE", "mygauge", base_tags_key]["value"] == 100
assert data["GAUGE", "customtag", custom_tags_key]["value"] == 200
Expand All @@ -99,6 +102,19 @@ def verify_seldon_metrics(data, mycounter_value, histogram_entries, method):
)


def test_generate_tags_key():
# initializing two different kinds of dictionary
insertion_order = OrderedDict({"b": "b", "a": "a"})
sorted_order = {"a": "a", "b": "b"}
# assert the items in the list differ based on order
assert list(insertion_order.items()) != list(sorted_order.items())

insertion_order_tag = SeldonMetrics._generate_tags_key(insertion_order)
sorted_order_tag = SeldonMetrics._generate_tags_key(sorted_order)
# same tag generated irrespective of order
assert insertion_order_tag == sorted_order_tag


@pytest.mark.parametrize("cls", [UserObject])
def test_seldon_runtime_data_predict(cls, client_gets_metrics):
user_object = cls()
Expand Down