Crate opentelemetry_prometheus

source ·
Expand description

An OpenTelemetry exporter for Prometheus metrics.

The development of prometheus exporter has halt until the Opentelemetry metrics API and SDK reaches 1.0. Current implementation is based on Opentelemetry API and SDK 0.23.
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")
    .init();
let histogram = meter
    .u64_histogram("a.histogram")
    .with_description("Records values")
    .init();

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§

Enums§

  • ResourceSelector is used to select which resource to export with every metrics.

Functions§