Skip to content

Commit cf86974

Browse files
committed
nits
1 parent 58d5e8e commit cf86974

File tree

4 files changed

+42
-45
lines changed

4 files changed

+42
-45
lines changed

examples/metrics-basic/src/main.rs

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use opentelemetry::{global, KeyValue};
2+
use opentelemetry_sdk::error::ShutdownError;
23
use opentelemetry_sdk::metrics::{PeriodicReader, SdkMeterProvider};
34
use opentelemetry_sdk::Resource;
45
use std::error::Error;
@@ -23,7 +24,7 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
2324
}
2425

2526
#[tokio::main]
26-
async fn main() {
27+
async fn main() -> Result<(), Box<dyn Error>> {
2728
// Initialize the MeterProvider with the stdout Exporter.
2829
let meter_provider = init_meter_provider();
2930

@@ -137,35 +138,41 @@ async fn main() {
137138
})
138139
.build();
139140

140-
// Metrics are exported by default every 30 seconds when using stdout exporter,
141-
// however shutting down the MeterProvider here instantly flushes
142-
// the metrics, instead of waiting for the 30 sec interval.
143-
let shutdown_result = meter_provider.shutdown();
144-
145-
// Demonstrate handling the shutdown result.
146-
match shutdown_result {
147-
Ok(_) => println!("MeterProvider shutdown successfully"),
148-
Err(e) => {
149-
match e {
150-
opentelemetry_sdk::error::ShutdownError::InternalFailure(e) => {
151-
// This indicates some failure during shutdown.
152-
// Not much to do here, but log the error.
153-
// So users at least know something went wrong,
154-
// and possibly explain why some metrics were not exported.
155-
println!("MeterProvider shutdown failed: {}", e)
156-
}
157-
opentelemetry_sdk::error::ShutdownError::AlreadyShutdown => {
158-
// This indicates some user code tried to shutdown elsewhere.
159-
// user need to review their code to ensure shutdown is called only once.
160-
println!("MeterProvider already shutdown")
161-
}
162-
opentelemetry_sdk::error::ShutdownError::Timeout(e) => {
163-
// This indicates the shutdown timed out, and a good
164-
// hint to user to increase the timeout or even retry.
165-
// (Shutdown method does not allow custom timeout today, but that is temporary)
166-
println!("MeterProvider shutdown timed out after {:?}", e)
167-
}
168-
}
169-
}
170-
}
141+
// Metrics are exported by default every 30 seconds when using stdout
142+
// exporter, however shutting down the MeterProvider here instantly flushes
143+
// the metrics, instead of waiting for the 30 sec interval. Shutdown returns
144+
// a result, which is bubbled up to the caller The commented code below
145+
// demonstrates handling the shutdown result, instead of bubbling up the
146+
// error.
147+
meter_provider.shutdown()?;
148+
149+
// let shutdown_result = meter_provider.shutdown();
150+
151+
// Handle the shutdown result.
152+
// match shutdown_result {
153+
// Ok(_) => println!("MeterProvider shutdown successfully"),
154+
// Err(e) => {
155+
// match e {
156+
// opentelemetry_sdk::error::ShutdownError::InternalFailure(message) => {
157+
// // This indicates some internal failure during shutdown. The
158+
// // error message is intended for logging purposes only and
159+
// // should not be used to make programmatic decisions.
160+
// println!("MeterProvider shutdown failed: {}", message)
161+
// }
162+
// opentelemetry_sdk::error::ShutdownError::AlreadyShutdown => {
163+
// // This indicates some user code tried to shutdown
164+
// // elsewhere. user need to review their code to ensure
165+
// // shutdown is called only once.
166+
// println!("MeterProvider already shutdown")
167+
// }
168+
// opentelemetry_sdk::error::ShutdownError::Timeout(e) => {
169+
// // This indicates the shutdown timed out, and a good hint to
170+
// // user to increase the timeout. (Shutdown method does not
171+
// // allow custom timeout today, but that is temporary)
172+
// println!("MeterProvider shutdown timed out after {:?}", e)
173+
// }
174+
// }
175+
// }
176+
// }
177+
Ok(())
171178
}

opentelemetry-otlp/src/exporter/http/metrics.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,7 @@ impl MetricsClient for OtlpHttpClient {
4747
fn shutdown(&self) -> ShutdownResult {
4848
self.client
4949
.lock()
50-
.map_err(|e| {
51-
ShutdownError::InternalFailure(format!(
52-
"Internal Error. Failed to acquire lock: {}",
53-
e
54-
))
55-
})?
50+
.map_err(|e| ShutdownError::InternalFailure(format!("Failed to acquire lock: {}", e)))?
5651
.take();
5752

5853
Ok(())

opentelemetry-otlp/src/exporter/tonic/metrics.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,7 @@ impl MetricsClient for TonicMetricsClient {
9393
fn shutdown(&self) -> ShutdownResult {
9494
self.inner
9595
.lock()
96-
.map_err(|e| {
97-
ShutdownError::InternalFailure(format!(
98-
"Internal Error. Failed to acquire lock: {}",
99-
e
100-
))
101-
})?
96+
.map_err(|e| ShutdownError::InternalFailure(format!("Failed to acquire lock: {}", e)))?
10297
.take();
10398

10499
Ok(())

opentelemetry-sdk/src/metrics/manual_reader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl MetricReader for ManualReader {
112112
/// Closes any connections and frees any resources used by the reader.
113113
fn shutdown(&self) -> ShutdownResult {
114114
let mut inner = self.inner.lock().map_err(|e| {
115-
ShutdownError::InternalFailure(format!("Internal Error. Failed to acquire lock: {}", e))
115+
ShutdownError::InternalFailure(format!("Failed to acquire lock: {}", e))
116116
})?;
117117

118118
// Any future call to collect will now return an error.

0 commit comments

Comments
 (0)