Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ if you want to use self defined counter configuration file on host, here's an ex
$ sudo docker run -d --name=mx-exporter --device=/dev/dri -p 0.0.0.0:<host port>:<http port> -v <new config file>:/work/counters.csv <image name> -c /work/counters.csv
```

Validate the counter CSV before starting the exporter:

```
$ python3 tools/lint_counters.py mx_exporter/default-counters.csv
```

## Run as python application

```
Expand Down
64 changes: 64 additions & 0 deletions tools/lint_counters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python3
"""Lint mx-exporter counter CSV files."""

from __future__ import annotations

import argparse
import csv
import sys
from pathlib import Path


VALID_TYPES = {"Gauge", "Counter", "Summary", "Histogram", "Info"}


def lint_counter_file(path: Path) -> list[str]:
errors: list[str] = []
seen_ids: set[str] = set()

with path.open(newline="", encoding="utf-8") as handle:
for line_no, row in enumerate(csv.reader(handle), start=1):
if not row or not "".join(row).strip() or row[0].lstrip().startswith("#"):
continue
if len(row) < 4:
errors.append(f"{line_no}: expected at least 4 columns, got {len(row)}")
continue

metric_id = row[0].strip()
metric_type = row[1].strip()
metric_name = row[2].strip()
if metric_id in seen_ids:
errors.append(f"{line_no}: duplicate metric id {metric_id!r}")
seen_ids.add(metric_id)

if metric_type not in VALID_TYPES:
errors.append(f"{line_no}: invalid metric type {metric_type!r}")
if not metric_name:
errors.append(f"{line_no}: empty prometheus metric name")

return errors


def main() -> int:
parser = argparse.ArgumentParser(description="Lint mx-exporter counter CSV files.")
parser.add_argument(
"path",
nargs="?",
type=Path,
default=Path(__file__).resolve().parents[1] / "mx_exporter" / "default-counters.csv",
)
args = parser.parse_args()

errors = lint_counter_file(args.path)
if errors:
print(f"Counter lint failed for {args.path}:", file=sys.stderr)
for error in errors:
print(f" {error}", file=sys.stderr)
return 1

print(f"Counter lint passed for {args.path}")
return 0


if __name__ == "__main__":
raise SystemExit(main())