Skip to content

Commit dc50259

Browse files
committed
test: validate JMX Exporter compatibility
Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
1 parent 3fc7b7b commit dc50259

5 files changed

Lines changed: 212 additions & 0 deletions

File tree

.github/renovate-tracked-deps.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
"mise"
3737
]
3838
},
39+
".github/workflows/jmx-exporter-compatibility.yml": {
40+
"regex": [
41+
"mise"
42+
]
43+
},
3944
".github/workflows/lint.yml": {
4045
"regex": [
4146
"mise"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
name: JMX Exporter Compatibility
3+
4+
on:
5+
pull_request:
6+
workflow_dispatch:
7+
inputs:
8+
repository:
9+
description: JMX Exporter repository to test, in owner/name form
10+
required: false
11+
default: prometheus/jmx_exporter
12+
ref:
13+
description: JMX Exporter branch, tag, or commit to test
14+
required: false
15+
default: main
16+
17+
permissions: {}
18+
19+
jobs:
20+
jmx-exporter-compatibility:
21+
runs-on: ubuntu-24.04
22+
steps:
23+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
24+
with:
25+
persist-credentials: false
26+
- uses: jdx/mise-action@1648a7812b9aeae629881980618f079932869151 # v4.0.1
27+
with:
28+
version: v2026.5.18
29+
sha256: cfac593469d028d7ae5fe36e37bd7c59118b5238e92d8a876209578464f24a84
30+
- name: Cache local Maven repository
31+
uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
32+
with:
33+
path: ~/.m2/repository
34+
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
35+
- name: Run JMX Exporter compatibility tests
36+
env:
37+
JMX_EXPORTER_REPOSITORY: ${{ inputs.repository || 'prometheus/jmx_exporter' }}
38+
JMX_EXPORTER_REF: ${{ inputs.ref || 'main' }}
39+
run: mise run jmx-exporter:test

.mise/lib/jmx_exporter_compat.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/usr/bin/env python3
2+
3+
from __future__ import annotations
4+
5+
import os
6+
import subprocess
7+
import xml.etree.ElementTree as ET
8+
from pathlib import Path
9+
from typing import Optional
10+
11+
12+
DEFAULT_JMX_EXPORTER_DIR = Path(
13+
os.environ.get("JMX_EXPORTER_DIR", "/tmp/jmx-exporter-compat")
14+
)
15+
DEFAULT_JMX_EXPORTER_REPOSITORY = os.environ.get(
16+
"JMX_EXPORTER_REPOSITORY", "prometheus/jmx_exporter"
17+
)
18+
DEFAULT_JMX_EXPORTER_REMOTE = os.environ.get("JMX_EXPORTER_REMOTE", "origin")
19+
DEFAULT_JMX_EXPORTER_REF = os.environ.get("JMX_EXPORTER_REF", "main")
20+
DEFAULT_MAVEN_MODULES = os.environ.get(
21+
"JMX_EXPORTER_MAVEN_MODULES",
22+
"jmx_prometheus_common,jmx_prometheus_javaagent,jmx_prometheus_standalone",
23+
)
24+
DEFAULT_PROM_VERSION = os.environ.get("PROM_VERSION")
25+
26+
27+
def run_cmd(cmd: list[str], cwd: Optional[Path] = None) -> None:
28+
subprocess.run(cmd, cwd=cwd, check=True)
29+
30+
31+
def jmx_exporter_repository_url(repository: str) -> str:
32+
return f"https://github.com/{repository}.git"
33+
34+
35+
def check_clean_worktree(jmx_exporter_dir: Path) -> None:
36+
result = subprocess.run(
37+
["git", "status", "--short"],
38+
cwd=jmx_exporter_dir,
39+
check=True,
40+
capture_output=True,
41+
text=True,
42+
)
43+
if result.stdout.strip():
44+
raise RuntimeError(
45+
f"{jmx_exporter_dir} has uncommitted changes; use a clean clone or set "
46+
"JMX_EXPORTER_DIR"
47+
)
48+
49+
50+
def get_prom_version(root_dir: Path = Path.cwd()) -> str:
51+
configured_version = DEFAULT_PROM_VERSION
52+
if configured_version:
53+
return configured_version
54+
pom = ET.parse(root_dir / "pom.xml")
55+
root = pom.getroot()
56+
version = root.findtext("./{*}version")
57+
if not version:
58+
version = root.findtext("./{*}parent/{*}version")
59+
if not version:
60+
raise RuntimeError("could not determine Prometheus version from pom.xml")
61+
return version
62+
63+
64+
def prepare_repo(
65+
jmx_exporter_dir: Path = DEFAULT_JMX_EXPORTER_DIR,
66+
repository: str = DEFAULT_JMX_EXPORTER_REPOSITORY,
67+
remote: str = DEFAULT_JMX_EXPORTER_REMOTE,
68+
ref: str = DEFAULT_JMX_EXPORTER_REF,
69+
) -> None:
70+
repository_url = jmx_exporter_repository_url(repository)
71+
if (jmx_exporter_dir / ".git").is_dir():
72+
check_clean_worktree(jmx_exporter_dir)
73+
run_cmd(
74+
["git", "remote", "set-url", remote, repository_url],
75+
cwd=jmx_exporter_dir,
76+
)
77+
run_cmd(["git", "fetch", remote, ref], cwd=jmx_exporter_dir)
78+
else:
79+
run_cmd(["git", "clone", repository_url, str(jmx_exporter_dir)])
80+
run_cmd(["git", "fetch", remote, ref], cwd=jmx_exporter_dir)
81+
run_cmd(
82+
["git", "checkout", "-B", "jmx-exporter-compat", "FETCH_HEAD"],
83+
cwd=jmx_exporter_dir,
84+
)
85+
86+
87+
def install_local_artifacts(root_dir: Path = Path.cwd()) -> None:
88+
run_cmd(
89+
[
90+
"./mvnw",
91+
"install",
92+
"-DskipTests",
93+
"-Dcoverage.skip=true",
94+
"-Dcheckstyle.skip=true",
95+
"-Dwarnings=-nowarn",
96+
],
97+
cwd=root_dir,
98+
)
99+
100+
101+
def run_maven_test(
102+
test_selector: Optional[str] = None,
103+
jmx_exporter_dir: Path = DEFAULT_JMX_EXPORTER_DIR,
104+
maven_modules: str = DEFAULT_MAVEN_MODULES,
105+
prom_version: Optional[str] = None,
106+
) -> None:
107+
if prom_version is None:
108+
prom_version = get_prom_version()
109+
cmd = [
110+
"./mvnw",
111+
"-B",
112+
"-pl",
113+
maven_modules,
114+
"-am",
115+
f"-Dprometheus.metrics.version={prom_version}",
116+
"-Djacoco.skip=true",
117+
]
118+
if test_selector:
119+
cmd.append(f"-Dtest={test_selector}")
120+
cmd.append("test")
121+
run_cmd(cmd, cwd=jmx_exporter_dir)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python3
2+
3+
# [MISE] description="Install local artifacts and check out a target JMX Exporter ref"
4+
# [MISE] alias="jmx-exporter:prepare"
5+
6+
import sys
7+
8+
9+
sys.path.insert(0, ".mise/lib")
10+
11+
12+
def main() -> int:
13+
from jmx_exporter_compat import install_local_artifacts, prepare_repo
14+
15+
install_local_artifacts()
16+
prepare_repo()
17+
return 0
18+
19+
20+
if __name__ == "__main__":
21+
raise SystemExit(main())

.mise/tasks/jmx-exporter/test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python3
2+
3+
# [MISE] description="Run JMX Exporter tests against a target ref"
4+
# [MISE] alias="jmx-exporter:test"
5+
6+
import sys
7+
8+
9+
sys.path.insert(0, ".mise/lib")
10+
11+
12+
def main() -> int:
13+
from jmx_exporter_compat import (
14+
install_local_artifacts,
15+
prepare_repo,
16+
run_maven_test,
17+
)
18+
19+
install_local_artifacts()
20+
prepare_repo()
21+
run_maven_test()
22+
return 0
23+
24+
25+
if __name__ == "__main__":
26+
raise SystemExit(main())

0 commit comments

Comments
 (0)