@@ -9,10 +9,22 @@ use crate::HasExportConfig;
99#[ cfg( any( feature = "http-proto" , feature = "http-json" ) ) ]
1010use crate :: { exporter:: http:: HttpExporterBuilder , HasHttpConfig , HttpExporterBuilderSet } ;
1111
12+ #[ cfg( feature = "http-proto" ) ]
13+ use crate :: exporter:: OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF ;
14+
15+ #[ cfg( feature = "http-json" ) ]
16+ use crate :: exporter:: OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON ;
17+
1218#[ cfg( feature = "grpc-tonic" ) ]
13- use crate :: { exporter:: tonic:: TonicExporterBuilder , HasTonicConfig , TonicExporterBuilderSet } ;
19+ use crate :: {
20+ exporter:: { tonic:: TonicExporterBuilder , OTEL_EXPORTER_OTLP_PROTOCOL_GRPC } ,
21+ HasTonicConfig , TonicExporterBuilderSet ,
22+ } ;
1423
15- use crate :: { ExporterBuildError , NoExporterBuilderSet } ;
24+ use crate :: {
25+ ExporterBuildError , NoExporterBuilderSet , OTEL_EXPORTER_OTLP_PROTOCOL ,
26+ OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT ,
27+ } ;
1628
1729use core:: fmt;
1830use opentelemetry_sdk:: error:: OTelSdkResult ;
@@ -187,6 +199,24 @@ impl MetricExporter {
187199 MetricExporterBuilder :: default ( )
188200 }
189201
202+ /// Builds a default [MetricExporter] based on the value of `OTEL_EXPORTER_OTLP_PROTOCOL`.
203+ pub fn build_default ( self ) -> Result < MetricExporter , ExporterBuildError > {
204+ match std:: env:: var ( OTEL_EXPORTER_OTLP_PROTOCOL )
205+ . unwrap_or ( OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT . to_string ( ) )
206+ . as_str ( )
207+ {
208+ #[ cfg( feature = "grpc-tonic" ) ]
209+ OTEL_EXPORTER_OTLP_PROTOCOL_GRPC => Self :: builder ( ) . with_tonic ( ) . build ( ) ,
210+ #[ cfg( feature = "http-proto" ) ]
211+ OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF => Self :: builder ( ) . with_http ( ) . build ( ) ,
212+ #[ cfg( feature = "http-json" ) ]
213+ OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON => Self :: builder ( ) . with_http ( ) . build ( ) ,
214+ other => Err ( ExporterBuildError :: InternalFailure ( format ! (
215+ "Invalid {OTEL_EXPORTER_OTLP_PROTOCOL}: {other}"
216+ ) ) ) ,
217+ }
218+ }
219+
190220 #[ cfg( feature = "grpc-tonic" ) ]
191221 pub ( crate ) fn from_tonic (
192222 client : crate :: exporter:: tonic:: metrics:: TonicMetricsClient ,
0 commit comments