Skip to main content

Using OpenTelemetry Exporter for Prometheus Remote Write

Use OpenTelemetry Prometheus Exporter package in Python applications to send data to Prometheus-compatible systems such as Levitate

You can push custom metrics to Prometheus-compatible backends, such as Levitate, using OpenTelemetry Exporter for Prometheus Remote Write.

This tutorial shows how to use the Otel Exporter with a Python application to send metrics to a Prometheus-compatible backend.

Here is a sample Python application that includes the package for Otel Expoter for Remote Write.

#!/usr/bin/env python

import logging
import random
import sys
import time
import json
from logging import INFO

from opentelemetry import metrics
from opentelemetry.exporter.prometheus_remote_write import (
from opentelemetry.metrics import Observation
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader

logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logger = logging.getLogger(__name__)

exporter = PrometheusRemoteWriteMetricsExporter(
"username": sys.argv[2],
"password": sys.argv[3],
reader = PeriodicExportingMetricReader(exporter, 1000)
provider = MeterProvider(metric_readers=[reader])
meter = metrics.get_meter(__name__)

requests_counter = meter.create_counter(
description="number of requests",

testing_labels = json.loads(sys.argv[5])

num = random.randint(0, 1000)
requests_counter.add(num % 131 + 200, testing_labels)
logger.log(level=INFO, msg="completed metrics collection cycle")

The script accepts the following CLI arguments.

  1. Remote Write URL
  2. Username
  3. Password
  4. Metric name
  5. Labels

A complete example is as follows:

python LEVITATE_REMOTE_WRITE_URL LEVITATE_CLUSTER_USERNAME LEVITATE_CLUSTER_PASSWORD 'my_metric' '{"environment": "staging", "label": "9999"}'

This will insert the metric into the Remote Write backend, and we can confirm this by looking into Grafana.

Sample metrics

The complete code example can be found here.