Skip to content

Commit 0a0d9ee

Browse files
committed
Use dedicated ShutdownResult for Metric SDK shutdown
1 parent 8520610 commit 0a0d9ee

File tree

16 files changed

+114
-57
lines changed

16 files changed

+114
-57
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::sync::Arc;
33
use async_trait::async_trait;
44
use http::{header::CONTENT_TYPE, Method};
55
use opentelemetry::otel_debug;
6+
use opentelemetry_sdk::error::{ShutdownError, ShutdownResult};
67
use opentelemetry_sdk::metrics::data::ResourceMetrics;
78
use opentelemetry_sdk::metrics::{MetricError, MetricResult};
89

@@ -43,8 +44,13 @@ impl MetricsClient for OtlpHttpClient {
4344
Ok(())
4445
}
4546

46-
fn shutdown(&self) -> MetricResult<()> {
47-
let _ = self.client.lock()?.take();
47+
fn shutdown(&self) -> ShutdownResult {
48+
self.client
49+
.lock()
50+
.map_err(|e| {
51+
ShutdownError::Failed(format!("Internal Error. Failed to acquire lock: {}", e))
52+
})?
53+
.take();
4854

4955
Ok(())
5056
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use opentelemetry::otel_debug;
66
use opentelemetry_proto::tonic::collector::metrics::v1::{
77
metrics_service_client::MetricsServiceClient, ExportMetricsServiceRequest,
88
};
9+
use opentelemetry_sdk::error::{ShutdownError, ShutdownResult};
910
use opentelemetry_sdk::metrics::data::ResourceMetrics;
1011
use opentelemetry_sdk::metrics::{MetricError, MetricResult};
1112
use tonic::{codegen::CompressionEncoding, service::Interceptor, transport::Channel, Request};
@@ -89,8 +90,13 @@ impl MetricsClient for TonicMetricsClient {
8990
Ok(())
9091
}
9192

92-
fn shutdown(&self) -> MetricResult<()> {
93-
let _ = self.inner.lock()?.take();
93+
fn shutdown(&self) -> ShutdownResult {
94+
self.inner
95+
.lock()
96+
.map_err(|e| {
97+
ShutdownError::Failed(format!("Internal Error. Failed to acquire lock: {}", e))
98+
})?
99+
.take();
94100

95101
Ok(())
96102
}

opentelemetry-otlp/src/metric.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::NoExporterBuilderSet;
1616

1717
use async_trait::async_trait;
1818
use core::fmt;
19+
use opentelemetry_sdk::error::ShutdownResult;
1920
use opentelemetry_sdk::metrics::MetricResult;
2021

2122
use opentelemetry_sdk::metrics::{
@@ -123,7 +124,7 @@ impl HasHttpConfig for MetricExporterBuilder<HttpExporterBuilderSet> {
123124
#[async_trait]
124125
pub trait MetricsClient: fmt::Debug + Send + Sync + 'static {
125126
async fn export(&self, metrics: &mut ResourceMetrics) -> MetricResult<()>;
126-
fn shutdown(&self) -> MetricResult<()>;
127+
fn shutdown(&self) -> ShutdownResult;
127128
}
128129

129130
/// Export metrics in OTEL format.
@@ -149,7 +150,7 @@ impl PushMetricExporter for MetricExporter {
149150
Ok(())
150151
}
151152

152-
fn shutdown(&self) -> MetricResult<()> {
153+
fn shutdown(&self) -> ShutdownResult {
153154
self.client.shutdown()
154155
}
155156

opentelemetry-sdk/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ temp-env = { workspace = true }
4242
pprof = { version = "0.14", features = ["flamegraph", "criterion"] }
4343

4444
[features]
45-
default = ["trace", "metrics", "logs", "internal-logs"]
45+
default = ["trace", "metrics", "logs", "internal-logs", "experimental_metrics_periodicreader_with_async_runtime"]
4646
trace = ["opentelemetry/trace", "rand", "percent-encoding"]
4747
jaeger_remote_sampler = ["trace", "opentelemetry-http", "http", "serde", "serde_json", "url"]
4848
logs = ["opentelemetry/logs", "serde_json"]

opentelemetry-sdk/benches/metric.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use opentelemetry::{
77
Key, KeyValue,
88
};
99
use opentelemetry_sdk::{
10+
error::ShutdownResult,
1011
metrics::{
1112
data::ResourceMetrics, new_view, reader::MetricReader, Aggregation, Instrument,
1213
InstrumentKind, ManualReader, MetricResult, Pipeline, SdkMeterProvider, Stream,
@@ -31,7 +32,7 @@ impl MetricReader for SharedReader {
3132
self.0.force_flush()
3233
}
3334

34-
fn shutdown(&self) -> MetricResult<()> {
35+
fn shutdown(&self) -> ShutdownResult {
3536
self.0.shutdown()
3637
}
3738

opentelemetry-sdk/src/error.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
11
//! Wrapper for error from trace, logs and metrics part of open telemetry.
22
3+
use std::{result::Result, time::Duration};
4+
5+
use thiserror::Error;
6+
37
/// Trait for errors returned by exporters
48
pub trait ExportError: std::error::Error + Send + Sync + 'static {
59
/// The name of exporter that returned this error
610
fn exporter_name(&self) -> &'static str;
711
}
12+
13+
#[derive(Error, Debug)]
14+
/// Errors that can occur during shutdown.
15+
pub enum ShutdownError {
16+
/// Shutdown already invoked.
17+
#[error("Shutdown already invoked")]
18+
AlreadyShutdown,
19+
20+
/// Shutdown timed out before completing.
21+
#[error("Shutdown timed out after {0:?}")]
22+
Timeout(Duration),
23+
24+
/// Shutdown failed with an error.
25+
/// This error is returned when the shutdown process failed with an error.
26+
#[error("Shutdown failed: {0}")]
27+
Failed(String),
28+
}
29+
30+
/// A specialized `Result` type for Shutdown operations.
31+
pub type ShutdownResult = Result<(), ShutdownError>;

opentelemetry-sdk/src/metrics/exporter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Interfaces for exporting metrics
22
use async_trait::async_trait;
33

4+
use crate::error::ShutdownResult;
45
use crate::metrics::MetricResult;
56

67
use crate::metrics::data::ResourceMetrics;
@@ -27,7 +28,7 @@ pub trait PushMetricExporter: Send + Sync + 'static {
2728
///
2829
/// After Shutdown is called, calls to Export will perform no operation and
2930
/// instead will return an error indicating the shutdown state.
30-
fn shutdown(&self) -> MetricResult<()>;
31+
fn shutdown(&self) -> ShutdownResult;
3132

3233
/// Access the [Temporality] of the MetricExporter.
3334
fn temporality(&self) -> Temporality;

opentelemetry-sdk/src/metrics/in_memory_exporter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::error::ShutdownResult;
12
use crate::metrics::data::{self, Gauge, Sum};
23
use crate::metrics::data::{Histogram, Metric, ResourceMetrics, ScopeMetrics};
34
use crate::metrics::exporter::PushMetricExporter;
@@ -277,7 +278,7 @@ impl PushMetricExporter for InMemoryMetricExporter {
277278
Ok(()) // In this implementation, flush does nothing
278279
}
279280

280-
fn shutdown(&self) -> MetricResult<()> {
281+
fn shutdown(&self) -> ShutdownResult {
281282
Ok(())
282283
}
283284

opentelemetry-sdk/src/metrics/manual_reader.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use std::{
55

66
use opentelemetry::otel_debug;
77

8-
use crate::metrics::{MetricError, MetricResult, Temporality};
8+
use crate::{
9+
error::{ShutdownError, ShutdownResult},
10+
metrics::{MetricError, MetricResult, Temporality},
11+
};
912

1013
use super::{
1114
data::ResourceMetrics,
@@ -107,8 +110,10 @@ impl MetricReader for ManualReader {
107110
}
108111

109112
/// Closes any connections and frees any resources used by the reader.
110-
fn shutdown(&self) -> MetricResult<()> {
111-
let mut inner = self.inner.lock()?;
113+
fn shutdown(&self) -> ShutdownResult {
114+
let mut inner = self.inner.lock().map_err(|e| {
115+
ShutdownError::Failed(format!("Internal Error. Failed to acquire lock: {}", e))
116+
})?;
112117

113118
// Any future call to collect will now return an error.
114119
inner.sdk_producer = None;

opentelemetry-sdk/src/metrics/meter_provider.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ use opentelemetry::{
1212
otel_debug, otel_error, otel_info, InstrumentationScope,
1313
};
1414

15-
use crate::metrics::{MetricError, MetricResult};
1615
use crate::Resource;
16+
use crate::{
17+
error::ShutdownResult,
18+
metrics::{MetricError, MetricResult},
19+
};
1720

1821
use super::{
1922
meter::SdkMeter, noop::NoopMeter, pipeline::Pipelines, reader::MetricReader, view::View,
@@ -108,7 +111,7 @@ impl SdkMeterProvider {
108111
///
109112
/// There is no guaranteed that all telemetry be flushed or all resources have
110113
/// been released on error.
111-
pub fn shutdown(&self) -> MetricResult<()> {
114+
pub fn shutdown(&self) -> ShutdownResult {
112115
otel_info!(
113116
name: "MeterProvider.Shutdown",
114117
message = "User initiated shutdown of MeterProvider."
@@ -131,15 +134,13 @@ impl SdkMeterProviderInner {
131134
}
132135
}
133136

134-
fn shutdown(&self) -> MetricResult<()> {
137+
fn shutdown(&self) -> ShutdownResult {
135138
if self
136139
.shutdown_invoked
137140
.swap(true, std::sync::atomic::Ordering::SeqCst)
138141
{
139142
// If the previous value was true, shutdown was already invoked.
140-
Err(MetricError::Other(
141-
"MeterProvider shutdown already invoked.".into(),
142-
))
143+
Err(crate::error::ShutdownError::AlreadyShutdown)
143144
} else {
144145
self.pipes.shutdown()
145146
}

0 commit comments

Comments
 (0)