From e130e2c38e1043e1d016823eb1d3ab6a0bc21da8 Mon Sep 17 00:00:00 2001 From: harshita-meena <70981082+harshita-meena@users.noreply.github.com> Date: Sat, 14 May 2022 04:29:56 -0700 Subject: [PATCH] Sorted metric tags to avoid duplicate prom data with gRPC requests (#4006) * Update seldon_methods.py * Update seldon_methods.py * Update seldon_methods.py * Test commit to trigger CI * iterate all tags in list of metrics * remove spaces * Update seldon_methods.py * Making global change to generate tags * Remove extra line * add unit tests * comment fix * fix formatting * fix formatting Co-authored-by: Alejandro Saucedo --- python/seldon_core/metrics.py | 2 +- python/tests/test_runtime_metrics_tags.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/python/seldon_core/metrics.py b/python/seldon_core/metrics.py index 15fe3a26b7..713fd1f8c3 100644 --- a/python/seldon_core/metrics.py +++ b/python/seldon_core/metrics.py @@ -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): diff --git a/python/tests/test_runtime_metrics_tags.py b/python/tests/test_runtime_metrics_tags.py index 571ff1df81..3f9160a695 100644 --- a/python/tests/test_runtime_metrics_tags.py +++ b/python/tests/test_runtime_metrics_tags.py @@ -1,6 +1,7 @@ import json import logging import os +from collections import OrderedDict import numpy as np import pytest @@ -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 @@ -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()