From 5cf2164ea29a2735966f51566be68f1f2482111c Mon Sep 17 00:00:00 2001 From: gruebel Date: Sat, 15 Feb 2025 11:58:21 +0100 Subject: [PATCH] leverage native async trait in MetricsClient --- opentelemetry-otlp/Cargo.toml | 1 - .../src/exporter/http/metrics.rs | 2 - opentelemetry-otlp/src/exporter/http/mod.rs | 2 +- .../src/exporter/tonic/metrics.rs | 2 - opentelemetry-otlp/src/exporter/tonic/mod.rs | 4 +- opentelemetry-otlp/src/metric.rs | 53 +++++++++++++++---- 6 files changed, 46 insertions(+), 18 deletions(-) diff --git a/opentelemetry-otlp/Cargo.toml b/opentelemetry-otlp/Cargo.toml index b202b23013..5d593a5b58 100644 --- a/opentelemetry-otlp/Cargo.toml +++ b/opentelemetry-otlp/Cargo.toml @@ -26,7 +26,6 @@ all-features = true rustdoc-args = ["--cfg", "docsrs"] [dependencies] -async-trait = { workspace = true } futures-core = { workspace = true } opentelemetry = { version = "0.28", default-features = false, path = "../opentelemetry" } opentelemetry_sdk = { version = "0.28", default-features = false, path = "../opentelemetry-sdk" } diff --git a/opentelemetry-otlp/src/exporter/http/metrics.rs b/opentelemetry-otlp/src/exporter/http/metrics.rs index f534b69a7f..759dd6a7d5 100644 --- a/opentelemetry-otlp/src/exporter/http/metrics.rs +++ b/opentelemetry-otlp/src/exporter/http/metrics.rs @@ -1,7 +1,6 @@ use std::sync::Arc; use crate::metric::MetricsClient; -use async_trait::async_trait; use http::{header::CONTENT_TYPE, Method}; use opentelemetry::otel_debug; use opentelemetry_sdk::error::{OTelSdkError, OTelSdkResult}; @@ -9,7 +8,6 @@ use opentelemetry_sdk::metrics::data::ResourceMetrics; use super::OtlpHttpClient; -#[async_trait] impl MetricsClient for OtlpHttpClient { async fn export(&self, metrics: &mut ResourceMetrics) -> OTelSdkResult { let client = self diff --git a/opentelemetry-otlp/src/exporter/http/mod.rs b/opentelemetry-otlp/src/exporter/http/mod.rs index 10342a858a..04d90867e9 100644 --- a/opentelemetry-otlp/src/exporter/http/mod.rs +++ b/opentelemetry-otlp/src/exporter/http/mod.rs @@ -262,7 +262,7 @@ impl HttpExporterBuilder { OTEL_EXPORTER_OTLP_METRICS_HEADERS, )?; - Ok(crate::MetricExporter::new(client, temporality)) + Ok(crate::MetricExporter::from_http(client, temporality)) } } diff --git a/opentelemetry-otlp/src/exporter/tonic/metrics.rs b/opentelemetry-otlp/src/exporter/tonic/metrics.rs index c101829a34..2e5eb9df41 100644 --- a/opentelemetry-otlp/src/exporter/tonic/metrics.rs +++ b/opentelemetry-otlp/src/exporter/tonic/metrics.rs @@ -1,7 +1,6 @@ use core::fmt; use std::sync::Mutex; -use async_trait::async_trait; use opentelemetry::otel_debug; use opentelemetry_proto::tonic::collector::metrics::v1::{ metrics_service_client::MetricsServiceClient, ExportMetricsServiceRequest, @@ -52,7 +51,6 @@ impl TonicMetricsClient { } } -#[async_trait] impl MetricsClient for TonicMetricsClient { async fn export(&self, metrics: &mut ResourceMetrics) -> OTelSdkResult { let (mut client, metadata, extensions) = self diff --git a/opentelemetry-otlp/src/exporter/tonic/mod.rs b/opentelemetry-otlp/src/exporter/tonic/mod.rs index 3b4bf03d42..d0829060df 100644 --- a/opentelemetry-otlp/src/exporter/tonic/mod.rs +++ b/opentelemetry-otlp/src/exporter/tonic/mod.rs @@ -23,7 +23,7 @@ use crate::{ pub(crate) mod logs; #[cfg(feature = "metrics")] -mod metrics; +pub(crate) mod metrics; #[cfg(feature = "trace")] pub(crate) mod trace; @@ -296,7 +296,7 @@ impl TonicExporterBuilder { let client = TonicMetricsClient::new(channel, interceptor, compression); - Ok(MetricExporter::new(client, temporality)) + Ok(MetricExporter::from_tonic(client, temporality)) } /// Build a new tonic span exporter diff --git a/opentelemetry-otlp/src/metric.rs b/opentelemetry-otlp/src/metric.rs index 4a5b4063b1..22bdcc25fb 100644 --- a/opentelemetry-otlp/src/metric.rs +++ b/opentelemetry-otlp/src/metric.rs @@ -14,7 +14,6 @@ use crate::{exporter::tonic::TonicExporterBuilder, HasTonicConfig, TonicExporter use crate::NoExporterBuilderSet; -use async_trait::async_trait; use core::fmt; use opentelemetry_sdk::error::OTelSdkResult; use opentelemetry_sdk::metrics::MetricResult; @@ -121,18 +120,28 @@ impl HasHttpConfig for MetricExporterBuilder { } /// An interface for OTLP metrics clients -#[async_trait] pub(crate) trait MetricsClient: fmt::Debug + Send + Sync + 'static { - async fn export(&self, metrics: &mut ResourceMetrics) -> OTelSdkResult; + fn export( + &self, + metrics: &mut ResourceMetrics, + ) -> impl std::future::Future + Send; fn shutdown(&self) -> OTelSdkResult; } /// Export metrics in OTEL format. pub struct MetricExporter { - client: Box, + client: SupportedTransportClient, temporality: Temporality, } +#[derive(Debug)] +enum SupportedTransportClient { + #[cfg(feature = "grpc-tonic")] + Tonic(crate::exporter::tonic::metrics::TonicMetricsClient), + #[cfg(any(feature = "http-proto", feature = "http-json"))] + Http(crate::exporter::http::OtlpHttpClient), +} + impl Debug for MetricExporter { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.debug_struct("MetricExporter").finish() @@ -141,7 +150,12 @@ impl Debug for MetricExporter { impl PushMetricExporter for MetricExporter { async fn export(&self, metrics: &mut ResourceMetrics) -> OTelSdkResult { - self.client.export(metrics).await + match &self.client { + #[cfg(feature = "grpc-tonic")] + SupportedTransportClient::Tonic(client) => client.export(metrics).await, + #[cfg(any(feature = "http-proto", feature = "http-json"))] + SupportedTransportClient::Http(client) => client.export(metrics).await, + } } fn force_flush(&self) -> OTelSdkResult { @@ -150,7 +164,12 @@ impl PushMetricExporter for MetricExporter { } fn shutdown(&self) -> OTelSdkResult { - self.client.shutdown() + match &self.client { + #[cfg(feature = "grpc-tonic")] + SupportedTransportClient::Tonic(client) => client.shutdown(), + #[cfg(any(feature = "http-proto", feature = "http-json"))] + SupportedTransportClient::Http(client) => client.shutdown(), + } } fn temporality(&self) -> Temporality { @@ -164,10 +183,24 @@ impl MetricExporter { MetricExporterBuilder::default() } - /// Create a new metrics exporter - pub(crate) fn new(client: impl MetricsClient, temporality: Temporality) -> MetricExporter { - MetricExporter { - client: Box::new(client), + #[cfg(feature = "grpc-tonic")] + pub(crate) fn from_tonic( + client: crate::exporter::tonic::metrics::TonicMetricsClient, + temporality: Temporality, + ) -> Self { + Self { + client: SupportedTransportClient::Tonic(client), + temporality, + } + } + + #[cfg(any(feature = "http-proto", feature = "http-json"))] + pub(crate) fn from_http( + client: crate::exporter::http::OtlpHttpClient, + temporality: Temporality, + ) -> Self { + Self { + client: SupportedTransportClient::Http(client), temporality, } }