简体   繁体   English

Prometheus 导出器 - 读取 CSV 文件,其中包含过去一天的数据

[英]Prometheus exporter - reading CSV file having data from the past day

I am writing a Prometheus exporter which has to rad different CSV files.我正在编写一个 Prometheus 导出器,它必须包含不同的 CSV 文件。 Each of them contains one entire day of data from the past (the goal is to have the exporter read a new CSV file each day. One CSV file is uploaded to the server each day, containing the data of the previous day.它们中的每一个都包含过去一整天的数据(目标是让导出器每天读取一个新的 CSV 文件。每天将一个 CSV 文件上传到服务器,其中包含前一天的数据。

IN the CSV file, I have the same metrics every 5 mns.在 CSV 文件中,我每 5 分钟就有一次相同的指标。 eg:例如:

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

I struggle to add theses data in Prometheus properly.我很难在 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 just read the first line, add it as a metric and read exactly the same again each time he scrapes the exporter. Prometheus 只需读取第一行,将其添加为度量标准,并在每次抓取导出器时再次读取完全相同的内容。 What am I doing wrong?我究竟做错了什么? I feel like this data should be a Jauge, as it goes up and down, but Prometheus seems that he doesn't want different data from the same Jauge in one scrape ?我觉得这个数据应该是一个 Jauge,因为它会上下波动,但 Prometheus 似乎不希望一次刮取来自同一个Jauge的不同数据?

The page introduces a way to import history data to Prometheus.页面介绍了一种将历史数据导入 Prometheus 的方法。

The.txt file is OpenMetrics format, and an example import command is: tsdb import rrd_exported_data.txt /var/lib/prometheus/ --max-samples-in-mem=10000 . .txt 文件为 OpenMetrics 格式,示例导入命令为: tsdb import rrd_exported_data.txt /var/lib/prometheus/ --max-samples-in-mem=10000

Sample file rrd_exported_data.txt ( [metric]{[labels]} [number value] [timestamp ms] ):示例文件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

Maybe you can execute the command in your python code.也许您可以在 python 代码中执行该命令。

Prometheus doesn't support data importing (aka push model) - it supports only data scraping (aka pull model). Prometheus 不支持数据导入(又名推送模型) - 它仅支持数据抓取(又名拉取模型)。 See this article for details.有关详细信息,请参阅本文 Additionally to this, Prometheus doesn't support storing historical data (aka backfilling).除此之外,Prometheus 不支持存储历史数据(也称为回填)。

If you need importing historical CSV data to Prometheus-like system, then take a look at VictoriaMetrics - it supports importing CSV data and it supports data backfilling .如果您需要将 CSV 历史数据导入到类似 Prometheus 的系统中,请查看 VictoriaMetrics - 它支持导入 CSV 数据支持数据回填

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM