Skip to content

Commit cffbb47

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 cffbb47

File tree

3 files changed

+134
-1
lines changed

3 files changed

+134
-1
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@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
14+
- name: Set up Python 3.x
15+
uses: uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ Line endings MUST be signalled with line feed (\n) and MUST NOT contain carriage
399399

400400
An example of a complete exposition:
401401

402-
```
402+
```openmetrics
403403
# TYPE acme_http_router_request_seconds summary
404404
# UNIT acme_http_router_request_seconds seconds
405405
# HELP acme_http_router_request_seconds Latency though all of ACME's HTTP request router.

scripts/check_openmetrics_spec.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/bin/env python3
2+
#
3+
# This script opens a markdown file containing the OpenMetrics specification,
4+
# extracts the ABNF grammar from it, and checks if the grammar is valid.
5+
# ABNF grammer must be enclosed in
6+
# ```abnf
7+
# exposition = metricset HASH SP eof [ LF ]
8+
# ...
9+
# ```
10+
# code block, and the top node must be `exposition`.
11+
# It also extracts examples from the OpenMetrics spec file and checks if they
12+
# are valid according to the grammar.
13+
# Exampes must be enclosed in
14+
# ```openmetrics
15+
# ... example content ...
16+
# ```
17+
# code blocks.
18+
19+
from abnf import Rule
20+
import sys
21+
22+
class Grammar(Rule):
23+
pass
24+
25+
# Start node for the OpenMetrics spec.
26+
start_node = 'exposition'
27+
28+
def get_spec(filename):
29+
with open(filename, 'r') as file:
30+
lines = file.readlines()
31+
spec = []
32+
collecting = False
33+
for line in lines:
34+
if collecting:
35+
if line.startswith('```'):
36+
collecting = False
37+
else:
38+
spec.append(line.strip())
39+
continue
40+
if line.startswith('```abnf'):
41+
if len(spec) > 0:
42+
raise ValueError("Multiple ABNF blocks found in the file.")
43+
collecting = True
44+
45+
if len(spec) == 0:
46+
raise ValueError("No or empty ABNF block found in the file. Wanted ```abnf ... ```.")
47+
return '\n'.join(spec)
48+
49+
50+
class example:
51+
def __init__(self, line_number, content):
52+
self.line_number = line_number
53+
self.content = content
54+
55+
class examples:
56+
"""
57+
Extracts examples from the OpenMetrics spec file with generator function.
58+
"""
59+
def __init__(self, filename):
60+
self.file = open(filename, 'r')
61+
self.line_number = 0
62+
63+
def __iter__(self):
64+
return self
65+
66+
def __next__(self):
67+
collecting = False
68+
start_line = self.line_number
69+
example_lines = []
70+
for line in self.file:
71+
self.line_number += 1
72+
if collecting:
73+
if line.startswith('```'):
74+
collecting = False
75+
break
76+
else:
77+
example_lines.append(line)
78+
elif line.startswith('```openmetrics'):
79+
start_line = self.line_number
80+
collecting = True
81+
if len(example_lines) > 0:
82+
return example(start_line, ''.join(example_lines).strip())
83+
84+
raise StopIteration("No more examples found.")
85+
86+
# Main
87+
if __name__ == "__main__":
88+
if len(sys.argv) != 2:
89+
print("Usage: python3 check_openmetrics_spec.py <filename.md>")
90+
sys.exit(1)
91+
92+
filename = sys.argv[1]
93+
if not filename.endswith('.md'):
94+
print(f"Error: {filename} is not a Markdown file.")
95+
sys.exit(1)
96+
spec = get_spec(filename)
97+
print(spec)
98+
try:
99+
Grammar.load_grammar(grammar=spec, strict=True)
100+
except Exception as e:
101+
print(f"Error parsing ABNF: {e}")
102+
sys.exit(1)
103+
print("ABNF parsed successfully.")
104+
for ex in examples(filename):
105+
try:
106+
Grammar.get(start_node).parse_all(ex.content)
107+
print(f"Example parsed successfully: {ex.line_number}: {ex.content[:30]}...") # Print first 30 chars
108+
except Exception as e:
109+
print(f"Error parsing example at line {ex.line_number}: {e}\nExample: {ex.content[:30]}...")

0 commit comments

Comments
 (0)