Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 98be272

Browse files
committed
Fix tests and exposition
Two things: * Changing to a GaugeHistogram, and adding help output, mildly changed the output from the metrics, so we need to update the tests * Our hacked-up `exposition.py` was stripping out some samples it shouldn't have been. Put them back in, to more closely match the upstream `exposition.py`.
1 parent bff2a6f commit 98be272

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

synapse/metrics/_exposition.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,33 @@ def generate_latest(registry, emit_help=False):
124124
)
125125
)
126126
output.append("# TYPE {0} {1}\n".format(mname, mtype))
127-
for sample in metric.samples:
128-
# Get rid of the OpenMetrics specific samples
127+
128+
om_samples = {}
129+
for s in metric.samples:
129130
for suffix in ["_created", "_gsum", "_gcount"]:
130-
if sample.name.endswith(suffix):
131+
if s.name == metric.name + suffix:
132+
# OpenMetrics specific sample, put in a gauge at the end.
133+
# (these come from gaugehistograms which don't get renamed,
134+
# so no need to faff with mnewname)
135+
om_samples.setdefault(suffix, []).append(sample_line(s, s.name))
131136
break
132137
else:
133-
newname = sample.name.replace(mnewname, mname)
138+
newname = s.name.replace(mnewname, mname)
134139
if ":" in newname and newname.endswith("_total"):
135140
newname = newname[: -len("_total")]
136-
output.append(sample_line(sample, newname))
141+
output.append(sample_line(s, newname))
142+
143+
for suffix, lines in sorted(om_samples.items()):
144+
if emit_help:
145+
output.append(
146+
"# HELP {0}{1} {2}\n".format(
147+
metric.name,
148+
suffix,
149+
metric.documentation.replace("\\", r"\\").replace("\n", r"\n"),
150+
)
151+
)
152+
output.append("# TYPE {0}{1} gauge\n".format(metric.name, suffix))
153+
output.extend(lines)
137154

138155
# Get rid of the weird colon things while we're at it
139156
if mtype == "counter":
@@ -152,16 +169,16 @@ def generate_latest(registry, emit_help=False):
152169
)
153170
)
154171
output.append("# TYPE {0} {1}\n".format(mnewname, mtype))
155-
for sample in metric.samples:
156-
# Get rid of the OpenMetrics specific samples
172+
173+
for s in metric.samples:
174+
# Get rid of the OpenMetrics specific samples (we should already have
175+
# dealt with them above anyway.)
157176
for suffix in ["_created", "_gsum", "_gcount"]:
158-
if sample.name.endswith(suffix):
177+
if s.name == metric.name + suffix:
159178
break
160179
else:
161180
output.append(
162-
sample_line(
163-
sample, sample.name.replace(":total", "").replace(":", "_")
164-
)
181+
sample_line(s, s.name.replace(":total", "").replace(":", "_"))
165182
)
166183

167184
return "".join(output).encode("utf-8")

tests/storage/test_event_metrics.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ def test_exposed_to_prometheus(self):
5252
self.reactor.advance(60 * 60 * 1000)
5353
self.pump(1)
5454

55-
items = set(
55+
items = list(
5656
filter(
5757
lambda x: b"synapse_forward_extremities_" in x,
58-
generate_latest(REGISTRY).split(b"\n"),
58+
generate_latest(REGISTRY, emit_help=False).split(b"\n"),
5959
)
6060
)
6161

62-
expected = {
62+
expected = [
6363
b'synapse_forward_extremities_bucket{le="1.0"} 0.0',
6464
b'synapse_forward_extremities_bucket{le="2.0"} 2.0',
6565
b'synapse_forward_extremities_bucket{le="3.0"} 2.0',
@@ -72,9 +72,12 @@ def test_exposed_to_prometheus(self):
7272
b'synapse_forward_extremities_bucket{le="100.0"} 3.0',
7373
b'synapse_forward_extremities_bucket{le="200.0"} 3.0',
7474
b'synapse_forward_extremities_bucket{le="500.0"} 3.0',
75-
b'synapse_forward_extremities_bucket{le="+Inf"} 3.0',
76-
b"synapse_forward_extremities_count 3.0",
77-
b"synapse_forward_extremities_sum 10.0",
78-
}
79-
75+
# per https://docs.google.com/document/d/1KwV0mAXwwbvvifBvDKH_LU1YjyXE_wxCkHNoCGq1GX0/edit#heading=h.wghdjzzh72j9,
76+
# "inf" is valid: "this includes variants such as inf"
77+
b'synapse_forward_extremities_bucket{le="inf"} 3.0',
78+
b"# TYPE synapse_forward_extremities_gcount gauge",
79+
b"synapse_forward_extremities_gcount 3.0",
80+
b"# TYPE synapse_forward_extremities_gsum gauge",
81+
b"synapse_forward_extremities_gsum 10.0",
82+
]
8083
self.assertEqual(items, expected)

0 commit comments

Comments
 (0)