【问题标题】:Prometheus exporter - reading CSV file having data from the past dayPrometheus 导出器 - 读取包含过去一天数据的 CSV 文件
【发布时间】:2020-07-26 21:20:28
【问题描述】:

我正在编写一个 Prometheus 导出器,它必须处理不同的 CSV 文件。它们中的每一个都包含过去一整天的数据(目标是让导出器每天读取一个新的 CSV 文件。每天将一个 CSV 文件上传到服务器,其中包含前一天的数据。

在 CSV 文件中,我每 5 分钟就有一次相同的指标。例如:

Date;Time;data
23.03.20;23:55:00;1
23.03.20;23:50:00;50
23.03.20;23:45:00;3

我很难在 Prometheus 中正确添加这些数据。

class CSVCollector(object):
  def collect(self):
    # We list all the min files in the current directory
    list_min = glob.glob("min*.csv")
    metric = GaugeMetricFamily(
                'day_tests_seconds',
                'kw', labels=["jobname"])
    for min in list_min :
      with open(min) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=';')
        line_count = 0
        for row in csv_reader:
            if line_count == 1:
                correct_date_format = row[0][:6] + "20" + row[0][6:]
                datetime_row = correct_date_format + ';' + row[1]
                timestamp = int(time.mktime(datetime.datetime.strptime(datetime_row, "%d.%m.%Y;%H:%M:%S").timetuple()))
                metric.add_metric(str(line_count), int(row[4]), timestamp)
            line_count += 1
    yield metric   
     


if __name__ == '__main__':
  # Usage: json_exporter.py port endpoint
  start_http_server(int(sys.argv[1]))
  REGISTRY.register(CSVCollector())
  while True: time.sleep(1)

Prometheus 只需读取第一行,将其添加为指标,并在每次 scrapes 导出器时再次读取完全相同的内容。我究竟做错了什么 ?我觉得这个数据应该是一个 Jauge,因为它会上升和下降,但 Prometheus 似乎不希望在一次 scrape 中来自同一个 Jauge 的不同数据?

【问题讨论】:

    标签: python prometheus exporter


    【解决方案1】:

    Prometheus 不支持数据导入(又名推送模型) - 它仅支持数据抓取(又名拉取模型)。有关详细信息,请参阅this article。除此之外,Prometheus 不支持存储历史数据(也称为回填)。

    如果您需要将历史 CSV 数据导入类似 Prometheus 的系统,请查看 VictoriaMetrics - supports importing CSV datasupports data backfilling

    【讨论】:

      【解决方案2】:

      page 引入了一种将历史数据导入 Prometheus 的方法。

      .txt 文件为 OpenMetrics 格式,示例导入命令为:tsdb import rrd_exported_data.txt /var/lib/prometheus/ --max-samples-in-mem=10000

      示例文件rrd_exported_data.txt ([metric]{[labels]} [number value] [timestamp ms]):

      collectd_df_complex{host="myserver.fqdn.com",df="var-log",dimension="free"} 5.8093906125e+10 1582226100000
      collectd_varnish_derive{host="myserver.fqdn.com",varnish="request_rate",dimension="MAIN.client_req"} 2.3021666667e+01 1582226100000
      collectd_df_complex{host="myserver.fqdn.com",df="var-log",dimension="free"} 5.8093906125e+10 1582226100000
      collectd_load{host="myserver.fqdn.com",type="midterm"} 0.0155 1582226100000
      

      也许你可以在你的python代码中执行命令。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-26
        • 1970-01-01
        • 1970-01-01
        • 2018-01-01
        • 2013-03-02
        • 2023-03-18
        • 1970-01-01
        • 2015-05-30
        相关资源
        最近更新 更多