Skip to content

Commit

Permalink
Preserve integer formatting in openmetrics text
Browse files Browse the repository at this point in the history
Previously, integers would be formatted the same as floats.

Openmetrics distinguishes floats and integers.

Signed-off-by: Mark Hansen <[email protected]>
  • Loading branch information
mhansen committed Mar 21, 2020
1 parent 0497442 commit 6984200
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
13 changes: 10 additions & 3 deletions prometheus_client/openmetrics/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"""Content type of the latest OpenMetrics text format"""


def _to_openmetrics_value(value):
# Openmetrics distinguishes integers and floats with different text representations.
if type(value) == int:
return str(value)
return floatToGoString(value)


def generate_latest(registry):
'''Returns the metrics from the registry in latest text format as a string.'''
output = []
Expand Down Expand Up @@ -37,13 +44,13 @@ def generate_latest(registry):
if s.exemplar.timestamp is not None:
exemplarstr = ' # {0} {1} {2}'.format(
labels,
floatToGoString(s.exemplar.value),
_to_openmetrics_value(s.exemplar.value),
s.exemplar.timestamp,
)
else:
exemplarstr = ' # {0} {1}'.format(
labels,
floatToGoString(s.exemplar.value),
_to_openmetrics_value(s.exemplar.value),
)
else:
exemplarstr = ''
Expand All @@ -53,7 +60,7 @@ def generate_latest(registry):
output.append('{0}{1} {2}{3}{4}\n'.format(
s.name,
labelstr,
floatToGoString(s.value),
_to_openmetrics_value(s.value),
timestamp,
exemplarstr,
))
Expand Down
42 changes: 21 additions & 21 deletions tests/openmetrics/test_exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ def collect(self):
self.registry.register(MyCollector())
self.assertEqual(b"""# HELP hh help
# TYPE hh histogram
hh_bucket{le="1"} 0.0 # {a="b"} 0.5
hh_bucket{le="2"} 0.0 # {le="7"} 0.5 12
hh_bucket{le="3"} 0.0 123 # {a="b"} 2.5 12
hh_bucket{le="4"} 0.0 # {a="\\n\\"\\\\"} 3.5
hh_bucket{le="+Inf"} 0.0
hh_bucket{le="1"} 0 # {a="b"} 0.5
hh_bucket{le="2"} 0 # {le="7"} 0.5 12
hh_bucket{le="3"} 0 123 # {a="b"} 2.5 12
hh_bucket{le="4"} 0 # {a="\\n\\"\\\\"} 3.5
hh_bucket{le="+Inf"} 0
# EOF
""", generate_latest(self.registry))

Expand Down Expand Up @@ -159,10 +159,10 @@ def test_gaugehistogram(self):
GaugeHistogramMetricFamily('gh', 'help', buckets=[('1.0', 4), ('+Inf', (5))], gsum_value=7))
self.assertEqual(b"""# HELP gh help
# TYPE gh gaugehistogram
gh_bucket{le="1.0"} 4.0
gh_bucket{le="+Inf"} 5.0
gh_gcount 5.0
gh_gsum 7.0
gh_bucket{le="1.0"} 4
gh_bucket{le="+Inf"} 5
gh_gcount 5
gh_gsum 7
# EOF
""", generate_latest(self.registry))

Expand All @@ -171,10 +171,10 @@ def test_gaugehistogram_negative_buckets(self):
GaugeHistogramMetricFamily('gh', 'help', buckets=[('-1.0', 4), ('+Inf', (5))], gsum_value=-7))
self.assertEqual(b"""# HELP gh help
# TYPE gh gaugehistogram
gh_bucket{le="-1.0"} 4.0
gh_bucket{le="+Inf"} 5.0
gh_gcount 5.0
gh_gsum -7.0
gh_bucket{le="-1.0"} 4
gh_bucket{le="+Inf"} 5
gh_gcount 5
gh_gsum -7
# EOF
""", generate_latest(self.registry))

Expand All @@ -192,8 +192,8 @@ def test_enum(self):
i.labels('c', 'd').state('bar')
self.assertEqual(b"""# HELP ee An enum
# TYPE ee stateset
ee{a="c",b="d",ee="foo"} 0.0
ee{a="c",b="d",ee="bar"} 1.0
ee{a="c",b="d",ee="foo"} 0
ee{a="c",b="d",ee="bar"} 1
# EOF
""", generate_latest(self.registry))

Expand Down Expand Up @@ -250,12 +250,12 @@ def collect(self):
self.registry.register(MyCollector())
self.assertEqual(b"""# HELP ts help
# TYPE ts unknown
ts{foo="a"} 0.0 123.456
ts{foo="b"} 0.0 -123.456
ts{foo="c"} 0.0 123
ts{foo="d"} 0.0 123.456000000
ts{foo="e"} 0.0 123.000456000
ts{foo="f"} 0.0 123.000000456
ts{foo="a"} 0 123.456
ts{foo="b"} 0 -123.456
ts{foo="c"} 0 123
ts{foo="d"} 0 123.456000000
ts{foo="e"} 0 123.000456000
ts{foo="f"} 0 123.000000456
# EOF
""", generate_latest(self.registry))

Expand Down

0 comments on commit 6984200

Please sign in to comment.