@@ -15,7 +15,6 @@ use crate::{exporter::tonic::TonicExporterBuilder, HasTonicConfig, TonicExporter
1515
1616use crate :: NoExporterBuilderSet ;
1717
18- use async_trait:: async_trait;
1918use core:: fmt;
2019use opentelemetry_sdk:: error:: OTelSdkResult ;
2120use opentelemetry_sdk:: metrics:: {
@@ -120,18 +119,28 @@ impl HasHttpConfig for MetricExporterBuilder<HttpExporterBuilderSet> {
120119}
121120
122121/// An interface for OTLP metrics clients
123- #[ async_trait]
124122pub ( 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.
130131pub 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+
135144impl 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
141150impl 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