Skip to content

Commit

Permalink
parser: ensure samples are of type Sample
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Neumann <marco.neumann@blue-yonder.com>
  • Loading branch information
crepererum committed Jan 7, 2019
1 parent 5aa256d commit e180e1d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion prometheus_client/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def build_metric(name, documentation, typ, samples):
else:
new_samples = []
for s in samples:
new_samples.append(tuple((s[0] + '_total', ) + s[1:]))
new_samples.append(Sample(s[0] + '_total', *s[1:]))
samples = new_samples
metric = Metric(name, documentation, typ)
metric.samples = samples
Expand Down
49 changes: 28 additions & 21 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,27 @@


class TestParse(unittest.TestCase):
def assertEqualMetrics(self, first, second, msg=None):
super(TestParse, self).assertEqual(first, second, msg)

# Test that samples are actually named tuples of type Sample.
for a, b in zip(first, second):
for sa, sb in zip(a.samples, b.samples):
assert sa.name == sb.name

def test_simple_counter(self):
families = text_string_to_metric_families("""# TYPE a counter
# HELP a help
a 1
""")
self.assertEqual([CounterMetricFamily("a", "help", value=1)], list(families))
self.assertEqualMetrics([CounterMetricFamily("a", "help", value=1)], list(families))

def test_simple_gauge(self):
families = text_string_to_metric_families("""# TYPE a gauge
# HELP a help
a 1
""")
self.assertEqual([GaugeMetricFamily("a", "help", value=1)], list(families))
self.assertEqualMetrics([GaugeMetricFamily("a", "help", value=1)], list(families))

def test_simple_summary(self):
families = text_string_to_metric_families("""# TYPE a summary
Expand All @@ -41,7 +48,7 @@ def test_simple_summary(self):
a_sum 2
""")
summary = SummaryMetricFamily("a", "help", count_value=1, sum_value=2)
self.assertEqual([summary], list(families))
self.assertEqualMetrics([summary], list(families))

def test_summary_quantiles(self):
families = text_string_to_metric_families("""# TYPE a summary
Expand All @@ -54,7 +61,7 @@ def test_summary_quantiles(self):
# still need to be able to parse them.
metric_family = SummaryMetricFamily("a", "help", count_value=1, sum_value=2)
metric_family.add_sample("a", {"quantile": "0.5"}, 0.7)
self.assertEqual([metric_family], list(families))
self.assertEqualMetrics([metric_family], list(families))

def test_simple_histogram(self):
families = text_string_to_metric_families("""# TYPE a histogram
Expand All @@ -64,14 +71,14 @@ def test_simple_histogram(self):
a_count 3
a_sum 2
""")
self.assertEqual([HistogramMetricFamily("a", "help", sum_value=2, buckets=[("1", 0.0), ("+Inf", 3.0)])], list(families))
self.assertEqualMetrics([HistogramMetricFamily("a", "help", sum_value=2, buckets=[("1", 0.0), ("+Inf", 3.0)])], list(families))

def test_no_metadata(self):
families = text_string_to_metric_families("""a 1
""")
metric_family = Metric("a", "", "untyped")
metric_family.add_sample("a", {}, 1)
self.assertEqual([metric_family], list(families))
self.assertEqualMetrics([metric_family], list(families))

def test_untyped(self):
# https://github.com/prometheus/client_python/issues/79
Expand All @@ -85,14 +92,14 @@ def test_untyped(self):
Sample("redis_connected_clients", {"instance": "rough-snowflake-web", "port": "6380"}, 10),
Sample("redis_connected_clients", {"instance": "rough-snowflake-web", "port": "6381"}, 12),
]
self.assertEqual([m], list(families))
self.assertEqualMetrics([m], list(families))

def test_type_help_switched(self):
families = text_string_to_metric_families("""# HELP a help
# TYPE a counter
a 1
""")
self.assertEqual([CounterMetricFamily("a", "help", value=1)], list(families))
self.assertEqualMetrics([CounterMetricFamily("a", "help", value=1)], list(families))

def test_blank_lines_and_comments(self):
families = text_string_to_metric_families("""
Expand All @@ -103,14 +110,14 @@ def test_blank_lines_and_comments(self):
a 1
""")
self.assertEqual([CounterMetricFamily("a", "help", value=1)], list(families))
self.assertEqualMetrics([CounterMetricFamily("a", "help", value=1)], list(families))

def test_tabs(self):
families = text_string_to_metric_families("""#\tTYPE\ta\tcounter
#\tHELP\ta\thelp
a\t1
""")
self.assertEqual([CounterMetricFamily("a", "help", value=1)], list(families))
self.assertEqualMetrics([CounterMetricFamily("a", "help", value=1)], list(families))

def test_labels_with_curly_braces(self):
families = text_string_to_metric_families("""# TYPE a counter
Expand All @@ -119,14 +126,14 @@ def test_labels_with_curly_braces(self):
""")
metric_family = CounterMetricFamily("a", "help", labels=["foo", "bar"])
metric_family.add_metric(["bar", "b{a}z"], 1)
self.assertEqual([metric_family], list(families))
self.assertEqualMetrics([metric_family], list(families))

def test_empty_help(self):
families = text_string_to_metric_families("""# TYPE a counter
# HELP a
a 1
""")
self.assertEqual([CounterMetricFamily("a", "", value=1)], list(families))
self.assertEqualMetrics([CounterMetricFamily("a", "", value=1)], list(families))

def test_labels_and_infinite(self):
families = text_string_to_metric_families("""# TYPE a counter
Expand All @@ -137,7 +144,7 @@ def test_labels_and_infinite(self):
metric_family = CounterMetricFamily("a", "help", labels=["foo"])
metric_family.add_metric(["bar"], float('inf'))
metric_family.add_metric(["baz"], float('-inf'))
self.assertEqual([metric_family], list(families))
self.assertEqualMetrics([metric_family], list(families))

def test_spaces(self):
families = text_string_to_metric_families("""# TYPE a counter
Expand All @@ -154,7 +161,7 @@ def test_spaces(self):
metric_family.add_metric(["buz"], 3)
metric_family.add_metric(["biz"], 4)
metric_family.add_metric(["boz"], 5)
self.assertEqual([metric_family], list(families))
self.assertEqualMetrics([metric_family], list(families))

def test_commas(self):
families = text_string_to_metric_families("""# TYPE a counter
Expand All @@ -177,7 +184,7 @@ def test_commas(self):
b = CounterMetricFamily("b", "help", value=2)
c = CounterMetricFamily("c", "help", value=3)
d = CounterMetricFamily("d", "help", value=4)
self.assertEqual([a, b, c, d], list(families))
self.assertEqualMetrics([a, b, c, d], list(families))

def test_multiple_trailing_commas(self):
text = """# TYPE a counter
Expand All @@ -192,7 +199,7 @@ def test_empty_brackets(self):
# HELP a help
a{} 1
""")
self.assertEqual([CounterMetricFamily("a", "help", value=1)], list(families))
self.assertEqualMetrics([CounterMetricFamily("a", "help", value=1)], list(families))

def test_nan(self):
families = text_string_to_metric_families("""a NaN
Expand All @@ -209,7 +216,7 @@ def test_empty_label(self):
metric_family = CounterMetricFamily("a", "help", labels=["foo"])
metric_family.add_metric(["bar"], 1)
metric_family.add_metric([""], 2)
self.assertEqual([metric_family], list(families))
self.assertEqualMetrics([metric_family], list(families))

def test_label_escaping(self):
for escaped_val, unescaped_val in [
Expand All @@ -231,7 +238,7 @@ def test_label_escaping(self):
metric_family = CounterMetricFamily(
"a", "help", labels=["foo", "bar"])
metric_family.add_metric([unescaped_val, "baz"], 1)
self.assertEqual([metric_family], list(families))
self.assertEqualMetrics([metric_family], list(families))

def test_help_escaping(self):
for escaped_val, unescaped_val in [
Expand All @@ -253,7 +260,7 @@ def test_help_escaping(self):
""" % escaped_val))
metric_family = CounterMetricFamily("a", unescaped_val, labels=["foo"])
metric_family.add_metric(["bar"], 1)
self.assertEqual([metric_family], list(families))
self.assertEqualMetrics([metric_family], list(families))

def test_escaping(self):
families = text_string_to_metric_families("""# TYPE a counter
Expand All @@ -264,7 +271,7 @@ def test_escaping(self):
metric_family = CounterMetricFamily("a", "he\n\\l\\tp", labels=["foo"])
metric_family.add_metric(["b\"a\nr"], 1)
metric_family.add_metric(["b\\a\\z"], 2)
self.assertEqual([metric_family], list(families))
self.assertEqualMetrics([metric_family], list(families))

def test_timestamps_discarded(self):
families = text_string_to_metric_families("""# TYPE a counter
Expand All @@ -277,7 +284,7 @@ def test_timestamps_discarded(self):
a = CounterMetricFamily("a", "help", labels=["foo"])
a.add_metric(["bar"], 1)
b = CounterMetricFamily("b", "help", value=2)
self.assertEqual([a, b], list(families))
self.assertEqualMetrics([a, b], list(families))

@unittest.skipIf(sys.version_info < (2, 7), "Test requires Python 2.7+.")
def test_roundtrip(self):
Expand Down

0 comments on commit e180e1d

Please sign in to comment.