Skip to main content

Python async gRPC Prometheus Interceptors

Project description

py-async-grpc-prometheus

Instrument library to provide prometheus metrics similar to:

Status

Currently, the library has the parity metrics with the Java and Go library.

Server side:

  • grpc_server_started_total
  • grpc_server_handled_total
  • grpc_server_msg_received_total
  • grpc_server_msg_sent_total
  • grpc_server_handling_seconds

Client side:

  • grpc_client_started_total
  • grpc_client_handled_total
  • grpc_client_msg_received_total
  • grpc_client_msg_sent_total
  • grpc_client_handling_seconds
  • grpc_client_msg_recv_handling_seconds
  • grpc_client_msg_send_handling_seconds

How to use

pip install py-async-grpc-prometheus

Client side:

Client metrics monitoring is done by intercepting the gPRC channel.

from grpc import aio
from py_async_grpc_prometheus.prometheus_async_client_interceptor import get_client_interceptors

channel = aio.insecure_channel("server:6565",
                                 interceptors=get_client_interceptors())
# Start an end point to expose metrics.
start_http_server(metrics_port)

Server side:

Server metrics are exposed by adding the interceptor when the gRPC server is started. Take a look at tests/integration/hello_world/hello_world_client.py for the complete example.

from grpc import aio
from concurrent import futures
from py_async_grpc_prometheus.prometheus_async_server_interceptor import PromAsyncServerInterceptor
from prometheus_client import start_http_server

Start the gRPC server with the interceptor, take a look at tests/integration/hello_world/hello_world_server.py for the complete example.

server = aio.server(futures.ThreadPoolExecutor(max_workers=10),
                       interceptors=(
                           PromAsyncServerInterceptor(),
                       ))
# Start an end point to expose metrics.
start_http_server(metrics_port)

Histograms

Prometheus histograms are a great way to measure latency distributions of your RPCs. However, since it is bad practice to have metrics of high cardinality the latency monitoring metrics are disabled by default. To enable them please call the following in your interceptor initialization code:

server = aio.server(futures.ThreadPoolExecutor(max_workers=10),
                       interceptors=(
                           PromAsyncServerInterceptor(enable_handling_time_histogram=True),
                       ))

After the call completes, its handling time will be recorded in a Prometheus histogram variable grpc_server_handling_seconds. The histogram variable contains three sub-metrics:

  • grpc_server_handling_seconds_count - the count of all completed RPCs by status and method
  • grpc_server_handling_seconds_sum - cumulative time of RPCs by status and method, useful for calculating average handling times
  • grpc_server_handling_seconds_bucket - contains the counts of RPCs by status and method in respective handling-time buckets. These buckets can be used by Prometheus to estimate SLAs (see here)

Server Side:

  • enable_handling_time_histogram: Enables 'grpc_server_handling_seconds'

Client Side:

  • enable_client_handling_time_histogram: Enables 'grpc_client_handling_seconds'
  • enable_client_stream_receive_time_histogram: Enables 'grpc_client_msg_recv_handling_seconds'
  • enable_client_stream_send_time_histogram: Enables 'grpc_client_msg_send_handling_seconds'

Legacy metrics:

Metric names have been updated to be in line with those from https://github.com/grpc-ecosystem/go-grpc-prometheus.

The legacy metrics are:

server side:

  • grpc_server_started_total
  • grpc_server_handled_total
  • grpc_server_handled_latency_seconds
  • grpc_server_msg_received_total
  • grpc_server_msg_sent_total

client side:

  • grpc_client_started_total
  • grpc_client_completed
  • grpc_client_completed_latency_seconds
  • grpc_client_msg_sent_total
  • grpc_client_msg_received_total

In order to be able to use these legacy metrics for backwards compatibility, the legacy flag can be set to True when initialising the server/client interceptors

For example, to enable the server side legacy metrics:

server = aio.server(futures.ThreadPoolExecutor(max_workers=10),
                       interceptors=(
                           PromAsyncServerInterceptor(legacy=True),
                       ))

How to run and test

make initialize-development
make test

TODO:

  • Test prometheus_async_client_interceptor

Reference

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_async_grpc_prometheus-0.2.0.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_async_grpc_prometheus-0.2.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file py_async_grpc_prometheus-0.2.0.tar.gz.

File metadata

  • Download URL: py_async_grpc_prometheus-0.2.0.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for py_async_grpc_prometheus-0.2.0.tar.gz
Algorithm Hash digest
SHA256 37dd7cb5d8b48ebba34a184977dd4b6c505f335e9fe316e5f3b6ee3dc0f37b67
MD5 66617bd1a260a755866b157f617caa3a
BLAKE2b-256 ecd0b2450efae0cce981c49432c83148e62b60f6f1973e6439a2c5be8b9398be

See more details on using hashes here.

File details

Details for the file py_async_grpc_prometheus-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for py_async_grpc_prometheus-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 007f8211e8a3c3a7492ed59eac80fcb163fe35c2e35aa282b1e7ed482bad5a25
MD5 c86131a9ad3af2abd5eb3ebcced93586
BLAKE2b-256 dfd385b795b73bdb87c3d95fb95f51f4ad00c015ce61ca1a2ad090ba4d48c7c5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page