Skip to content

Commit bf7b4a0

Browse files
authored
Modify metrics example to shutdown without global shutdown method (#1739)
1 parent 736712d commit bf7b4a0

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

examples/metrics-basic/src/main.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use opentelemetry_sdk::metrics::{PeriodicReader, SdkMeterProvider};
55
use opentelemetry_sdk::{runtime, Resource};
66
use std::error::Error;
77

8-
fn init_meter_provider() {
8+
fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
99
let exporter = opentelemetry_stdout::MetricsExporterBuilder::default()
1010
// uncomment the below lines to pretty print output.
1111
// .with_encoder(|writer, data|
@@ -19,13 +19,14 @@ fn init_meter_provider() {
1919
"metrics-basic-example",
2020
)]))
2121
.build();
22-
global::set_meter_provider(provider);
22+
global::set_meter_provider(provider.clone());
23+
provider
2324
}
2425

2526
#[tokio::main]
2627
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
2728
// Initialize the MeterProvider with the stdout Exporter.
28-
init_meter_provider();
29+
let meter_provider = init_meter_provider();
2930

3031
// Create a meter from the above MeterProvider.
3132
let meter = global::meter("mylibraryname");
@@ -137,6 +138,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
137138
// Metrics are exported by default every 30 seconds when using stdout exporter,
138139
// however shutting down the MeterProvider here instantly flushes
139140
// the metrics, instead of waiting for the 30 sec interval.
140-
global::shutdown_meter_provider();
141+
// global::shutdown_meter_provider();
142+
meter_provider.shutdown()?;
141143
Ok(())
142144
}

0 commit comments

Comments
 (0)