Crates.io | prometheus-json-encoder |
lib.rs | prometheus-json-encoder |
version | 0.1.1 |
source | src |
created_at | 2022-07-15 14:42:24.810923 |
updated_at | 2022-07-15 14:59:31.511552 |
description | JSON encoder implementation for the prometheus crate : https://docs.rs/prometheus/latest/prometheus/ |
homepage | |
repository | |
max_upload_size | |
id | 626252 |
size | 16,427 |
Implements a JsonEncoder
object for prometheus 0.13.0.
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)
}