Prometheous 모니터링 용 Metric export example with python


특정 서버의 redis에 있는 값으로 계산되는 항목을 지속적으로 모니터링 해야 하는 상황이 있어서,
어떻게 할까 하다가, 이전에 구축해놓은 프로메테우스-그래파나 서버에서 metiric을 땡겨가는 것이 더 좋을 것 같아서 검색해 보았다.

파이썬 프로메테우스 클라이언트가 존재 하더라!


[https://github.com/prometheus/client_python](https://github.com/prometheus/client_python)

```jsx
pip install prometheus_client
```

```jsx
from prometheus_client import start_http_server, Summary
import random
import time
import redis
from prometheus_client import Gauge

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

# Decorate function with metric.
client_count = Gauge('client_count', 'number of client that connected to server')
client_bandwidth = Gauge('client_bandwidth', 'assigned bandwidth of server with current clients')

@REQUEST_TIME.time()
def process_request():
    """A dummy function that takes some time."""
    client_count.set(4.2)   # TODO: Set client count from redis
    client_bandwidth.set(4.2)   # TODO: Set server bandwidth from redis

if __name__ == '__main__':
    # Start up the server to expose the metrics.
    start_http_server(8000)
    # Generate some requests.
    while True:
        process_request()
```

`localhost:8000`으로 접속하면 다음과 같은 결과를 얻는다.


댓글

이 블로그의 인기 게시물

엘릭서 Phoenix migration (1.5 -> 1.6 and scss for esbuild)

윈도우 디스크 관리세어 삭제 불가능 파티션 삭제 (필요해서 찾아봄)