Expand description
An OpenTelemetry exporter for Prometheus metrics.
Warning: This crate is no longer recommended for use.
Development of the Prometheus exporter has been discontinued. See the related [issue](https://github.com/open-telemetry/opentelemetry-rust/issues/2451). This crate depends on the unmaintained `protobuf` crate and has unresolved security vulnerabilities. Version 0.29 will be the final release.
Development of the Prometheus exporter has been discontinued. See the related [issue](https://github.com/open-telemetry/opentelemetry-rust/issues/2451). This crate depends on the unmaintained `protobuf` crate and has unresolved security vulnerabilities. Version 0.29 will be the final release.
For Prometheus integration, we strongly recommend using the OTLP exporter instead. Prometheus natively supports OTLP, providing a more stable and actively maintained solution.
use opentelemetry::{metrics::MeterProvider, KeyValue};
use opentelemetry_sdk::metrics::SdkMeterProvider;
use prometheus::{Encoder, TextEncoder};
// create a new prometheus registry
let registry = prometheus::Registry::new();
// configure OpenTelemetry to use this registry
let exporter = opentelemetry_prometheus::exporter()
.with_registry(registry.clone())
.build()?;
// set up a meter to create instruments
let provider = SdkMeterProvider::builder().with_reader(exporter).build();
let meter = provider.meter("my-app");
// Use two instruments
let counter = meter
.u64_counter("a.counter")
.with_description("Counts things")
.build();
let histogram = meter
.u64_histogram("a.histogram")
.with_description("Records values")
.build();
counter.add(100, &[KeyValue::new("key", "value")]);
histogram.record(100, &[KeyValue::new("key", "value")]);
// Encode data as text or protobuf
let encoder = TextEncoder::new();
let metric_families = registry.gather();
let mut result = Vec::new();
encoder.encode(&metric_families, &mut result)?;
// result now contains encoded metrics:
//
// # HELP a_counter_total Counts things
// # TYPE a_counter_total counter
// a_counter_total{key="value",otel_scope_name="my-app"} 100
// # HELP a_histogram Records values
// # TYPE a_histogram histogram
// a_histogram_bucket{key="value",otel_scope_name="my-app",le="0"} 0
// a_histogram_bucket{key="value",otel_scope_name="my-app",le="5"} 0
// a_histogram_bucket{key="value",otel_scope_name="my-app",le="10"} 0
// a_histogram_bucket{key="value",otel_scope_name="my-app",le="25"} 0
// a_histogram_bucket{key="value",otel_scope_name="my-app",le="50"} 0
// a_histogram_bucket{key="value",otel_scope_name="my-app",le="75"} 0
// a_histogram_bucket{key="value",otel_scope_name="my-app",le="100"} 1
// a_histogram_bucket{key="value",otel_scope_name="my-app",le="250"} 1
// a_histogram_bucket{key="value",otel_scope_name="my-app",le="500"} 1
// a_histogram_bucket{key="value",otel_scope_name="my-app",le="750"} 1
// a_histogram_bucket{key="value",otel_scope_name="my-app",le="1000"} 1
// a_histogram_bucket{key="value",otel_scope_name="my-app",le="2500"} 1
// a_histogram_bucket{key="value",otel_scope_name="my-app",le="5000"} 1
// a_histogram_bucket{key="value",otel_scope_name="my-app",le="7500"} 1
// a_histogram_bucket{key="value",otel_scope_name="my-app",le="10000"} 1
// a_histogram_bucket{key="value",otel_scope_name="my-app",le="+Inf"} 1
// a_histogram_sum{key="value",otel_scope_name="my-app"} 100
// a_histogram_count{key="value",otel_scope_name="my-app"} 1
// # HELP otel_scope_info Instrumentation Scope metadata
// # TYPE otel_scope_info gauge
// otel_scope_info{otel_scope_name="my-app"} 1
// # HELP target_info Target metadata
// # TYPE target_info gauge
// target_info{service_name="unknown_service"} 1
Structs§
- Exporter
Builder - PrometheusExporter configuration options
- Prometheus
Exporter - Prometheus metrics exporter
Enums§
- Resource
Selector ResourceSelector
is used to select which resource to export with every metrics.
Functions§
- exporter
- Creates a builder to configure a PrometheusExporter