-
Notifications
You must be signed in to change notification settings - Fork 1
增加监控导出计数器汇总结构化输出 #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ghangz
wants to merge
2
commits into
MetaX-MACA:main
Choose a base branch
from
ghangz:mengz/mxexporter-counter-summary-json
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+89
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import csv | ||
| import tempfile | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
| from tools.summarize_counters import summarize | ||
|
|
||
|
|
||
| class SummarizeCountersTest(unittest.TestCase): | ||
| def test_counts_metric_types_and_duplicates(self): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| path = Path(tmpdir) / "counters.csv" | ||
| with path.open("w", newline="", encoding="utf-8") as handle: | ||
| writer = csv.writer(handle) | ||
| writer.writerow(["1", "Gauge", "mx_gpu_temp"]) | ||
| writer.writerow(["2", "Gauge", "mx_gpu_temp"]) | ||
| writer.writerow(["3", "Counter", "mx_error_count"]) | ||
| writer.writerow(["4", "Gauge", ""]) | ||
| writer.writerow(["5", "", "mx_ignored"]) | ||
|
|
||
| summary = summarize(path) | ||
|
|
||
| self.assertEqual(summary["metric_count"], 3) | ||
| self.assertEqual(summary["type_counts"], {"Counter": 1, "Gauge": 2}) | ||
| self.assertEqual(summary["duplicate_metric_names"], ["mx_gpu_temp"]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #!/usr/bin/env python3 | ||
| """Summarize mx-exporter counter CSV files as JSON.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import csv | ||
| import json | ||
| from collections import Counter | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def summarize(path: Path) -> dict[str, object]: | ||
| type_counts: Counter[str] = Counter() | ||
| metric_names: list[str] = [] | ||
| with path.open(newline="", encoding="utf-8") as handle: | ||
| for row in csv.reader(handle): | ||
| if not row or not "".join(row).strip() or row[0].lstrip().startswith("#"): | ||
| continue | ||
| if len(row) < 3: | ||
| continue | ||
| metric_type = row[1].strip() | ||
| metric_name = row[2].strip() | ||
| if metric_type and metric_name: | ||
| type_counts[metric_type] += 1 | ||
| metric_names.append(metric_name) | ||
| duplicates = sorted(name for name, count in Counter(metric_names).items() if count > 1) | ||
| return { | ||
| "path": str(path), | ||
| "metric_count": len(metric_names), | ||
| "type_counts": dict(sorted(type_counts.items())), | ||
| "duplicate_metric_names": duplicates, | ||
| } | ||
|
|
||
|
|
||
| def main() -> int: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument( | ||
| "path", | ||
| nargs="?", | ||
| type=Path, | ||
| default=Path(__file__).resolve().parents[1] / "mx_exporter" / "default-counters.csv", | ||
| ) | ||
| parser.add_argument("--output", type=Path, help="write summary JSON to this path") | ||
| args = parser.parse_args() | ||
|
|
||
| if not args.path.is_file(): | ||
| parser.error(f"input file does not exist: {args.path}") | ||
|
|
||
| payload = summarize(args.path) | ||
|
Comment on lines
+45
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| text = json.dumps(payload, indent=2, ensure_ascii=False) | ||
| if args.output: | ||
| args.output.write_text(text + "\n", encoding="utf-8") | ||
| else: | ||
| print(text) | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果
metric_name为空,当前逻辑仍会增加type_counts[metric_type]的计数,但不会将该指标加入metric_names。这会导致最终输出的metric_count(即len(metric_names))与type_counts的各项之和不一致。建议仅在metric_name和metric_type均非空时才进行统计,以保证数据的一致性。