-
Notifications
You must be signed in to change notification settings - Fork 8
otto/prometheus: add --format prometheus to checkup output #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
JoshuaGabriel
wants to merge
1
commit into
clyso:main
Choose a base branch
from
JoshuaGabriel:wip-prometheus-output
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # Copyright (C) 2025 Clyso | ||
| # SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
|
||
| import json | ||
|
|
||
| # Check result -> numeric gauge value, ordered by severity | ||
| # (PASS < WARN < UNKNOWN < FAIL) so higher values mean "worse" and dashboards | ||
| # can alert on a threshold. | ||
| STATUS_VALUE = {"PASS": 0, "WARN": 1, "UNKNOWN": 2, "FAIL": 3} | ||
|
|
||
|
|
||
| def _escape_label(value: str) -> str: | ||
| """Escape a Prometheus label value (backslash, double-quote, newline).""" | ||
| return value.replace("\\", "\\\\").replace('"', '\\"').replace("\n", "\\n") | ||
|
|
||
|
|
||
| def render_prometheus(result: str) -> str: | ||
| """Render a checkup result JSON string as Prometheus text exposition.""" | ||
| data = json.loads(result) | ||
| summary = data["summary"] | ||
| sections = data["sections"] | ||
|
|
||
| lines: list[str] = [] | ||
|
|
||
| lines.append("# HELP otto_checkup_score Overall checkup score") | ||
| lines.append("# TYPE otto_checkup_score gauge") | ||
| lines.append(f"otto_checkup_score {summary['score']}") | ||
|
|
||
| lines.append("# HELP otto_checkup_max_score Maximum possible checkup score") | ||
| lines.append("# TYPE otto_checkup_max_score gauge") | ||
| lines.append(f"otto_checkup_max_score {summary['max_score']}") | ||
|
|
||
| lines.append("# HELP otto_checkup_section_score Per-section checkup score") | ||
| lines.append("# TYPE otto_checkup_section_score gauge") | ||
| for section in sections: | ||
| label = _escape_label(section["id"]) | ||
| lines.append( | ||
| f'otto_checkup_section_score{{section="{label}"}} {section["score"]}' | ||
| ) | ||
|
|
||
| lines.append( | ||
| "# HELP otto_checkup_check_status " | ||
| "Check status (0=PASS, 1=WARN, 2=UNKNOWN, 3=FAIL)" | ||
| ) | ||
| lines.append("# TYPE otto_checkup_check_status gauge") | ||
| for section in sections: | ||
| section_label = _escape_label(section["id"]) | ||
| for check in section["checks"]: | ||
| check_label = _escape_label(check["id"]) | ||
| value = STATUS_VALUE[check["result"]] | ||
| lines.append( | ||
| f'otto_checkup_check_status{{section="{section_label}",' | ||
| f'check="{check_label}"}} {value}' | ||
| ) | ||
|
|
||
| return "\n".join(lines) + "\n" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| # Copyright (C) 2025 Clyso | ||
| # SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
|
||
| import json | ||
| import re | ||
| import subprocess | ||
| import unittest | ||
|
|
||
| from clyso.ceph.otto.prometheus import STATUS_VALUE, render_prometheus | ||
|
|
||
| # Every non-comment line must be `name`, optional `{labels}`, a space, a number. | ||
| _LINE_RE = re.compile(r"^[a-z_]+(\{[^}]*\})? -?[0-9.]+$") | ||
|
|
||
|
|
||
| class RenderPrometheusTest(unittest.TestCase): | ||
| def setUp(self): | ||
| with open("tests/otto.json") as f: | ||
| self.result = json.load(f) | ||
| self.out = render_prometheus(json.dumps(self.result)) | ||
|
|
||
| def test_metric_families_and_completeness(self): | ||
| for type_line in ( | ||
| "# TYPE otto_checkup_score gauge", | ||
| "# TYPE otto_checkup_max_score gauge", | ||
| "# TYPE otto_checkup_section_score gauge", | ||
| "# TYPE otto_checkup_check_status gauge", | ||
| ): | ||
| self.assertIn(type_line, self.out) | ||
|
|
||
| # One check_status line per check in the input. | ||
| n_checks = sum(len(s["checks"]) for s in self.result["sections"]) | ||
| n_status_lines = sum( | ||
| 1 | ||
| for line in self.out.splitlines() | ||
| if line.startswith("otto_checkup_check_status{") | ||
| ) | ||
| self.assertEqual(n_status_lines, n_checks) | ||
|
|
||
| # Spot-check two status values: Cluster/Health is WARN (1); | ||
| # Version/Check for Known Issues is FAIL (3). | ||
| self.assertIn( | ||
| 'otto_checkup_check_status{section="Cluster",check="Health"} ' | ||
| f"{STATUS_VALUE['WARN']}", | ||
| self.out, | ||
| ) | ||
| self.assertIn( | ||
| 'otto_checkup_check_status{section="Version",' | ||
| 'check="Check for Known Issues in Running Version"} ' | ||
| f"{STATUS_VALUE['FAIL']}", | ||
| self.out, | ||
| ) | ||
|
|
||
| def test_label_escaping(self): | ||
| result = { | ||
| "summary": {"score": 0.0, "max_score": 1, "grade": "F"}, | ||
| "sections": [ | ||
| { | ||
| "id": 'Quo"te\\back', | ||
| "score": 0.0, | ||
| "max_score": 1, | ||
| "grade": "F", | ||
| "checks": [{"id": 'a"b\\c', "result": "FAIL"}], | ||
| } | ||
| ], | ||
| } | ||
| out = render_prometheus(json.dumps(result)) | ||
| self.assertIn(r'section="Quo\"te\\back"', out) | ||
| self.assertIn(r'check="a\"b\\c"', out) | ||
|
|
||
| def test_exposition_format_shape(self): | ||
| self.assertTrue(self.out.endswith("\n")) | ||
| for line in self.out.splitlines(): | ||
| if line.startswith("#") or not line: | ||
| continue | ||
| self.assertRegex(line, _LINE_RE) | ||
|
|
||
|
|
||
| class CliPrometheusTest(unittest.TestCase): | ||
| def test_cli_format_prometheus(self): | ||
| process = subprocess.Popen( | ||
| [ # noqa: S607 | ||
| "otto", | ||
| "checkup", | ||
| "--ceph_report_json=tests/report.pacific.json", | ||
| "--format=prometheus", | ||
| ], | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, | ||
| ) | ||
| stdout_output, _ = process.communicate() | ||
| self.assertEqual(process.returncode, 0) | ||
| first_line = stdout_output.decode().splitlines()[0] | ||
| self.assertEqual(first_line, "# HELP otto_checkup_score Overall checkup score") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/cluster/
also maybe should give the relevant cephadm path ?