增强配置解析诊断信息#14
Conversation
There was a problem hiding this comment.
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.
| 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)) |
There was a problem hiding this comment.
在 init_required_metrics 中,metric_func = globals()[row[1]] 这一行位于 try-except 块之外。如果配置文件中包含未导入的指标类型(例如 Summary、Histogram 或 Info,虽然它们在 self.metric_types 中被声明为支持,但实际上并未在文件顶部导入),此处会抛出 KeyError 并导致 exporter 启动崩溃。
此外,为了完整支持 Summary、Histogram 和 Info 等指标类型,建议在获取指标函数时,如果 globals() 中不存在,则尝试从 prometheus_client 动态获取,并将该查找过程放入 try-except 块中,以确保即使遇到不支持的类型也能够优雅地记录错误并跳过,而不是直接崩溃。
| 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)) |
| seen_metric_ids = set() | ||
| seen_metric_names = set() | ||
|
|
||
| with open(config_file, "r", newline="") as file_handle: |
There was a problem hiding this comment.
在打开配置文件时,建议显式指定 encoding="utf-8"。在不同的操作系统或区域设置下,默认的系统编码可能不是 UTF-8(例如在某些 Windows 环境下默认是 GBK),这可能会导致读取包含中文注释或描述的 CSV 配置文件时发生 UnicodeDecodeError。显式指定编码可以提高代码的跨平台鲁棒性。
| with open(config_file, "r", newline="") as file_handle: | |
| with open(config_file, "r", newline="", encoding="utf-8") as file_handle: |
| if len(row) == 0: | ||
| return "empty line" | ||
|
|
||
| if row[0].startswith("#"): |
?? API ?? 2 ??????
这个改动增强了配置解析阶段的容错和定位能力,既能更稳妥地解析指标类型,也能正确处理带缩进的注释行和 UTF-8 配置文件,减少配置问题被误判成运行时异常的情况。
相关单元测试已经补充并通过,覆盖了不支持的指标类型、带缩进注释和 UTF-8 配置读取场景。