Skip to content

Commit c4377ac

Browse files
authored
Merge branch 'main' into anujnegi/disable-name-check
2 parents 1099de4 + a1860eb commit c4377ac

File tree

29 files changed

+503
-334
lines changed

29 files changed

+503
-334
lines changed

examples/metrics-basic/src/main.rs

Lines changed: 37 additions & 4 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() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
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,9 +138,41 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
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.
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.
143147
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+
// }
144177
Ok(())
145178
}

opentelemetry-http/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Bump msrv to 1.75.0.
66
- Add "internal-logs" feature flag (enabled by default), and emit internal logs.
7+
- Add `HttpClient::send_bytes` with `bytes::Bytes` request payload and deprecate old `HttpClient::send` function.
78

89
## 0.27.0
910

opentelemetry-http/src/lib.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,24 @@ pub type HttpError = Box<dyn std::error::Error + Send + Sync + 'static>;
5555
/// users to bring their choice of HTTP client.
5656
#[async_trait]
5757
pub trait HttpClient: Debug + Send + Sync {
58-
/// Send the specified HTTP request
58+
/// Send the specified HTTP request with `Vec<u8>` payload
5959
///
6060
/// Returns the HTTP response including the status code and body.
6161
///
6262
/// Returns an error if it can't connect to the server or the request could not be completed,
6363
/// e.g. because of a timeout, infinite redirects, or a loss of connection.
64-
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError>;
64+
#[deprecated(note = "Use `send_bytes` with `Bytes` payload instead.")]
65+
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
66+
self.send_bytes(request.map(Into::into)).await
67+
}
68+
69+
/// Send the specified HTTP request with `Bytes` payload.
70+
///
71+
/// Returns the HTTP response including the status code and body.
72+
///
73+
/// Returns an error if it can't connect to the server or the request could not be completed,
74+
/// e.g. because of a timeout, infinite redirects, or a loss of connection.
75+
async fn send_bytes(&self, request: Request<Bytes>) -> Result<Response<Bytes>, HttpError>;
6576
}
6677

6778
#[cfg(feature = "reqwest")]
@@ -72,7 +83,7 @@ mod reqwest {
7283

7384
#[async_trait]
7485
impl HttpClient for reqwest::Client {
75-
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
86+
async fn send_bytes(&self, request: Request<Bytes>) -> Result<Response<Bytes>, HttpError> {
7687
otel_debug!(name: "ReqwestClient.Send");
7788
let request = request.try_into()?;
7889
let mut response = self.execute(request).await?.error_for_status()?;
@@ -89,7 +100,7 @@ mod reqwest {
89100
#[cfg(not(target_arch = "wasm32"))]
90101
#[async_trait]
91102
impl HttpClient for reqwest::blocking::Client {
92-
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
103+
async fn send_bytes(&self, request: Request<Bytes>) -> Result<Response<Bytes>, HttpError> {
93104
otel_debug!(name: "ReqwestBlockingClient.Send");
94105
let request = request.try_into()?;
95106
let mut response = self.execute(request)?.error_for_status()?;
@@ -159,7 +170,7 @@ pub mod hyper {
159170

160171
#[async_trait]
161172
impl HttpClient for HyperClient {
162-
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
173+
async fn send_bytes(&self, request: Request<Bytes>) -> Result<Response<Bytes>, HttpError> {
163174
otel_debug!(name: "HyperClient.Send");
164175
let (parts, body) = request.into_parts();
165176
let mut request = Request::from_parts(parts, Body(Full::from(body)));

opentelemetry-otlp/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"reqwest-blocking-client" features as default, to align with the
1111
specification.
1212
[2516](https://github.com/open-telemetry/opentelemetry-rust/pull/2516)
13+
- Remove unnecessarily public trait `opentelemetry_otlp::metrics::MetricsClient`
14+
and `MetricExporter::new(..)` method. Use
15+
`MetricExporter::builder()...build()` to obtain `MetricExporter`.
1316

1417
## 0.27.0
1518

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl LogExporter for OtlpHttpClient {
2828
.method(Method::POST)
2929
.uri(&self.collector_endpoint)
3030
.header(CONTENT_TYPE, content_type)
31-
.body(body)
31+
.body(body.into())
3232
.map_err(|e| crate::Error::RequestFailed(Box::new(e)))?;
3333

3434
for (k, v) in &self.headers {
@@ -37,7 +37,7 @@ impl LogExporter for OtlpHttpClient {
3737

3838
let request_uri = request.uri().to_string();
3939
otel_debug!(name: "HttpLogsClient.CallingExport");
40-
let response = client.send(request).await?;
40+
let response = client.send_bytes(request).await?;
4141

4242
if !response.status().is_success() {
4343
let error = format!(

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

Lines changed: 8 additions & 4 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

@@ -27,7 +28,7 @@ impl MetricsClient for OtlpHttpClient {
2728
.method(Method::POST)
2829
.uri(&self.collector_endpoint)
2930
.header(CONTENT_TYPE, content_type)
30-
.body(body)
31+
.body(body.into())
3132
.map_err(|e| crate::Error::RequestFailed(Box::new(e)))?;
3233

3334
for (k, v) in &self.headers {
@@ -36,15 +37,18 @@ impl MetricsClient for OtlpHttpClient {
3637

3738
otel_debug!(name: "HttpMetricsClient.CallingExport");
3839
client
39-
.send(request)
40+
.send_bytes(request)
4041
.await
4142
.map_err(|e| MetricError::ExportErr(Box::new(Error::RequestFailed(e))))?;
4243

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| ShutdownError::InternalFailure(format!("Failed to acquire lock: {}", e)))?
51+
.take();
4852

4953
Ok(())
5054
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl SpanExporter for OtlpHttpClient {
3030
.method(Method::POST)
3131
.uri(&self.collector_endpoint)
3232
.header(CONTENT_TYPE, content_type)
33-
.body(body)
33+
.body(body.into())
3434
{
3535
Ok(req) => req,
3636
Err(e) => {
@@ -48,7 +48,7 @@ impl SpanExporter for OtlpHttpClient {
4848
Box::pin(async move {
4949
let request_uri = request.uri().to_string();
5050
otel_debug!(name: "HttpTracesClient.CallingExport");
51-
let response = client.send(request).await?;
51+
let response = client.send_bytes(request).await?;
5252

5353
if !response.status().is_success() {
5454
let error = format!(

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

Lines changed: 6 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,11 @@ 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| ShutdownError::InternalFailure(format!("Failed to acquire lock: {}", e)))?
97+
.take();
9498

9599
Ok(())
96100
}

opentelemetry-otlp/src/metric.rs

Lines changed: 5 additions & 4 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::{
@@ -121,9 +122,9 @@ impl HasHttpConfig for MetricExporterBuilder<HttpExporterBuilderSet> {
121122

122123
/// An interface for OTLP metrics clients
123124
#[async_trait]
124-
pub trait MetricsClient: fmt::Debug + Send + Sync + 'static {
125+
pub(crate) 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

@@ -165,7 +166,7 @@ impl MetricExporter {
165166
}
166167

167168
/// Create a new metrics exporter
168-
pub fn new(client: impl MetricsClient, temporality: Temporality) -> MetricExporter {
169+
pub(crate) fn new(client: impl MetricsClient, temporality: Temporality) -> MetricExporter {
169170
MetricExporter {
170171
client: Box::new(client),
171172
temporality,

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

0 commit comments

Comments
 (0)