Skip to content

Commit

Permalink
Add logger and change the enum usage
Browse files Browse the repository at this point in the history
  • Loading branch information
krishung5 committed May 2, 2023
1 parent beece9a commit 79a5e9a
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions qa/python_models/custom_metrics/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ def _get_metrics(self):
return r.text

def _metric_api_helper(self, metric, kind):
# Adding logger to test if custom metrics and logging work together
# as they use the same message queue.
logger = pb_utils.Logger

# The value should be 0.0 before the test
self.assertEqual(metric.value(), 0.0)

# Test increment positive value
increment = 2023.0
metric.increment(increment)
self.assertEqual(metric.value(), increment)
logger.log_info("Incremented metric to : {}".format(metric.value()))

# Test increment negative value
decrement = -23.5
Expand All @@ -56,6 +61,7 @@ def _metric_api_helper(self, metric, kind):
else:
metric.increment(decrement)
self.assertEqual(metric.value(), increment + decrement)
logger.log_info("Decremented metric to : {}".format(metric.value()))

# Test set value
value = 999.9
Expand All @@ -66,12 +72,18 @@ def _metric_api_helper(self, metric, kind):
else:
metric.set(value)
self.assertEqual(metric.value(), value)
logger.log_info("Set metric to : {}".format(metric.value()))

def _dup_metric_helper(self, labels={}):
# Adding logger to test if custom metrics and logging work together
# as they use the same message queue.
logger = pb_utils.Logger

description = "dup metric"
metric_family = pb_utils.MetricFamily(name="test_dup_metric",
description=description,
kind=pb_utils.COUNTER)
metric_family = pb_utils.MetricFamily(
name="test_dup_metric",
description=description,
kind=pb_utils.MetricFamily.COUNTER)

# Verify dupe metrics reference same underlying metric
metric1 = metric_family.Metric(labels=labels)
Expand All @@ -85,6 +97,8 @@ def _dup_metric_helper(self, labels={}):
increment = 7.5
metric1.increment(increment)
self.assertEqual(metric1.value(), metric2.value())
logger.log_info("Incremented metric1 to : {}".format(metric1.value()))
logger.log_info("Incremented metric2 to : {}".format(metric2.value()))

# Assert custom metric/family remains when there's still a reference to it
del metric1
Expand All @@ -95,7 +109,7 @@ def test_counter_e2e(self):
metric_family = pb_utils.MetricFamily(
name="test_counter_e2e",
description="test metric counter kind end to end",
kind=pb_utils.COUNTER)
kind=pb_utils.MetricFamily.COUNTER)
labels = {"example1": "counter_label1", "example2": "counter_label2"}
metric = metric_family.Metric(labels=labels)
self._metric_api_helper(metric, 'counter')
Expand All @@ -108,7 +122,7 @@ def test_gauge_e2e(self):
metric_family = pb_utils.MetricFamily(
name="test_gauge_e2e",
description="test metric gauge kind end to end",
kind=pb_utils.GAUGE)
kind=pb_utils.MetricFamily.GAUGE)
labels = {"example1": "counter_label1", "example2": "counter_label2"}
metric = metric_family.Metric(labels=labels)
self._metric_api_helper(metric, 'gauge')
Expand All @@ -122,13 +136,13 @@ def test_dup_metric_family_diff_kind(self):
metric_family1 = pb_utils.MetricFamily(
name="test_dup_metric_family_diff_kind",
description="test metric family with same name but different kind",
kind=pb_utils.COUNTER)
kind=pb_utils.MetricFamily.COUNTER)
with self.assertRaises(pb_utils.TritonModelException):
metric_family2 = pb_utils.MetricFamily(
name="test_dup_metric_family_diff_kind",
description=
"test metric family with same name but different kind",
kind=pb_utils.GAUGE)
kind=pb_utils.MetricFamily.GAUGE)
self.assertIsNone(metric_family2)

self.assertIsNotNone(metric_family1)
Expand All @@ -139,11 +153,11 @@ def test_dup_metric_family_diff_description(self):
metric_family1 = pb_utils.MetricFamily(
name="test_dup_metric_family_diff_description",
description="first description",
kind=pb_utils.COUNTER)
kind=pb_utils.MetricFamily.COUNTER)
metric_family2 = pb_utils.MetricFamily(
name="test_dup_metric_family_diff_description",
description="second description",
kind=pb_utils.COUNTER)
kind=pb_utils.MetricFamily.COUNTER)

metric2 = metric_family2.Metric()
self.assertEqual(metric2.value(), 0)
Expand All @@ -162,12 +176,14 @@ def test_dup_metric_family_diff_description(self):
def test_dup_metric_family(self):
# Test that adding a duplicate metric family will reuse the original
# and not add another entry to registry
metric_family1 = pb_utils.MetricFamily(name="test_dup_metric_family",
description="dup description",
kind=pb_utils.COUNTER)
metric_family2 = pb_utils.MetricFamily(name="test_dup_metric_family",
description="dup description",
kind=pb_utils.COUNTER)
metric_family1 = pb_utils.MetricFamily(
name="test_dup_metric_family",
description="dup description",
kind=pb_utils.MetricFamily.COUNTER)
metric_family2 = pb_utils.MetricFamily(
name="test_dup_metric_family",
description="dup description",
kind=pb_utils.MetricFamily.COUNTER)

metric_key = "custom_metric_key"
metric1 = metric_family1.Metric(labels={metric_key: "label1"})
Expand Down

0 comments on commit 79a5e9a

Please sign in to comment.