aioprometheus.service module

This module implements an asynchronous Prometheus metrics export service.

class aioprometheus.service.Service(registry: aioprometheus.collectors.Registry = <aioprometheus.collectors.Registry object>)

Bases: object

This class implements a Prometheus metrics service that can be embedded within asyncio based applications so they can be scraped by the Prometheus.io server.

property base_url: str

Return the base service url

Raises

Exception if the server has not been started.

Returns

the base service URL as a string

async handle_metrics(request: aiohttp.web_request.Request) aiohttp.web_response.Response

Handle a request to the metrics route.

The request is inspected and the most efficient response data format is chosen.

async handle_robots(request: aiohttp.web_request.Request) aiohttp.web_response.Response

Handle a request to /robots.txt

If a robot ever stumbles on this server, discourage it from indexing.

async handle_root(request: aiohttp.web_request.Request) aiohttp.web_response.Response

Handle a request to the / route.

Serves a trivial page with a link to the metrics. Use this if ever you need to point a health check at your the service.

property metrics_url: str

Return the Prometheus metrics url

Raises

Exception if the server has not been started.

Returns

the metrics URL as a string

property root_url: str

Return the root service url

Raises

Exception if the server has not been started.

Returns

the root URL as a string

async start(addr: str = '', port: int = 0, ssl: Optional[ssl.SSLContext] = None, metrics_url: str = '/metrics') None

Start the prometheus metrics HTTP(S) server.

Parameters
  • addr – the address to bind the server on. By default this is set to an empty string so that the service becomes available on all interfaces.

  • port – The port to bind the server on. The default value is 0 which will cause the server to bind to an ephemeral port. If you want the server to operate on a fixed port then you need to specify the port.

  • ssl – a sslContext for use with TLS.

  • metrics_url – The name of the endpoint route to expose prometheus metrics on. Defaults to ‘/metrics’.

Raises

Exception if the server could not be started.

async stop() None

Stop the prometheus metrics HTTP(S) server