Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leverage native async trait in MetricsClient #2672

Merged
merged 1 commit into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion opentelemetry-otlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 0 additions & 2 deletions opentelemetry-otlp/src/exporter/http/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
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};
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
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-otlp/src/exporter/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@
OTEL_EXPORTER_OTLP_METRICS_HEADERS,
)?;

Ok(crate::MetricExporter::new(client, temporality))
Ok(crate::MetricExporter::from_http(client, temporality))

Check warning on line 265 in opentelemetry-otlp/src/exporter/http/mod.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L265 was not covered by tests
}
}

Expand Down
2 changes: 0 additions & 2 deletions opentelemetry-otlp/src/exporter/tonic/metrics.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-otlp/src/exporter/tonic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
pub(crate) mod logs;

#[cfg(feature = "metrics")]
mod metrics;
pub(crate) mod metrics;

#[cfg(feature = "trace")]
pub(crate) mod trace;
Expand Down Expand Up @@ -296,7 +296,7 @@

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

Ok(MetricExporter::new(client, temporality))
Ok(MetricExporter::from_tonic(client, temporality))

Check warning on line 299 in opentelemetry-otlp/src/exporter/tonic/mod.rs

View check run for this annotation

Codecov / codecov/patch

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

Added line #L299 was not covered by tests
}

/// Build a new tonic span exporter
Expand Down
53 changes: 43 additions & 10 deletions opentelemetry-otlp/src/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use crate::NoExporterBuilderSet;

use async_trait::async_trait;
use core::fmt;
use opentelemetry_sdk::error::OTelSdkResult;
use opentelemetry_sdk::metrics::MetricResult;
Expand Down Expand Up @@ -121,18 +120,28 @@
}

/// 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<Output = OTelSdkResult> + Send;
fn shutdown(&self) -> OTelSdkResult;
}

/// Export metrics in OTEL format.
pub struct MetricExporter {
client: Box<dyn MetricsClient>,
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()
Expand All @@ -141,7 +150,12 @@

impl PushMetricExporter for MetricExporter {
async fn export(&self, metrics: &mut ResourceMetrics) -> OTelSdkResult {
self.client.export(metrics).await
match &self.client {

Check warning on line 153 in opentelemetry-otlp/src/metric.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/metric.rs#L153

Added line #L153 was not covered by tests
#[cfg(feature = "grpc-tonic")]
SupportedTransportClient::Tonic(client) => client.export(metrics).await,

Check warning on line 155 in opentelemetry-otlp/src/metric.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/metric.rs#L155

Added line #L155 was not covered by tests
#[cfg(any(feature = "http-proto", feature = "http-json"))]
SupportedTransportClient::Http(client) => client.export(metrics).await,

Check warning on line 157 in opentelemetry-otlp/src/metric.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/metric.rs#L157

Added line #L157 was not covered by tests
}
}

fn force_flush(&self) -> OTelSdkResult {
Expand All @@ -150,7 +164,12 @@
}

fn shutdown(&self) -> OTelSdkResult {
self.client.shutdown()
match &self.client {

Check warning on line 167 in opentelemetry-otlp/src/metric.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/metric.rs#L167

Added line #L167 was not covered by tests
#[cfg(feature = "grpc-tonic")]
SupportedTransportClient::Tonic(client) => client.shutdown(),

Check warning on line 169 in opentelemetry-otlp/src/metric.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/metric.rs#L169

Added line #L169 was not covered by tests
#[cfg(any(feature = "http-proto", feature = "http-json"))]
SupportedTransportClient::Http(client) => client.shutdown(),

Check warning on line 171 in opentelemetry-otlp/src/metric.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/metric.rs#L171

Added line #L171 was not covered by tests
}
}

fn temporality(&self) -> Temporality {
Expand All @@ -164,10 +183,24 @@
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,
}
}

Check warning on line 195 in opentelemetry-otlp/src/metric.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/metric.rs#L187-L195

Added lines #L187 - L195 were not covered by tests

#[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),

Check warning on line 203 in opentelemetry-otlp/src/metric.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/metric.rs#L198-L203

Added lines #L198 - L203 were not covered by tests
temporality,
}
}
Expand Down