Skip to content

Commit

Permalink
Add tests for metric dict merging logic
Browse files Browse the repository at this point in the history
  • Loading branch information
braedon committed Aug 9, 2020
1 parent 1039289 commit dc42bc9
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 6 deletions.
154 changes: 154 additions & 0 deletions tests/test_merge_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import unittest

from prometheus_es_exporter.metrics import merge_metric_dicts
from tests.utils import convert_metric_dict


class Test(unittest.TestCase):
maxDiff = None

def test_new_metric(self):
old_dict = {
'other': ('other docstring', (), {(): 1}),
}
new_dict = {
'foo': ('test docstring', ('bar', 'baz'), {('a', 'b'): 1}),
'other': ('other docstring', (), {(): 2}),
}

expected = {
'foo{bar="a",baz="b"}': 1,
'other': 2,
}
result = convert_metric_dict(merge_metric_dicts(old_dict, new_dict))
self.assertEqual(expected, result)

def test_updated_metric(self):
old_dict = {
'foo': ('test docstring', ('bar', 'baz'), {('a', 'b'): 1}),
'other': ('other docstring', (), {(): 1}),
}
new_dict = {
'foo': ('test docstring', ('bar', 'baz'), {('a', 'b'): 2}),
'other': ('other docstring', (), {(): 2}),
}

expected = {
'foo{bar="a",baz="b"}': 2,
'other': 2,
}
result = convert_metric_dict(merge_metric_dicts(old_dict, new_dict))
self.assertEqual(expected, result)

def test_missing_metric_preserve(self):
old_dict = {
'foo': ('test docstring', ('bar', 'baz'), {('a', 'b'): 1}),
'other': ('other docstring', (), {(): 1}),
}
new_dict = {
'other': ('other docstring', (), {(): 2}),
}

expected = {
'foo{bar="a",baz="b"}': 1,
'other': 2,
}
result = convert_metric_dict(merge_metric_dicts(old_dict, new_dict, zero_missing=False))
self.assertEqual(expected, result)

def test_missing_metric_zero(self):
old_dict = {
'foo': ('test docstring', ('bar', 'baz'), {('a', 'b'): 1}),
'other': ('other docstring', (), {(): 1}),
}
new_dict = {
'other': ('other docstring', (), {(): 2}),
}

expected = {
'foo{bar="a",baz="b"}': 0,
'other': 2,
}
result = convert_metric_dict(merge_metric_dicts(old_dict, new_dict, zero_missing=True))
self.assertEqual(expected, result)

def test_new_label_keys(self):
old_dict = {
'foo': ('test docstring', ('bar', 'baz'), {('a', 'b'): 1}),
'other': ('other docstring', (), {(): 1}),
}
new_dict = {
'foo': ('test docstring', ('bar', 'baz'), {('a', 'b'): 2,
('c', 'd'): 1}),
'other': ('other docstring', (), {(): 2}),
}

expected = {
'foo{bar="a",baz="b"}': 2,
'foo{bar="c",baz="d"}': 1,
'other': 2,
}
result = convert_metric_dict(merge_metric_dicts(old_dict, new_dict))
self.assertEqual(expected, result)

def test_updated_label_keys(self):
old_dict = {
'foo': ('test docstring', ('bar', 'baz'), {('a', 'b'): 1,
('c', 'd'): 1}),
'other': ('other docstring', (), {(): 1}),
}
new_dict = {
'foo': ('test docstring', ('bar', 'baz'), {('a', 'b'): 2,
('c', 'd'): 2}),
'other': ('other docstring', (), {(): 2}),
}

expected = {
'foo{bar="a",baz="b"}': 2,
'foo{bar="c",baz="d"}': 2,
'other': 2,
}
result = convert_metric_dict(merge_metric_dicts(old_dict, new_dict))
self.assertEqual(expected, result)

def test_missing_label_keys_preserve(self):
old_dict = {
'foo': ('test docstring', ('bar', 'baz'), {('a', 'b'): 1,
('c', 'd'): 1}),
'other': ('other docstring', (), {(): 1}),
}
new_dict = {
'foo': ('test docstring', ('bar', 'baz'), {('a', 'b'): 2}),
'other': ('other docstring', (), {(): 2}),
}

expected = {
'foo{bar="a",baz="b"}': 2,
'foo{bar="c",baz="d"}': 1,
'other': 2,
}
result = convert_metric_dict(merge_metric_dicts(old_dict, new_dict, zero_missing=False))
self.assertEqual(expected, result)

def test_missing_label_keys_zero(self):
old_dict = {
'foo': ('test docstring', ('bar', 'baz'), {('a', 'b'): 1,
('c', 'd'): 1}),
'other': ('other docstring', (), {(): 1}),
}
new_dict = {
'foo': ('test docstring', ('bar', 'baz'), {('a', 'b'): 2}),
'other': ('other docstring', (), {(): 2}),
}

expected = {
'foo{bar="a",baz="b"}': 2,
'foo{bar="c",baz="d"}': 0,
'other': 2,
}
result = convert_metric_dict(merge_metric_dicts(old_dict, new_dict, zero_missing=True))
self.assertEqual(expected, result)


if __name__ == '__main__':
unittest.main()
17 changes: 11 additions & 6 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ def format_metrics(metric_name, label_keys, value_dict):
return metrics


# Converts the parse_response() result into a psuedo-prometheus format
# that is useful for comparing results in tests.
# Uses the 'group_metrics()' function used by the exporter, so effectively
# tests that function.
def convert_result(result):
metric_dict = group_metrics(result)
# Converts a metric into a psuedo-prometheus format that is useful for comparing results in tests.
# Uses the 'group_metrics()' function used by the exporter, so effectively tests that function.
def convert_metric_dict(metric_dict):
return {
metric: value
for metric_name, (metric_doc, label_keys, value_dict) in metric_dict.items()
for metric, value in format_metrics(metric_name, label_keys, value_dict).items()
}


# Converts the parse_response() result into a psuedo-prometheus format that is useful for comparing
# results in tests.
# Uses the 'group_metrics()' function used by the exporter, so effectively tests that function.
def convert_result(result):
metric_dict = group_metrics(result)
return convert_metric_dict(metric_dict)

0 comments on commit dc42bc9

Please sign in to comment.