Prometheus API Client for Python

0.7.0 · active · verified Sat Apr 11

prometheus-api-client is a Python wrapper for the Prometheus HTTP API, providing tools for collecting and processing metrics. The library is currently at version 0.7.0 and maintains an active development cadence with regular updates and feature additions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a Prometheus host, retrieve all available metric names, query a metric for a specific time range, and fetch its current value. It uses environment variables for the Prometheus URL for better practice.

import os
from prometheus_api_client import PrometheusConnect
from datetime import datetime, timedelta

# Configure your Prometheus URL. Use environment variable for production.
prom_url = os.environ.get('PROMETHEUS_URL', 'http://localhost:9090')

# Establish connection to Prometheus
prom = PrometheusConnect(url=prom_url, disable_ssl=True)

# Get a list of all available metric names
all_metrics = prom.all_metrics()
print(f"Found {len(all_metrics)} metrics. Example: {all_metrics[:5]}")

# Query a specific metric for a range of data
end_time = datetime.now()
start_time = end_time - timedelta(hours=1)
metric_data = prom.get_metric_range_data(
    query='up',
    start_time=start_time,
    end_time=end_time,
    step='5m'
)
print(f"'up' metric data points fetched: {len(metric_data)}")

# Example of getting current value
current_up = prom.get_current_metric_value(query='up')
print(f"Current 'up' metric values: {current_up[:2]}")

view raw JSON →