【问题标题】:Expose package metrics to Prometheus with prometheus_client使用 prometheus_client 向 Prometheus 公开包指标
【发布时间】:2020-09-18 14:16:02
【问题描述】:

我有两个文件自己运行以下代码:

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)

有没有办法使用 prometheus_client 将 my_counter1 和 my_counter2 暴露给我的 Prometheus 服务器?

我查看了https://github.com/prometheus/client_python,但我不太清楚如何将sandbox1.py 和sandbox2.py 中的指标调用到实际向Prometheus 公开指标的文件中。指标是否必须始终属于同一个文件?

【问题讨论】:

    标签: python python-3.x prometheus


    【解决方案1】:

    Prometheus 服务器抓取提供指标的 HTTP 端点。这与将指标推送到公制系统的其他一些公制系统不同。由于 Prometheus 会抓取指标端点,因此您需要做两件事:

    1. 使用 HTTP 服务器|端点公开来自客户端的指标
    2. 将 Prometheus 服务器配置为针对此指标服务器|端点

    如果您在您引用的页面上运行 Three Step Demo 示例,然后浏览 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
    

    这是您的 Prometheus 服务器将配置为抓取的页面。你可以看到,例如提供python_gc_objects_collected_total 计数器。 Prometheus 指标是人类可读的,这很有用。

    如果你把你的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())
    

    再次运行代码,您现在应该看到:

    # 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
    

    注意页面底部是my_counter1_total和my_counter1_created,对应您的my_counter1 = Counter("my_counter1","My counter")

    如果您将 Prometheus 服务器指向此目标 (localhost:8000),您应该能够例如绘制my_counter1_total 计数器和my_counter1_created 仪表。

    创建一个名为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"
    

    然后运行 ​​Prometheus:

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

    注意这假定prometheus.yml 在工作目录中

    这个,你可以在 http://localhost:9090 上浏览 Prometheus

    您可以看到您的代码配置为目标:http://localhost:9090/targets

    您可以查询|绘制您的指标。输入例如my_counter1_ 看到两者,然后 my_counter1_total 看到计数器:

    【讨论】:

    • 谢谢,这很有用。有没有办法将sandbox1.py 和sandbox2.py 的代码保存在单独的文件中?仅在我想从多个模块一起工作的更复杂的包中收集不同的指标时询问。
    • 很高兴它有帮助。是的,您可以将代码拆分到多个文件中,您只需要管理导入。
    猜你喜欢
    • 2022-06-20
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 2019-12-13
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多