A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://opentelemetry-python.readthedocs.io/en/latest/exporter/prometheus/prometheus.html below:

OpenTelemetry Prometheus Exporter — OpenTelemetry Python documentation

OpenTelemetry Python OpenTelemetry Prometheus Exporter

This library allows export of metrics data to Prometheus.

Usage

The OpenTelemetry Prometheus Exporter allows export of OpenTelemetry metrics to Prometheus.

from prometheus_client import start_http_server

from opentelemetry.exporter.prometheus import PrometheusMetricReader
from opentelemetry.metrics import get_meter_provider, set_meter_provider
from opentelemetry.sdk.metrics import MeterProvider

# Start Prometheus client
start_http_server(port=8000, addr="localhost")

# Exporter to export metrics to Prometheus
prefix = "MyAppPrefix"
reader = PrometheusMetricReader(prefix)

# Meter is responsible for creating and recording metrics
set_meter_provider(MeterProvider(metric_readers=[reader]))
meter = get_meter_provider().get_meter("myapp", "0.1.2")

counter = meter.create_counter(
    "requests",
    "requests",
    "number of requests",
)

# Labels are used to identify key-values that are associated with a specific
# metric that you want to record. These are useful for pre-aggregation and can
# be used to store custom dimensions pertaining to a metric
labels = {"environment": "staging"}

counter.add(25, labels)
input("Press any key to exit...")
API
class opentelemetry.exporter.prometheus.PrometheusMetricReader(disable_target_info=False)[source]

Bases: MetricReader

Prometheus metric exporter for OpenTelemetry.

shutdown(timeout_millis=30000, **kwargs)[source]

Shuts down the MetricReader. This method provides a way for the MetricReader to do any cleanup required. A metric reader can only be shutdown once, any subsequent calls are ignored and return failure status.

When a MetricReader is registered on a MeterProvider, shutdown() will invoke this automatically.

Return type:

None

Installation

The OpenTelemetry Prometheus Exporter package is available on PyPI:

pip install opentelemetry-exporter-prometheus
Usage

The Prometheus exporter starts an HTTP server that collects metrics and serializes them to Prometheus text format on request:

from prometheus_client import start_http_server

from opentelemetry import metrics
from opentelemetry.exporter.prometheus import PrometheusMetricReader
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.resources import SERVICE_NAME, Resource

# Service name is required for most backends
resource = Resource(attributes={
    SERVICE_NAME: "your-service-name"
})

# Start Prometheus client
start_http_server(port=9464, addr="localhost")
# Initialize PrometheusMetricReader which pulls metrics from the SDK
# on-demand to respond to scrape requests
reader = PrometheusMetricReader()
provider = MeterProvider(resource=resource, metric_readers=[reader])
metrics.set_meter_provider(provider)
Configuration

The following environment variables are supported:

References

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4