# 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)

  Bases: `MetricReader`

  Prometheus metric exporter for OpenTelemetry.

  shutdown(timeout_millis=30000, **kwargs)

    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:

- `OTEL_EXPORTER_PROMETHEUS_HOST` (default: “localhost”): The host to bind to

- `OTEL_EXPORTER_PROMETHEUS_PORT` (default: 9464): The port to bind to

## Limitations

- No multiprocessing support: The Prometheus exporter is not designed to operate in multiprocessing environments (see #3747).

## References

- Prometheus

- OpenTelemetry Project