Skip to content

Commit caf8a1b

Browse files
committed
chore(ci): add action to test abnf syntax and examples in OM2.0 spec
Action item from OpenMetrics 2.0 WG. Signed-off-by: György Krajcsovits <[email protected]>
1 parent 90377c2 commit caf8a1b

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

.github/workflows/openmetrics.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: OpenMetrics
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- 'docs/specs/om/open_metrics_spec_2_0.md'
7+
8+
jobs:
9+
check-abnf:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v4
14+
- name: Set up Python 3.10
15+
uses: actions/setup-python@v3
16+
with:
17+
python-version: "3.12.3"
18+
- name: Install dependencies
19+
run: |
20+
python -m pip install --upgrade pip
21+
pip install abnf
22+
- name: Check ABNF for OpenMetrics 2.0
23+
run: |
24+
python3 scripts/check_openmetrics_spec.py docs/specs/om/open_metrics_spec_2_0.md

docs/specs/om/open_metrics_spec_2_0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
23
title: "OpenMetrics 2.0"
34
nav_title: "2.0"
45
sort_rank: 2

scripts/check_openmetrics_spec.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/env python3
2+
3+
from abnf import Rule
4+
import sys
5+
6+
class FromStringRule(Rule):
7+
pass
8+
9+
# Start node for the OpenMetrics spec.
10+
start_node = 'exposition'
11+
12+
def get_spec(filename):
13+
with open(filename, 'r') as file:
14+
lines = file.readlines()
15+
spec = []
16+
collecting = False
17+
for line in lines:
18+
if collecting:
19+
if line.startswith('```'):
20+
collecting = False
21+
else:
22+
spec.append(line.strip())
23+
continue
24+
if line.startswith('```abnf'):
25+
if len(spec) > 0:
26+
raise ValueError("Multiple ABNF blocks found in the file.")
27+
collecting = True
28+
29+
if len(spec) == 0:
30+
raise ValueError("No or empty ABNF block found in the file. Wanted ```abnf ... ```.")
31+
return spec
32+
33+
# Main
34+
if __name__ == "__main__":
35+
if len(sys.argv) != 2:
36+
print("Usage: python3 check_openmetrics_spec.py <filename.md>")
37+
sys.exit(1)
38+
39+
filename = sys.argv[1]
40+
if not filename.endswith('.md'):
41+
print(f"Error: {filename} is not a Markdown file.")
42+
sys.exit(1)
43+
spec = get_spec(filename)
44+
try:
45+
rule = FromStringRule.load_grammar(grammar='\n'.join(spec), strict=True)
46+
except Exception as e:
47+
print(f"Error parsing ABNF: {e}")
48+
sys.exit(1)
49+
print("ABNF parsed successfully.")

0 commit comments

Comments
 (0)