简体   繁体   English

使用 prometheus_client 向 Prometheus 公开包指标

[英]Expose package metrics to Prometheus with prometheus_client

I have two files running the following code on their own:我有两个文件自己运行以下代码:

sandbox1.py

from prometheus_client import Counter
import time
while True:
    my_counter1 = Counter('my_counter1', 'My counter)
    my_counter1.inc()
    time.sleep(1)

sandbox2.py

from prometheus_client import Counter
import time
while True:
    my_counter2 = Counter('my_counter2', 'My counter)
    my_counter2.inc()
    time.sleep(2)

Is there a way of using prometheus_client to expose my_counter1 and my_counter2 to my Prometheus server?有没有办法使用 prometheus_client 将my_counter1my_counter2暴露给我的 Prometheus 服务器?

I've had a look at https://github.com/prometheus/client_python , but I'm not too sure how how to call the metrics from sandbox1.py and sandbox2.py into the file that actually exposes the metrics to Prometheus.我看过https://github.com/prometheus/client_python ,但我不太确定如何将来自sandbox1.pysandbox2.py的指标调用到实际向 Prometheus 公开指标的文件中. Do the metrics always have to be part of the same file?指标是否总是必须是同一个文件的一部分?

Prometheus server scrapes HTTP endpoints that provide metrics. Prometheus 服务器抓取提供指标的 HTTP 端点。 This differs from some other metric systems where the metrics are pushed to the metric system.这与将指标推送到公制系统的其他一些公制系统不同。 Because Prometheus scrapes metric endpoints, you need to do two things:因为 Prometheus 会抓取指标端点,所以您需要做两件事:

  1. Expose the metrics from your client using an HTTP server|endpoint使用 HTTP 服务器|端点公开来自客户端的指标
  2. Configure the Prometheus server to target this metric server|endpoint配置 Prometheus 服务器以定位此指标服务器|端点

If you run the Three Step Demo example on the page that you reference, and then browse http://localhost:8000, you should see something like:如果在您引用的页面上运行三步演示示例,然后浏览 http://localhost:8000,您应该看到如下内容:

# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 357.0
python_gc_objects_collected_total{generation="1"} 0.0
python_gc_objects_collected_total{generation="2"} 0.0
...
...
# HELP request_processing_seconds Time spent processing request
# TYPE request_processing_seconds summary
request_processing_seconds_count 4.0
request_processing_seconds_sum 2.0374009040533565
# HELP request_processing_seconds_created Time spent processing request
# TYPE request_processing_seconds_created gauge
request_processing_seconds_created 1.6004497426536365e+09

This is the page that your Prometheus server would be configured to scrape.这是您的 Prometheus 服务器将配置为抓取的页面。 You can see, eg python_gc_objects_collected_total counter is provided.您可以看到,例如提供了python_gc_objects_collected_total计数器。 Prometheus metrics are human readable which is useful. Prometheus 指标是人类可读的,这很有用。

If you combine, your sandbox1.py into this example:如果你结合,你的sandbox1.py到这个例子中:

from prometheus_client import start_http_server, Counter, Summary
import random
import time

# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds',
                       'Time spent processing request')


# Decorate function with metric.
@REQUEST_TIME.time()
def process_request(t):
    """A dummy function that takes some time."""
    time.sleep(t)


if __name__ == '__main__':
    my_counter1 = Counter('my_counter1', 'My counter')
    # Start up the server to expose the metrics.
    start_http_server(8000)
    # Generate some requests.
    while True:
        my_counter1.inc()
        process_request(random.random())

And run the code again, you should now see:再次运行代码,您现在应该看到:

# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 357.0
python_gc_objects_collected_total{generation="1"} 0.0
python_gc_objects_collected_total{generation="2"} 0.0
...
...
# HELP my_counter1_total My counter
# TYPE my_counter1_total counter
my_counter1_total 13.0
# HELP my_counter1_created My counter
# TYPE my_counter1_created gauge
my_counter1_created 1.6004498408280134e+09

NOTE At the bottom of the page is my_counter1_total and my_counter1_created which correspond to your my_counter1 = Counter("my_counter1","My counter")注意页面底部是my_counter1_totalmy_counter1_created ,它们对应于您的my_counter1 = Counter("my_counter1","My counter")

If you point a Prometheus server at this target ( localhost:8000 ), you should be able to eg graph the my_counter1_total counter and the my_counter1_created gauge.如果您将 Prometheus 服务器指向此目标 ( localhost:8000 ),您应该能够绘制my_counter1_total计数器和my_counter1_created仪表等图表。

Create a file called prometheus.yml :创建一个名为prometheus.yml的文件:

global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  # Self
  - job_name: "prometheus-server"
    static_configs:
      - targets:
          - "localhost:9090"

  # Python example
  - job_name: "63957470"
    static_configs:
      - targets:
          - "localhost:8000"

And then run Prometheus:然后运行 ​​Prometheus:

docker run \
--interactive --tty \
--net=host \
--volume=${PWD}/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus@sha256:f3ada803723ccbc443ebea19f7ab24d3323def496e222134bf9ed54ae5b787bd

NOTE This assumes that prometheus.yml is in the working directory注意这里假设prometheus.yml在工作目录中

The, you can browse Prometheus on http://localhost:9090 ,你可以在 http://localhost:9090 上浏览 Prometheus

You can see your code configured as a target: http://localhost:9090/targets您可以看到您的代码配置为目标:http://localhost:9090/targets

在此处输入图片说明

And you can query|graph your metrics.您可以查询|绘制您的指标。 Type eg my_counter1_ to see both and then my_counter1_total for the Counter:键入如my_counter1_同时看到,然后my_counter1_total的计数器:

在此处输入图片说明

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

相关问题 使用 Flask 的 prometheus_client 自定义指标 - prometheus_client custom metrics with Flask python prometheus_client guage 指标未保存在 prometheus 中 - python prometheus_client guage metrics are not saved in prometheus Python prometheus_client,CollectorRegistry 中的重复时间序列 - Python prometheus_client, Duplicated timeseries in CollectorRegistry 使用 python prometheus_client 将 f5sdk 池节点详细信息推送到 Prometheus - push f5sdk pool node details to Prometheus using python prometheus_client 是否有一个arg可以在start_http_server(prometheus_client)而非/中设置其他端点? - Is there an arg to set other endpoint in start_http_server (prometheus_client) instead of /? 时间戳未过期的普罗米修斯客户端自定义指标 - prometheus client custom metrics with timestamp not expiring Python。 prometheus_client ValueError:仪表指标缺少标签值 - Python. prometheus_client ValueError: gauge metric is missing label values 如何从多个子流程公开普罗米修斯指标? - How can I expose prometheus metrics from multiple subprocesses? Prometheus 如何使用 start_http_server 在多进程应用程序中公开指标 - Prometheus how to expose metrics in multiprocess app with start_http_server 普罗米修斯计量指标的单元测试 - Unittests for prometheus gauge metrics
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM