[英]Expose package metrics to Prometheus with prometheus_client
我有兩個文件自己運行以下代碼:
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 公開指標的文件中. 指標是否總是必須是同一個文件的一部分?
Prometheus 服務器抓取提供指標的 HTTP 端點。 這與將指標推送到公制系統的其他一些公制系統不同。 因為 Prometheus 會抓取指標端點,所以您需要做兩件事:
如果在您引用的頁面上運行三步演示示例,然后瀏覽 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
的計數器:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.