Skip to content

Commit 05aac62

Browse files
committed
MeterProvider to return specific MetricError
1 parent 80629c8 commit 05aac62

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

opentelemetry-sdk/src/metrics/error.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ pub enum MetricError {
1717
/// Invalid configuration
1818
#[error("Config error {0}")]
1919
Config(String),
20+
/// Shutdown already invoked
21+
#[error("Shutdown already invoked")]
22+
AlreadyShutdown,
23+
/// Shutdown failed due to timeout exceeding
24+
#[error("Shutdown failed due to timeout exceeding")]
25+
ShutdownTimeout,
2026
/// Fail to export metrics
2127
#[error("Metrics exporter {0} failed with {name}", name = .0.exporter_name())]
2228
ExportErr(Box<dyn ExportError>),
@@ -27,6 +33,25 @@ pub enum MetricError {
2733
InvalidInstrumentConfiguration(&'static str),
2834
}
2935

36+
impl PartialEq for MetricError {
37+
fn eq(&self, other: &Self) -> bool {
38+
match (self, other) {
39+
(MetricError::Other(a), MetricError::Other(b)) => a == b,
40+
(MetricError::Config(a), MetricError::Config(b)) => a == b,
41+
(MetricError::AlreadyShutdown, MetricError::AlreadyShutdown) => true,
42+
(MetricError::ShutdownTimeout, MetricError::ShutdownTimeout) => true,
43+
(MetricError::ExportErr(a), MetricError::ExportErr(b)) => {
44+
a.exporter_name() == b.exporter_name()
45+
}
46+
(
47+
MetricError::InvalidInstrumentConfiguration(a),
48+
MetricError::InvalidInstrumentConfiguration(b),
49+
) => a == b,
50+
_ => false,
51+
}
52+
}
53+
}
54+
3055
impl<T: ExportError> From<T> for MetricError {
3156
fn from(err: T) -> Self {
3257
MetricError::ExportErr(Box::new(err))

opentelemetry-sdk/src/metrics/meter_provider.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ impl SdkMeterProviderInner {
123123
.shutdown_invoked
124124
.load(std::sync::atomic::Ordering::Relaxed)
125125
{
126-
Err(MetricError::Other(
127-
"Cannot perform flush as MeterProvider shutdown already invoked.".into(),
128-
))
126+
Err(MetricError::AlreadyShutdown)
129127
} else {
130128
self.pipes.force_flush()
131129
}
@@ -137,9 +135,7 @@ impl SdkMeterProviderInner {
137135
.swap(true, std::sync::atomic::Ordering::SeqCst)
138136
{
139137
// If the previous value was true, shutdown was already invoked.
140-
Err(MetricError::Other(
141-
"MeterProvider shutdown already invoked.".into(),
142-
))
138+
Err(MetricError::AlreadyShutdown)
143139
} else {
144140
self.pipes.shutdown()
145141
}

opentelemetry-sdk/src/metrics/periodic_reader.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,14 +408,19 @@ impl PeriodicReaderInner {
408408
.send(Message::Shutdown(response_tx))
409409
.map_err(|e| MetricError::Other(e.to_string()))?;
410410

411-
if let Ok(response) = response_rx.recv() {
412-
if response {
413-
Ok(())
414-
} else {
411+
// TODO: accept timeout from caller.
412+
match response_rx.recv_timeout(Duration::from_secs(5)) {
413+
Ok(response) => {
414+
if response {
415+
Ok(())
416+
} else {
417+
Err(MetricError::Other("Failed to shutdown".into()))
418+
}
419+
}
420+
Err(mpsc::RecvTimeoutError::Timeout) => Err(MetricError::ShutdownTimeout),
421+
Err(mpsc::RecvTimeoutError::Disconnected) => {
415422
Err(MetricError::Other("Failed to shutdown".into()))
416423
}
417-
} else {
418-
Err(MetricError::Other("Failed to shutdown".into()))
419424
}
420425
}
421426
}
@@ -573,10 +578,12 @@ mod tests {
573578
// calling shutdown again should return Err
574579
let result = meter_provider.shutdown();
575580
assert!(result.is_err());
581+
assert_eq!(result.unwrap_err(), MetricError::AlreadyShutdown);
576582

577583
// calling shutdown again should return Err
578584
let result = meter_provider.shutdown();
579585
assert!(result.is_err());
586+
assert_eq!(result.unwrap_err(), MetricError::AlreadyShutdown);
580587
}
581588

582589
#[test]
@@ -598,6 +605,7 @@ mod tests {
598605
// calling force_flush after shutdown should return Err
599606
let result = meter_provider.force_flush();
600607
assert!(result.is_err());
608+
assert_eq!(result.unwrap_err(), MetricError::AlreadyShutdown);
601609
}
602610

603611
#[test]

0 commit comments

Comments
 (0)