#prometheus #encoder #json #text #docs #latest #json-encoder

prometheus-json-encoder

JSON encoder implementation for the prometheus crate : https://docs.rs/prometheus/latest/prometheus/

1 unstable release

0.1.1 Jul 15, 2022
0.1.0 Jul 15, 2022

#74 in #latest

Apache-2.0

17KB
266 lines

prometheus-json-encoder

Implements a JsonEncoder object for prometheus 0.13.0.

Basic example

use opentelemetry::{global, KeyValue, sdk::Resource};
use opentelemetry_prometheus::PrometheusExporter;
use prometheus_json_encoder::JsonEncoder;

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

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

    // Use two instruments
    let counter = meter
        .u64_counter("a.counter")
        .with_description("Counts things")
        .init();
    let recorder = meter
        .i64_value_recorder("a.value_recorder")
        .with_description("Records values")
        .init();

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

    // Encode data as text or protobuf
    let encoder = JsonEncoder::new();
    let metric_families = exporter.registry().gather();
    let mut result = Vec::new();
    encoder.encode(&metric_families, &mut result);
    println!("format :\n{}", encoder.format_type());
    println!("{:?}", result);
    let test = std::str::from_utf8(&result).unwrap();
    println!("{}", test)
    
}


Dependencies

~3.5–8MB
~93K SLoC