Using OpenTelemetry Exporter for Prometheus Remote Write
Use OpenTelemetry Prometheus Exporter package in Python applications to send data to Prometheus-compatible systems such as Last9
You can push custom metrics to Prometheus-compatible backends, such as Last9, 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 (
PrometheusRemoteWriteMetricsExporter,
)
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(
endpoint=sys.argv[1],
basic_auth={
"username": sys.argv[2],
"password": sys.argv[3],
},
headers={},
)
reader = PeriodicExportingMetricReader(exporter, 1000)
provider = MeterProvider(metric_readers=[reader])
metrics.set_meter_provider(provider)
meter = metrics.get_meter(__name__)
requests_counter = meter.create_counter(
name=sys.argv[4],
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.
- Remote Write URL
- Username
- Password
- Metric name
- Labels
A complete example is as follows:
python app.py 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.
The complete code example can be found here.