如何使用Prometheus存储要由Grafana可视化的数据 [英] How to use Prometheus to store Data to be visualized by Grafana

查看:334
本文介绍了如何使用Prometheus存储要由Grafana可视化的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

With the help of Python request.get and json.loads functions, I was able to source the data from an URL. The data contain a list of instances where each one of them have metrics such as instance_id, status, etc.

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

My question is that is there any way I could upload those metrics to Prometheus? I looked into the pushgateway function but was not sure if that is the correct way of doing data pushing and storage.

我目前将数据推送到Prometheus的努力如下:

My current effort to push data to Prometheus is the following:

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)

但是,我不确定应该使用哪种度量标准类型(量规",摘要"还是其他?)

However, I am not sure what metric type I should be pushing (Gauge, Summary or something else?)

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

Here is an example of the instance data I wish to push:

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

之所以将这些数据推送给Prometheus,是因为我想使用Grafana来获取这些数据并对其进行可视化.

The reason why I am pushing that data to Prometheus is that I wanted to use Grafana to get those data and visualize them.

示例Grafana图将是:以时间为x轴,带有状态码的实例数:以200为y轴.

An example Grafana graph would be: time as x-axis, number of instances with status-code: 200 as y-axis.

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

Any thoughts or help will be appreciated. Thank you!

推荐答案

由于我做了类似的事情,所以我可以给你一些指导.您可以将pushgateway用于"临时和批处理作业,以将其度量标准暴露给Prometheus ",如

Since I did something similar, I can give you a few pointers. You can use pushgateway for "ephemeral and batch jobs to expose their metrics to Prometheus" as it says on there github page.

我认为您需要摘要指标,但是请确保阅读有关Prometheus

I think you need the summary metric, but to be sure please read the docs on the types of metrics on the prometheus docs page.

您已经完成了第一部分,通过 pushgateway 公开了所需的指标,您始终可以通过网络浏览器或curl来检查其是否有效:

You did the first part, you exposed the metrics that you want through pushgateway, you can always check that it works through your web browser or with curl:

您的IP地址:9091

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

If you can see some data on that port, you have to setup your configuration on the prometheus server.

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

You need to tell the prometheus server that is should scrape your nodes pushgateway's metrics. The file you're looking for is "prometheus.yml" its location depends on where you installed the prometheus server.

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

When you open it you should write something like this:

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

重启后,您的prometheus服务器将在路径 YourPrometheusIP:9090/targets 上看到来自节点的数据.

After that restart your prometheus server and the data from your node should be visible on the path YourPrometheusIP:9090/targets.

这篇关于如何使用Prometheus存储要由Grafana可视化的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆