Skip to content

增强配置解析诊断信息#14

Open
ghangz wants to merge 4 commits into
MetaX-MACA:mainfrom
ghangz:mengz/config-parse-diagnostics
Open

增强配置解析诊断信息#14
ghangz wants to merge 4 commits into
MetaX-MACA:mainfrom
ghangz:mengz/config-parse-diagnostics

Conversation

@ghangz

@ghangz ghangz commented Jun 25, 2026

Copy link
Copy Markdown

这个改动增强了配置解析阶段的容错和定位能力,既能更稳妥地解析指标类型,也能正确处理带缩进的注释行和 UTF-8 配置文件,减少配置问题被误判成运行时异常的情况。

相关单元测试已经补充并通过,覆盖了不支持的指标类型、带缩进注释和 UTF-8 配置读取场景。

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces configuration diagnostics to the exporter by extracting CSV validation and loading logic into a new config_loader module, accompanied by unit tests. It ensures that startup fails immediately if no valid metrics are loaded. The review feedback highlights several robust improvements: handling potential KeyError exceptions when dynamically resolving metric functions from globals(), explicitly specifying utf-8 encoding when opening the configuration file to prevent cross-platform decoding issues, and stripping leading whitespace before checking for comment lines.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread mx_exporter/mx_exporter.py Outdated
Comment on lines +176 to +183
metric_func = globals()[row[1]]
metric_name = row[2]
metric_description = row[3]
metric_labels = row[4:]
try:
self.metrics_required[metric_id] = metric_func(metric_name, metric_description, metric_labels)
except Exception as e:
print("Create metric exception: %s" % (e))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

init_required_metrics 中,metric_func = globals()[row[1]] 这一行位于 try-except 块之外。如果配置文件中包含未导入的指标类型(例如 SummaryHistogramInfo,虽然它们在 self.metric_types 中被声明为支持,但实际上并未在文件顶部导入),此处会抛出 KeyError 并导致 exporter 启动崩溃。

此外,为了完整支持 SummaryHistogramInfo 等指标类型,建议在获取指标函数时,如果 globals() 中不存在,则尝试从 prometheus_client 动态获取,并将该查找过程放入 try-except 块中,以确保即使遇到不支持的类型也能够优雅地记录错误并跳过,而不是直接崩溃。

Suggested change
metric_func = globals()[row[1]]
metric_name = row[2]
metric_description = row[3]
metric_labels = row[4:]
try:
self.metrics_required[metric_id] = metric_func(metric_name, metric_description, metric_labels)
except Exception as e:
print("Create metric exception: %s" % (e))
metric_name = row[2]
metric_description = row[3]
metric_labels = row[4:]
try:
if row[1] not in globals():
import prometheus_client
metric_func = getattr(prometheus_client, row[1])
else:
metric_func = globals()[row[1]]
self.metrics_required[metric_id] = metric_func(metric_name, metric_description, metric_labels)
except (AttributeError, KeyError):
print("Unsupported metric type: %s" % row[1])
except Exception as e:
print("Create metric exception: %s" % (e))

Comment thread mx_exporter/config_loader.py Outdated
seen_metric_ids = set()
seen_metric_names = set()

with open(config_file, "r", newline="") as file_handle:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在打开配置文件时,建议显式指定 encoding="utf-8"。在不同的操作系统或区域设置下,默认的系统编码可能不是 UTF-8(例如在某些 Windows 环境下默认是 GBK),这可能会导致读取包含中文注释或描述的 CSV 配置文件时发生 UnicodeDecodeError。显式指定编码可以提高代码的跨平台鲁棒性。

Suggested change
with open(config_file, "r", newline="") as file_handle:
with open(config_file, "r", newline="", encoding="utf-8") as file_handle:

Comment thread mx_exporter/config_loader.py Outdated
if len(row) == 0:
return "empty line"

if row[0].startswith("#"):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在判断是否为注释行时,使用 row[0].startswith("#")。如果用户在 CSV 文件的注释行前不小心留有空格(例如 # comment),该行将无法被正确识别为注释行,从而可能导致解析错误。建议在判断前使用 .lstrip() 去除首部空格。

Suggested change
if row[0].startswith("#"):
if row[0].lstrip().startswith("#"):

?? API ?? 2 ??????
@ghangz ghangz changed the title 增加配置解析诊断信息 ?????????? Jun 26, 2026
@ghangz ghangz changed the title ?????????? 增强配置解析诊断信息 Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant