|
| 1 | +use opentelemetry_api::metrics::Unit; |
| 2 | +use opentelemetry_api::Key; |
| 3 | +use opentelemetry_api::{metrics::MeterProvider as _, KeyValue}; |
| 4 | +use opentelemetry_sdk::metrics::{Instrument, MeterProvider, PeriodicReader, Stream}; |
| 5 | +use opentelemetry_sdk::{runtime, Resource}; |
| 6 | +use std::error::Error; |
| 7 | + |
| 8 | +fn init_meter_provider() -> MeterProvider { |
| 9 | + // for example 1 |
| 10 | + let my_view_rename_and_unit = |i: &Instrument| { |
| 11 | + if i.name == "my_histogram" { |
| 12 | + Some( |
| 13 | + Stream::new() |
| 14 | + .name("my_histogram_renamed") |
| 15 | + .unit(Unit::new("milliseconds")), |
| 16 | + ) |
| 17 | + } else { |
| 18 | + None |
| 19 | + } |
| 20 | + }; |
| 21 | + |
| 22 | + // for example 2 |
| 23 | + let my_view_drop_attributes = |i: &Instrument| { |
| 24 | + if i.name == "my_counter" { |
| 25 | + Some(Stream::new().allowed_attribute_keys(vec![Key::from("mykey1")])) |
| 26 | + } else { |
| 27 | + None |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + let exporter = opentelemetry_stdout::MetricsExporter::default(); |
| 32 | + let reader = PeriodicReader::builder(exporter, runtime::Tokio).build(); |
| 33 | + MeterProvider::builder() |
| 34 | + .with_reader(reader) |
| 35 | + .with_resource(Resource::new(vec![KeyValue::new( |
| 36 | + "service.name", |
| 37 | + "metrics-advanced-example", |
| 38 | + )])) |
| 39 | + .with_view(my_view_rename_and_unit) |
| 40 | + .with_view(my_view_drop_attributes) |
| 41 | + .build() |
| 42 | +} |
| 43 | + |
| 44 | +#[tokio::main] |
| 45 | +async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { |
| 46 | + let meter_provider = init_meter_provider(); |
| 47 | + let meter = meter_provider.meter("mylibraryname"); |
| 48 | + |
| 49 | + // Example 1 - Rename metric using View. |
| 50 | + // This instrument will be renamed to "my_histogram_renamed", |
| 51 | + // and its unit changed to "milliseconds" |
| 52 | + // using view. |
| 53 | + let histogram = meter |
| 54 | + .f64_histogram("my_histogram") |
| 55 | + .with_unit(Unit::new("ms")) |
| 56 | + .with_description("My histogram example description") |
| 57 | + .init(); |
| 58 | + |
| 59 | + // Record measurements using the histogram instrument. |
| 60 | + histogram.record( |
| 61 | + 10.5, |
| 62 | + [ |
| 63 | + KeyValue::new("mykey1", "myvalue1"), |
| 64 | + KeyValue::new("mykey2", "myvalue2"), |
| 65 | + KeyValue::new("mykey3", "myvalue3"), |
| 66 | + KeyValue::new("mykey4", "myvalue4"), |
| 67 | + ] |
| 68 | + .as_ref(), |
| 69 | + ); |
| 70 | + |
| 71 | + // Example 2 - Drop unwanted attributes using view. |
| 72 | + let counter = meter.u64_counter("my_counter").init(); |
| 73 | + |
| 74 | + // Record measurements using the Counter instrument. |
| 75 | + // Though we are passing 4 attributes here, only 1 will be used |
| 76 | + // for aggregation as view is configured to use only "mykey1" |
| 77 | + // attribute. |
| 78 | + counter.add( |
| 79 | + 10, |
| 80 | + [ |
| 81 | + KeyValue::new("mykey1", "myvalue1"), |
| 82 | + KeyValue::new("mykey2", "myvalue2"), |
| 83 | + KeyValue::new("mykey3", "myvalue3"), |
| 84 | + KeyValue::new("mykey4", "myvalue4"), |
| 85 | + ] |
| 86 | + .as_ref(), |
| 87 | + ); |
| 88 | + |
| 89 | + // Metrics are exported by default every 30 seconds when using stdout exporter, |
| 90 | + // however shutting down the MeterProvider here instantly flushes |
| 91 | + // the metrics, instead of waiting for the 30 sec interval. |
| 92 | + meter_provider.shutdown()?; |
| 93 | + Ok(()) |
| 94 | +} |
0 commit comments