|
| 1 | +use opentelemetry_api::metrics::Unit; |
| 2 | +use opentelemetry_api::{metrics::MeterProvider as _, Context, KeyValue}; |
| 3 | +use opentelemetry_sdk::metrics::{MeterProvider, PeriodicReader}; |
| 4 | +use opentelemetry_sdk::{runtime, Resource}; |
| 5 | +use std::error::Error; |
| 6 | + |
| 7 | +fn init_meter_provider() -> MeterProvider { |
| 8 | + let exporter = opentelemetry_stdout::MetricsExporter::default(); |
| 9 | + let reader = PeriodicReader::builder(exporter, runtime::Tokio).build(); |
| 10 | + MeterProvider::builder() |
| 11 | + .with_reader(reader) |
| 12 | + .with_resource(Resource::new(vec![KeyValue::new( |
| 13 | + "service.name", |
| 14 | + "metrics-basic-example", |
| 15 | + )])) |
| 16 | + .build() |
| 17 | +} |
| 18 | + |
| 19 | +#[tokio::main] |
| 20 | +async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { |
| 21 | + // Initialize the MeterProvider with the stdout Exporter. |
| 22 | + let meter_provider = init_meter_provider(); |
| 23 | + |
| 24 | + // Create a meter from the above MeterProvider. |
| 25 | + let meter = meter_provider.meter("mylibraryname"); |
| 26 | + |
| 27 | + // Create a Counter Instrument. |
| 28 | + let counter = meter.u64_counter("my_counter").init(); |
| 29 | + |
| 30 | + // Record measurements using the Counter instrument. |
| 31 | + counter.add( |
| 32 | + &Context::current(), |
| 33 | + 10, |
| 34 | + [ |
| 35 | + KeyValue::new("mykey1", "myvalue1"), |
| 36 | + KeyValue::new("mykey2", "myvalue2"), |
| 37 | + ] |
| 38 | + .as_ref(), |
| 39 | + ); |
| 40 | + |
| 41 | + // Create a ObservableCounter instrument and register a callback that reports the measurement. |
| 42 | + let observable_counter = meter |
| 43 | + .u64_observable_counter("my_observable_counter") |
| 44 | + .with_description("My observable counter example description") |
| 45 | + .with_unit(Unit::new("myunit")) |
| 46 | + .init(); |
| 47 | + |
| 48 | + meter.register_callback(&[observable_counter.as_any()], move |observer| { |
| 49 | + observer.observe_u64( |
| 50 | + &observable_counter, |
| 51 | + 100, |
| 52 | + [ |
| 53 | + KeyValue::new("mykey1", "myvalue1"), |
| 54 | + KeyValue::new("mykey2", "myvalue2"), |
| 55 | + ] |
| 56 | + .as_ref(), |
| 57 | + ) |
| 58 | + })?; |
| 59 | + |
| 60 | + // Create a UpCounter Instrument. |
| 61 | + let updown_counter = meter.i64_up_down_counter("my_updown_counter").init(); |
| 62 | + |
| 63 | + // Record measurements using the UpCounter instrument. |
| 64 | + updown_counter.add( |
| 65 | + &Context::current(), |
| 66 | + -10, |
| 67 | + [ |
| 68 | + KeyValue::new("mykey1", "myvalue1"), |
| 69 | + KeyValue::new("mykey2", "myvalue2"), |
| 70 | + ] |
| 71 | + .as_ref(), |
| 72 | + ); |
| 73 | + |
| 74 | + // Create a Observable UpDownCounter instrument and register a callback that reports the measurement. |
| 75 | + let observable_up_down_counter = meter |
| 76 | + .i64_observable_up_down_counter("my_observable_updown_counter") |
| 77 | + .with_description("My observable updown counter example description") |
| 78 | + .with_unit(Unit::new("myunit")) |
| 79 | + .init(); |
| 80 | + |
| 81 | + meter.register_callback(&[observable_up_down_counter.as_any()], move |observer| { |
| 82 | + observer.observe_i64( |
| 83 | + &observable_up_down_counter, |
| 84 | + 100, |
| 85 | + [ |
| 86 | + KeyValue::new("mykey1", "myvalue1"), |
| 87 | + KeyValue::new("mykey2", "myvalue2"), |
| 88 | + ] |
| 89 | + .as_ref(), |
| 90 | + ) |
| 91 | + })?; |
| 92 | + |
| 93 | + // Create a Histogram Instrument. |
| 94 | + let histogram = meter |
| 95 | + .f64_histogram("my_histogram") |
| 96 | + .with_description("My histogram example description") |
| 97 | + .init(); |
| 98 | + |
| 99 | + // Record measurements using the histogram instrument. |
| 100 | + histogram.record( |
| 101 | + &Context::current(), |
| 102 | + 10.5, |
| 103 | + [ |
| 104 | + KeyValue::new("mykey1", "myvalue1"), |
| 105 | + KeyValue::new("mykey2", "myvalue2"), |
| 106 | + ] |
| 107 | + .as_ref(), |
| 108 | + ); |
| 109 | + |
| 110 | + // Note that there is no ObservableHistogram instruments. |
| 111 | + |
| 112 | + // Create a ObservableGauge instrument and register a callback that reports the measurement. |
| 113 | + let gauge = meter |
| 114 | + .f64_observable_gauge("my_gauge") |
| 115 | + .with_description("A gauge set to 1.0") |
| 116 | + .with_unit(Unit::new("myunit")) |
| 117 | + .init(); |
| 118 | + |
| 119 | + // Register a callback that reports the measurement. |
| 120 | + meter.register_callback(&[gauge.as_any()], move |observer| { |
| 121 | + observer.observe_f64( |
| 122 | + &gauge, |
| 123 | + 1.0, |
| 124 | + [ |
| 125 | + KeyValue::new("mykey1", "myvalue1"), |
| 126 | + KeyValue::new("mykey2", "myvalue2"), |
| 127 | + ] |
| 128 | + .as_ref(), |
| 129 | + ) |
| 130 | + })?; |
| 131 | + |
| 132 | + // Note that Gauge only has a Observable version. |
| 133 | + |
| 134 | + // Metrics are exported by default every 30 seconds, |
| 135 | + // however shutting down the MeterProvider here instantly flushes |
| 136 | + // the metrics, instead of waiting for the 30 sec interval. |
| 137 | + meter_provider.shutdown()?; |
| 138 | + Ok(()) |
| 139 | +} |
0 commit comments