Prometheus Remote Writer

1.1.3 · active · verified Thu Apr 16

The `prometheus-remote-writer` is a Python library (version 1.1.3) designed to simplify the process of sending time-series data to Prometheus-compatible storage using the Prometheus Remote Write protocol. It offers a platform-independent, intuitive API, supporting custom HTTP headers, various authentication methods, and efficient batch data sending. The library is actively maintained with regular updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `RemoteWriter` with a target URL and an authorization header, then construct and send a list of time-series data points. Timestamps should be in milliseconds. Ensure `PROMETHEUS_REMOTE_WRITE_URL` and `PROMETHEUS_REMOTE_WRITE_TOKEN` environment variables are set or replace the placeholder values.

import os
import time
from prometheus_remote_writer import RemoteWriter

# Configure the remote write endpoint URL
REMOTE_WRITE_URL = os.environ.get('PROMETHEUS_REMOTE_WRITE_URL', 'https://example.com/prometheus/api/v1/write')
# Configure authentication (e.g., Bearer token)
AUTH_TOKEN = os.environ.get('PROMETHEUS_REMOTE_WRITE_TOKEN', 'YOUR_STATIC_TOKEN_HERE')

# Create a RemoteWriter instance
writer = RemoteWriter(
    url=REMOTE_WRITE_URL,
    headers={'Authorization': f'Bearer {AUTH_TOKEN}'}
)

# Prepare some sample data to send
# Prometheus Remote Write expects timestamps in milliseconds
current_time_ms = int(time.time() * 1000)

data_points = [
    {
        'metric': {'__name__': 'my_app_cpu_usage_total', 'host': 'server1', 'region': 'us-east-1'},
        'values': [9.5],
        'timestamps': [current_time_ms]
    },
    {
        'metric': {'__name__': 'my_app_memory_utilization_bytes', 'host': 'server1', 'region': 'us-east-1'},
        'values': [1024 * 1024 * 500],
        'timestamps': [current_time_ms]
    }
]

try:
    # Send the data
    writer.send(data_points)
    print(f"Successfully sent {len(data_points)} data points to {REMOTE_WRITE_URL}")
except Exception as e:
    print(f"Failed to send data: {e}")

view raw JSON →