Expand description

OpenTelemetry Prometheus Exporter

Prometheus Exporter Example

use opentelemetry::{global, KeyValue, sdk::Resource};
use opentelemetry_prometheus::PrometheusExporter;
use prometheus::{TextEncoder, Encoder};

fn init_meter() -> PrometheusExporter {
        .with_resource(Resource::new(vec![KeyValue::new("R", "V")]))

let exporter = init_meter();
let meter = global::meter("my-app");

// Use two instruments
let counter = meter
    .with_description("Counts things")
let recorder = meter
    .with_description("Records values")

counter.add(100, &[KeyValue::new("key", "value")]);
recorder.record(100, &[KeyValue::new("key", "value")]);

// Encode data as text or protobuf
let encoder = TextEncoder::new();
let metric_families = exporter.registry().gather();
let mut result = Vec::new();
encoder.encode(&metric_families, &mut result);

// result now contains encoded metrics:
// # HELP a_counter Counts things
// # TYPE a_counter counter
// a_counter{R="V",key="value"} 100
// # HELP a_value_recorder Records values
// # TYPE a_value_recorder histogram
// a_value_recorder_bucket{R="V",key="value",le="0.5"} 0
// a_value_recorder_bucket{R="V",key="value",le="0.9"} 0
// a_value_recorder_bucket{R="V",key="value",le="0.99"} 0
// a_value_recorder_bucket{R="V",key="value",le="+Inf"} 1
// a_value_recorder_sum{R="V",key="value"} 100
// a_value_recorder_count{R="V",key="value"} 1


Configuration for the prometheus exporter.

An implementation of metrics::Exporter that sends metrics to Prometheus.

An implementation of an Encoder that converts a MetricFamily proto message into text format.


An interface for encoding metric families into an underlying wire protocol.


Create a new prometheus exporter builder.