Python结合Prometheus

参考:
Prometheus Python Client https://github.com/prometheus/client_python
https://www.zybuluo.com/rickyChen/note/831219

INSTALL

pip install prometheus_client

DEMO

from prometheus_client import start_http_server, 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__':
    # Start up the server to expose the metrics.
    start_http_server(8000)
    # Generate some requests.
    while True:
        process_request(random.random())

运行程序,然后访问 http://localhost:8000/ 可以查看 metrics.

image.png

CONFIG Prometheus

在配置文件添加:

  - job_name: python_test
    static_configs:
      - targets: ['localhost:8000']

可以看到已经被监控起来了


image.png

可以查询看一下结果:


image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容