mirror of
https://github.com/chatmail/relay.git
synced 2026-11-26 23:09:35 +00:00
feat(expire): export deployed relay commit as chatmail_version_info
Add chatmail_version_info to CHATMAIL_EXPIRE_PROMPATH: Read hash and dirty state from /etc/chatmail-version and report as label values; by convention metric value is always 1 to allow joins.
This commit is contained in:
@@ -125,6 +125,21 @@ def print_info(msg):
|
||||
print(msg, file=sys.stderr)
|
||||
|
||||
|
||||
CHATMAIL_VERSION_PATH = "/etc/chatmail-version"
|
||||
|
||||
|
||||
def read_chatmail_version(path):
|
||||
"""Return (commit, dirty) from the version file that cmdeploy writes."""
|
||||
try:
|
||||
lines = Path(path).read_text().splitlines()
|
||||
except OSError:
|
||||
return "unknown", "false"
|
||||
commit = lines[0] if lines else "unknown"
|
||||
# Lines after the commit hold the `git diff` of the deploying checkout.
|
||||
dirty = "true" if len(lines) > 1 else "false"
|
||||
return commit, dirty
|
||||
|
||||
|
||||
PROM_HELP = {
|
||||
"storage_bytes": "Mailbox storage in bytes.",
|
||||
"messages_bytes": "Sum of msg bytes >= threshold.",
|
||||
@@ -136,6 +151,7 @@ PROM_HELP = {
|
||||
"expire_removed_files": "Files removed by the last run.",
|
||||
"expire_duration_seconds": "Duration of the last run.",
|
||||
"expire_last_run_timestamp_seconds": "End of the last run.",
|
||||
"version_info": "Deployed relay commit and whether it had local changes.",
|
||||
}
|
||||
|
||||
|
||||
@@ -298,6 +314,12 @@ class Expiry:
|
||||
gauge("expire_removed_files", self.del_files)
|
||||
gauge("expire_duration_seconds", f"{end - self.start:.2f}")
|
||||
gauge("expire_last_run_timestamp_seconds", f"{end:.0f}")
|
||||
commit, dirty = read_chatmail_version(CHATMAIL_VERSION_PATH)
|
||||
print(f"# HELP chatmail_version_info {help.pop('version_info')}", file=f)
|
||||
print("# TYPE chatmail_version_info gauge", file=f)
|
||||
print(
|
||||
f'chatmail_version_info{{commit="{commit}",dirty="{dirty}"}} 1', file=f
|
||||
)
|
||||
assert not help, f"unused PROM_HELP entries: {list(help)}"
|
||||
os.replace(tmp, path)
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ from chatmaild.expire import (
|
||||
os_listdir_if_exists,
|
||||
parse_dovecot_filename,
|
||||
quota_expire_main,
|
||||
read_chatmail_version,
|
||||
scan_mailbox_messages,
|
||||
)
|
||||
from chatmaild.expire import daily_expire_main as expiry_main
|
||||
@@ -134,6 +135,9 @@ def test_expiry_cli_prom(example_config, mbox1, tmp_path, monkeypatch):
|
||||
create_new_messages(mbox1.basedir, ["cur/msg_4days"], size=100, days=4)
|
||||
path = tmp_path / "chatmail-expire.prom"
|
||||
monkeypatch.setenv("CHATMAIL_EXPIRE_PROMPATH", str(path))
|
||||
version = tmp_path / "chatmail-version"
|
||||
version.write_text("a" * 40 + "\n")
|
||||
monkeypatch.setattr("chatmaild.expire.CHATMAIL_VERSION_PATH", str(version))
|
||||
expiry_main([str(example_config._inipath), "--remove"])
|
||||
text = path.read_text()
|
||||
# fill_mbox 500 + 600 bytes and msg_4days remain after msg_old expired
|
||||
@@ -145,6 +149,23 @@ def test_expiry_cli_prom(example_config, mbox1, tmp_path, monkeypatch):
|
||||
# msg_old and maildirsize
|
||||
assert "chatmail_expire_removed_files 2\n" in text
|
||||
assert "chatmail_expire_last_run_timestamp_seconds " in text
|
||||
assert f'chatmail_version_info{{commit="{"a" * 40}",dirty="false"}} 1\n' in text
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("content", "expected"),
|
||||
[
|
||||
(None, ("unknown", "false")),
|
||||
("", ("unknown", "false")),
|
||||
("abc123\n", ("abc123", "false")),
|
||||
("abc123\ndiff --git a/x b/x\n", ("abc123", "true")),
|
||||
],
|
||||
)
|
||||
def test_read_chatmail_version(tmp_path, content, expected):
|
||||
path = tmp_path / "chatmail-version"
|
||||
if content is not None:
|
||||
path.write_text(content)
|
||||
assert read_chatmail_version(path) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize("dry", [True, False])
|
||||
|
||||
Reference in New Issue
Block a user