Skip to content

Commit 68abcc3

Browse files
committed
Expose success metric
1 parent 4cf69b6 commit 68abcc3

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

prometheus_script_wrapper/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ def main(prefix: str, output: Path, labels: List[Tuple[str, str]], script: List[
8282
exit_code_metric.add_sample(exit_code_metric.name, value=exit_code, labels=labels)
8383
registry.add(exit_code_metric)
8484

85+
success_metric = Metric(name=f"{prefix}_success", documentation="Whether the wrapped script succeeded.", typ="gauge")
86+
success_metric.add_sample(success_metric.name, value=int(exit_code == 0), labels=labels)
87+
registry.add(success_metric)
88+
8589
runtime_metric = Metric(name=f"{prefix}_runtime_seconds", documentation="Runtime duration of the script in seconds", typ="gauge")
8690
runtime_metric.add_sample(runtime_metric.name, value=runtime, labels=labels)
8791
registry.add(runtime_metric)

tests/test_output.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def test_success(tmp_path):
1919
with (tmp_path / "out.prom").open("r") as f:
2020
content = f.read()
2121
assert "test_exit_code 0" in content
22+
assert "test_success 1" in content
2223
assert "test_runtime_seconds" in content
2324
assert "test_success_timestamp_seconds" in content
2425

@@ -35,6 +36,7 @@ def test_failure(tmp_path):
3536
with (tmp_path / "out.prom").open("r") as f:
3637
content = f.read()
3738
assert "test_exit_code 1" in content
39+
assert "test_success 0" in content
3840
assert "test_runtime_seconds" in content
3941
assert "test_success_timestamp_seconds" not in content # Should not be present for failures
4042

@@ -60,6 +62,7 @@ def test_exit_code(tmp_path):
6062
with (tmp_path / "out.prom").open("r") as f:
6163
content = f.read()
6264
assert "test_exit_code 42" in content
65+
assert "test_success 0" in content
6366

6467

6568
def test_custom_metrics(tmp_path):
@@ -89,7 +92,6 @@ def test_custom_metrics(tmp_path):
8992
assert os.path.exists(tmp_path / "out.prom")
9093
with (tmp_path / "out.prom").open("r") as f:
9194
content = f.read()
92-
assert "test_exit_code" in content
9395
assert "test_runtime_seconds" in content
9496
assert "test_script_test_count_total 42" in content
9597
assert 'test_script_test_gauge{mylabel="test"} 23' in content
@@ -140,6 +142,7 @@ def test_extra_labels(tmp_path):
140142
with (tmp_path / "out.prom").open("r") as f:
141143
content = f.read()
142144
assert 'test_exit_code{x="one",y="two"}' in content
145+
assert 'test_success{x="one",y="two"}' in content
143146
assert 'test_runtime_seconds{x="one",y="two"}' in content
144147
assert 'test_script_test_count_total{x="one",y="two"} 42' in content
145148
assert 'test_script_test_gauge{mylabel="test",x="one",y="two"} 23' in content
@@ -170,6 +173,4 @@ def test_argument_passing(tmp_path):
170173
assert os.path.exists(tmp_path / "out.prom")
171174
with (tmp_path / "out.prom").open("r") as f:
172175
content = f.read()
173-
assert "test_exit_code" in content
174-
assert "test_runtime_seconds" in content
175176
assert 'test_script_test_count_total{test="--extra"} 1337' in content

0 commit comments

Comments
 (0)