如何将时间序列CSV转换为Prometheus的OpenMetrics格式?
将CSV时间序列转换为OpenMetrics格式导入Prometheus
针对你提供的CSV结构(包含时间、站点ID和4个传感器值),可以通过以下两种可靠方式完成格式转换:
方法一:自定义Python脚本(最灵活可控)
如果工具无法适配你的数据格式,自己写脚本是最直接的解决方案。以下脚本会将CSV逐行转换为符合OpenMetrics规范的内容:
import csv from datetime import datetime # 定义每个传感器对应的Prometheus指标元数据 metric_defs = { "s0": {"name": "station_sensor_s0", "help": "Reading from sensor s0 at weather station", "type": "gauge"}, "s1": {"name": "station_sensor_s1", "help": "Reading from sensor s1 at weather station", "type": "gauge"}, "s2": {"name": "station_sensor_s2", "help": "Reading from sensor s2 at weather station", "type": "gauge"}, "s3": {"name": "station_sensor_s3", "help": "Reading from sensor s3 at weather station", "type": "gauge"}, } # 初始化OpenMetrics头部(必须包含HELP和TYPE注释) output_lines = [] for metric in metric_defs.values(): output_lines.append(f"# HELP {metric['name']} {metric['help']}") output_lines.append(f"# TYPE {metric['name']} {metric['type']}") # 读取CSV并转换每一行数据 with open("your_data.csv", "r", encoding="utf-8") as csv_file: reader = csv.DictReader(csv_file) for row in reader: # 将ISO格式时间转换为Unix时间戳(秒级,Prometheus默认支持) unix_ts = int(datetime.fromisoformat(row["time"]).timestamp()) station_id = row["id_station"] # 为每个传感器生成指标行 for sensor_col in ["s0", "s1", "s2", "s3"]: metric_info = metric_defs[sensor_col] sensor_value = row[sensor_col] output_lines.append(f'{metric_info["name"]}{{id_station="{station_id}"}} {sensor_value} {unix_ts}') # 将结果写入输出文件 with open("station_metrics.openmetrics", "w", encoding="utf-8") as out_file: out_file.write("\n".join(output_lines) + "\n")
使用说明:
- 将脚本中的
your_data.csv替换为你的CSV文件路径 - 运行脚本后会生成
station_metrics.openmetrics文件 - 可以用Prometheus的
promtool验证格式正确性:promtool check metrics station_metrics.openmetrics
方法二:调整csv-exporter配置(适配现有工具)
如果坚持使用csv-exporter,问题大概率出在配置文件未匹配你的CSV结构。以下是针对你的数据的示例配置:
jobs: - name: "station_sensors_export" url: "file:///path/to/your_data.csv" metrics: - name: "station_sensor_s0" type: "gauge" help: "Sensor s0 reading" labels: id_station: "{{.id_station}}" value: "{{.s0}}" timestamp: "{{.time}}" timestamp_format: "2006-01-02T15:04:05" - name: "station_sensor_s1" type: "gauge" help: "Sensor s1 reading" labels: id_station: "{{.id_station}}" value: "{{.s1}}" timestamp: "{{.time}}" timestamp_format: "2006-01-02T15:04:05" - name: "station_sensor_s2" type: "gauge" help: "Sensor s2 reading" labels: id_station: "{{.id_station}}" value: "{{.s2}}" timestamp: "{{.time}}" timestamp_format: "2006-01-02T15:04:05" - name: "station_sensor_s3" type: "gauge" help: "Sensor s3 reading" labels: id_station: "{{.id_station}}" value: "{{.s3}}" timestamp: "{{.time}}" timestamp_format: "2006-01-02T15:04:05"
使用说明:
- 将配置中的文件路径替换为你的CSV路径
- 启动csv-exporter时指定该配置文件
- 通过
http://localhost:9275/metrics访问导出的指标,保存为文件或直接配置Prometheus scrape该端点
内容的提问来源于stack exchange,提问作者AbdelKh
相关产品推荐
相关产品推荐

