如何使用 Prometheus 存储 Grafana 可视化的数据

Posted

技术标签:

【中文标题】如何使用 Prometheus 存储 Grafana 可视化的数据【英文标题】:How to use Prometheus to store Data to be visualized by Grafana 【发布时间】:2019-02-28 05:51:13 【问题描述】:

借助 Python request.getjson.loads 函数,我能够从 URL 获取数据。数据包含一个实例列表,其中每个实例都有 instance_id、status 等指标。

我的问题是有什么方法可以将这些指标上传到 Prometheus?我查看了 pushgateway 功能,但不确定这是否是进行数据推送和存储的正确方法。

我目前将数据推送到 Prometheus 的工作如下:

from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
from prometheus_client import Summary

registry1 = CollectorRegistry()
registry2 = CollectorRegistry()

s = Summary('request_latency_seconds', 'Description of summary', registry=registry1)
s.observe(4.7)

g = Gauge('job_last_success_unixtime', 'Last time a batch job successfully finished', registry=registry2)
g.set_to_current_time()

push_to_gateway('localhost:9091', job='batch_summary', registry=registry1)
push_to_gateway('localhost:9091', job='batch_gauge', registry=registry2)

但是,我不确定我应该推送哪种指标类型(仪表、摘要或其他?)

这是我要推送的实例数据的示例:

'url': 'https://example.com', 
'created': '2017-09-17-time', 
'status_code': 200, 
'instance_start': '2018-09-17-time', 

我将这些数据推送到 Prometheus 的原因是我想使用 Grafana 获取这些数据并将它们可视化。

Grafana 图表示例如下:x 轴为时间,y 轴为状态码为 200 的实例数。

任何想法或帮助将不胜感激。谢谢!

【问题讨论】:

【参考方案1】:

由于我做了类似的事情,我可以给你一些指示。正如github 页面上所说,您可以将 pushgateway 用于“临时和批处理作业以将其指标公开给 Prometheus”。

我认为您需要 summary 指标,但请务必阅读 prometheus docs 页面上有关指标类型的文档。

你做了第一部分,你通过 pushgateway 公开了你想要的指标,你总是可以通过你的网络浏览器或 curl 检查它是否有效:

YOURIP:9091

如果您可以在该端口上看到一些数据,则必须在 prometheus 服务器 上设置您的配置。

您需要告诉 prometheus 服务器应该抓取您的节点 pushgateway 的指标。您要查找的文件是“prometheus.yml”,其位置取决于您安装 prometheus 服务器的位置。

当你打开它时,你应该这样写:

  - job_name: 'SomeJob-Name'
    static_configs:
      - targets: ['YourIPADDRESS:9091']
        labels:
          environment: 'prod'

之后重新启动您的 prometheus 服务器,您节点中的数据应该会在路径 YourPrometheusIP:9090/targets 上可见。

【讨论】:

谢谢我尝试在我的本地主机上听 Prometheus 读取,现在我将尝试通过 pushgateway 来完成。

以上是关于如何使用 Prometheus 存储 Grafana 可视化的数据的主要内容,如果未能解决你的问题,请参考以下文章

prometheus+grafana+consul+supervisor搭建监控系统之supervisor搭建

如何使用 Prometheus 存储 Grafana 可视化的数据

如何精简 Prometheus 的指标和存储占用

prometheus(或grafana)查询来评估grafana loki使用的存储?

我们如何在 prometheus 数据存储中获得高可用性?

如何将 mule(基于 Java)日志推送到 Prometheus 存储?