1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
"""Empirical contract for Stream's real process metrics endpoint."""
from __future__ import annotations
import math
import os
import re
import time
import urllib.request
BASE = os.environ.get("STREAM_BASE_URL", "http://127.0.0.1:6008")
TYPE_RE = re.compile(r"^# TYPE (\S+) (\S+)$")
SAMPLE_RE = re.compile(r"^(\S+?)(?:\{[^}]*\})?\s+([^\s]+)$")
STORE_COUNT_RE = re.compile(
r'^jetstream_store_op_duration_seconds_count\{op="([^"]+)",status="([^"]+)"\}\s+([^\s]+)$',
re.MULTILINE,
)
STORE_BUCKET_RE = re.compile(
r'^jetstream_store_op_duration_seconds_bucket\{op="([^"]+)",status="([^"]+)",le="([^"]+)"\}\s+([^\s]+)$',
re.MULTILINE,
)
EXPECTED_TYPES = {
"process_start_time_seconds": "gauge",
"process_cpu_seconds_total": "counter",
"process_resident_memory_bytes": "gauge",
"process_virtual_memory_bytes": "gauge",
"stream_process_threads": "gauge",
"process_open_fds": "gauge",
"process_max_fds": "gauge",
"process_minor_page_faults_total": "counter",
"process_major_page_faults_total": "counter",
"process_voluntary_context_switches_total": "counter",
"process_involuntary_context_switches_total": "counter",
}
COUNTERS = {name for name, kind in EXPECTED_TYPES.items() if kind == "counter"}
def scrape() -> tuple[str, dict[str, str], dict[str, float]]:
with urllib.request.urlopen(BASE + "/metrics", timeout=5) as response:
assert response.status == 200
raw = response.read().decode()
types: dict[str, str] = {}
samples: dict[str, float] = {}
for line in raw.splitlines():
if match := TYPE_RE.match(line):
types[match.group(1)] = match.group(2)
elif not line.startswith("#") and (match := SAMPLE_RE.match(line)):
samples[match.group(1)] = float(match.group(2))
return raw, types, samples
def wait_past_startup_gate() -> None:
"""listener-first startup: /healthz and /metrics answer from bind time,
before the storage reads this contract counts (the fresh-dir phase read
is the guaranteed get-notfound). /status leaves "starting" only after
storage is published."""
deadline = time.monotonic() + 15
while time.monotonic() < deadline:
try:
with urllib.request.urlopen(BASE + "/status", timeout=1) as response:
if b"state starting" not in response.read():
return
except OSError:
pass
time.sleep(0.05)
raise AssertionError("startup gate never opened")
def main() -> None:
wait_past_startup_gate()
first_raw, first_types, first = scrape()
assert not re.search(r"(?m)^go_", first_raw), "Stream emitted synthetic Go metrics"
for name, kind in EXPECTED_TYPES.items():
assert first_types.get(name) == kind, (name, first_types.get(name), kind)
assert name in first and math.isfinite(first[name]) and first[name] >= 0, (name, first.get(name))
assert first_types.get("jetstream_store_op_duration_seconds") == "histogram"
store_counts = {
(op, status): float(value)
for op, status, value in STORE_COUNT_RE.findall(first_raw)
}
expected_store_series = {
("get", "ok"),
("get", "notfound"),
("get", "error"),
("set", "ok"),
("set", "error"),
("delete", "ok"),
("delete", "error"),
("batch_commit", "ok"),
("batch_commit", "error"),
}
assert set(store_counts) == expected_store_series, store_counts
assert store_counts[("get", "notfound")] > 0
assert store_counts[("set", "ok")] > 0
assert store_counts[("batch_commit", "ok")] > 0
buckets: dict[tuple[str, str], list[tuple[str, float]]] = {}
for op, status, upper, value in STORE_BUCKET_RE.findall(first_raw):
buckets.setdefault((op, status), []).append((upper, float(value)))
assert set(buckets) == expected_store_series
for labels, observations in buckets.items():
assert len(observations) == 16, (labels, observations)
assert observations[-1] == ("+Inf", store_counts[labels]), (labels, observations[-1])
values = [value for _, value in observations]
assert values == sorted(values), (labels, values)
assert 0 < first["process_start_time_seconds"] <= time.time()
assert 0 < first["process_resident_memory_bytes"] <= first["process_virtual_memory_bytes"]
assert first["stream_process_threads"] >= 1
assert 0 < first["process_open_fds"] <= first["process_max_fds"]
# Exercise the handler between snapshots, then prove cumulative process
# counters never moved backwards and identity stayed fixed.
for _ in range(8):
urllib.request.urlopen(BASE + "/healthz", timeout=5).read()
_, second_types, second = scrape()
assert second_types == first_types
assert second["process_start_time_seconds"] == first["process_start_time_seconds"]
for name in COUNTERS:
assert second[name] >= first[name], (name, first[name], second[name])
print(
"process metrics PASS: "
f"rss={int(second['process_resident_memory_bytes'])} "
f"vm={int(second['process_virtual_memory_bytes'])} "
f"threads={int(second['stream_process_threads'])} "
f"fds={int(second['process_open_fds'])}/{int(second['process_max_fds'])} "
f"store=get-notfound:{int(store_counts[('get', 'notfound')])},"
f"set:{int(store_counts[('set', 'ok')])},"
f"batch:{int(store_counts[('batch_commit', 'ok')])}"
)
if __name__ == "__main__":
main()