Skip to content

Commit 175cb13

Browse files
gruebelcijothomas
authored andcommitted
leverage native async trait in MetricsClient (open-telemetry#2672)
1 parent f8c5da6 commit 175cb13

File tree

6 files changed

+46
-18
lines changed

6 files changed

+46
-18
lines changed

opentelemetry-otlp/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ all-features = true
2626
rustdoc-args = ["--cfg", "docsrs"]
2727

2828
[dependencies]
29-
async-trait = { workspace = true }
3029
futures-core = { workspace = true }
3130
opentelemetry = { version = "0.28", default-features = false, path = "../opentelemetry" }
3231
opentelemetry_sdk = { version = "0.28", default-features = false, path = "../opentelemetry-sdk" }

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
use std::sync::Arc;
22

33
use crate::metric::MetricsClient;
4-
use async_trait::async_trait;
54
use http::{header::CONTENT_TYPE, Method};
65
use opentelemetry::otel_debug;
76
use opentelemetry_sdk::error::{OTelSdkError, OTelSdkResult};
87
use opentelemetry_sdk::metrics::data::ResourceMetrics;
98

109
use super::OtlpHttpClient;
1110

12-
#[async_trait]
1311
impl MetricsClient for OtlpHttpClient {
1412
async fn export(&self, metrics: &mut ResourceMetrics) -> OTelSdkResult {
1513
let client = self

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl HttpExporterBuilder {
260260
OTEL_EXPORTER_OTLP_METRICS_HEADERS,
261261
)?;
262262

263-
Ok(crate::MetricExporter::new(client, temporality))
263+
Ok(crate::MetricExporter::from_http(client, temporality))
264264
}
265265
}
266266

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use core::fmt;
22
use std::sync::Mutex;
33

4-
use async_trait::async_trait;
54
use opentelemetry::otel_debug;
65
use opentelemetry_proto::tonic::collector::metrics::v1::{
76
metrics_service_client::MetricsServiceClient, ExportMetricsServiceRequest,
@@ -52,7 +51,6 @@ impl TonicMetricsClient {
5251
}
5352
}
5453

55-
#[async_trait]
5654
impl MetricsClient for TonicMetricsClient {
5755
async fn export(&self, metrics: &mut ResourceMetrics) -> OTelSdkResult {
5856
let (mut client, metadata, extensions) = self

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
pub(crate) mod logs;
2727

2828
#[cfg(feature = "metrics")]
29-
mod metrics;
29+
pub(crate) mod metrics;
3030

3131
#[cfg(feature = "trace")]
3232
pub(crate) mod trace;
@@ -302,7 +302,7 @@ impl TonicExporterBuilder {
302302

303303
let client = TonicMetricsClient::new(channel, interceptor, compression);
304304

305-
Ok(MetricExporter::new(client, temporality))
305+
Ok(MetricExporter::from_tonic(client, temporality))
306306
}
307307

308308
/// Build a new tonic span exporter

opentelemetry-otlp/src/metric.rs

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::{exporter::tonic::TonicExporterBuilder, HasTonicConfig, TonicExporter
1515

1616
use crate::NoExporterBuilderSet;
1717

18-
use async_trait::async_trait;
1918
use core::fmt;
2019
use opentelemetry_sdk::error::OTelSdkResult;
2120
use opentelemetry_sdk::metrics::{
@@ -120,18 +119,28 @@ impl HasHttpConfig for MetricExporterBuilder<HttpExporterBuilderSet> {
120119
}
121120

122121
/// An interface for OTLP metrics clients
123-
#[async_trait]
124122
pub(crate) trait MetricsClient: fmt::Debug + Send + Sync + 'static {
125-
async fn export(&self, metrics: &mut ResourceMetrics) -> OTelSdkResult;
123+
fn export(
124+
&self,
125+
metrics: &mut ResourceMetrics,
126+
) -> impl std::future::Future<Output = OTelSdkResult> + Send;
126127
fn shutdown(&self) -> OTelSdkResult;
127128
}
128129

129130
/// Export metrics in OTEL format.
130131
pub struct MetricExporter {
131-
client: Box<dyn MetricsClient>,
132+
client: SupportedTransportClient,
132133
temporality: Temporality,
133134
}
134135

136+
#[derive(Debug)]
137+
enum SupportedTransportClient {
138+
#[cfg(feature = "grpc-tonic")]
139+
Tonic(crate::exporter::tonic::metrics::TonicMetricsClient),
140+
#[cfg(any(feature = "http-proto", feature = "http-json"))]
141+
Http(crate::exporter::http::OtlpHttpClient),
142+
}
143+
135144
impl Debug for MetricExporter {
136145
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
137146
f.debug_struct("MetricExporter").finish()
@@ -140,7 +149,12 @@ impl Debug for MetricExporter {
140149

141150
impl PushMetricExporter for MetricExporter {
142151
async fn export(&self, metrics: &mut ResourceMetrics) -> OTelSdkResult {
143-
self.client.export(metrics).await
152+
match &self.client {
153+
#[cfg(feature = "grpc-tonic")]
154+
SupportedTransportClient::Tonic(client) => client.export(metrics).await,
155+
#[cfg(any(feature = "http-proto", feature = "http-json"))]
156+
SupportedTransportClient::Http(client) => client.export(metrics).await,
157+
}
144158
}
145159

146160
fn force_flush(&self) -> OTelSdkResult {
@@ -149,7 +163,12 @@ impl PushMetricExporter for MetricExporter {
149163
}
150164

151165
fn shutdown(&self) -> OTelSdkResult {
152-
self.client.shutdown()
166+
match &self.client {
167+
#[cfg(feature = "grpc-tonic")]
168+
SupportedTransportClient::Tonic(client) => client.shutdown(),
169+
#[cfg(any(feature = "http-proto", feature = "http-json"))]
170+
SupportedTransportClient::Http(client) => client.shutdown(),
171+
}
153172
}
154173

155174
fn temporality(&self) -> Temporality {
@@ -163,10 +182,24 @@ impl MetricExporter {
163182
MetricExporterBuilder::default()
164183
}
165184

166-
/// Create a new metrics exporter
167-
pub(crate) fn new(client: impl MetricsClient, temporality: Temporality) -> MetricExporter {
168-
MetricExporter {
169-
client: Box::new(client),
185+
#[cfg(feature = "grpc-tonic")]
186+
pub(crate) fn from_tonic(
187+
client: crate::exporter::tonic::metrics::TonicMetricsClient,
188+
temporality: Temporality,
189+
) -> Self {
190+
Self {
191+
client: SupportedTransportClient::Tonic(client),
192+
temporality,
193+
}
194+
}
195+
196+
#[cfg(any(feature = "http-proto", feature = "http-json"))]
197+
pub(crate) fn from_http(
198+
client: crate::exporter::http::OtlpHttpClient,
199+
temporality: Temporality,
200+
) -> Self {
201+
Self {
202+
client: SupportedTransportClient::Http(client),
170203
temporality,
171204
}
172205
}

0 commit comments

Comments
 (0)