1
1
use opentelemetry:: metrics:: MetricsError ;
2
- use opentelemetry_otlp:: OtlpMetricPipeline ;
3
2
use opentelemetry_otlp:: { ExportConfig , Protocol , WithExportConfig } ;
3
+ use opentelemetry_otlp:: { OtlpMetricPipeline , OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT } ;
4
4
use opentelemetry_sdk:: metrics:: MeterProvider ;
5
5
use std:: ops:: Deref ;
6
6
use std:: time:: Duration ;
@@ -33,18 +33,8 @@ impl Drop for OtelMeterProvider {
33
33
/// from within code, consider using [`init_http_with_timeout_period`].
34
34
#[ cfg( feature = "otel-push-exporter-http" ) ]
35
35
pub fn init_http ( url : impl Into < String > ) -> Result < OtelMeterProvider , MetricsError > {
36
- runtime ( )
37
- . with_exporter (
38
- opentelemetry_otlp:: new_exporter ( )
39
- . http ( )
40
- . with_export_config ( ExportConfig {
41
- endpoint : url. into ( ) ,
42
- protocol : Protocol :: HttpBinary ,
43
- ..Default :: default ( )
44
- } ) ,
45
- )
46
- . build ( )
47
- . map ( OtelMeterProvider )
36
+ let ( timeout, period) = timeout_and_period_from_env_or_default ( ) ;
37
+ init_http_with_timeout_period ( url, timeout, period)
48
38
}
49
39
50
40
/// Initialize the OpenTelemetry push exporter using HTTP transport with customized `timeout` and `period`.
@@ -78,18 +68,8 @@ pub fn init_http_with_timeout_period(
78
68
/// from within code, consider using [`init_grpc_with_timeout_period`].
79
69
#[ cfg( feature = "otel-push-exporter-grpc" ) ]
80
70
pub fn init_grpc ( url : impl Into < String > ) -> Result < OtelMeterProvider , MetricsError > {
81
- runtime ( )
82
- . with_exporter (
83
- opentelemetry_otlp:: new_exporter ( )
84
- . tonic ( )
85
- . with_export_config ( ExportConfig {
86
- endpoint : url. into ( ) ,
87
- protocol : Protocol :: Grpc ,
88
- ..Default :: default ( )
89
- } ) ,
90
- )
91
- . build ( )
92
- . map ( OtelMeterProvider )
71
+ let ( timeout, period) = timeout_and_period_from_env_or_default ( ) ;
72
+ init_grpc_with_timeout_period ( url, timeout, period)
93
73
}
94
74
95
75
/// Initialize the OpenTelemetry push exporter using gRPC transport with customized `timeout` and `period`.
@@ -115,6 +95,29 @@ pub fn init_grpc_with_timeout_period(
115
95
. map ( OtelMeterProvider )
116
96
}
117
97
98
+ /// returns timeout and period from their respective environment variables
99
+ /// or the default, if they are not set or set to an invalid value
100
+ fn timeout_and_period_from_env_or_default ( ) -> ( Duration , Duration ) {
101
+ const OTEL_EXPORTER_TIMEOUT_ENV : & str = "OTEL_METRIC_EXPORT_TIMEOUT" ;
102
+ const OTEL_EXPORTER_INTERVAL_ENV : & str = "OTEL_METRIC_EXPORT_INTERVAL" ;
103
+
104
+ let timeout = Duration :: from_secs (
105
+ std:: env:: var_os ( OTEL_EXPORTER_TIMEOUT_ENV )
106
+ . and_then ( |os_string| os_string. into_string ( ) . ok ( ) )
107
+ . and_then ( |str| str. parse ( ) . ok ( ) )
108
+ . unwrap_or ( OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT ) ,
109
+ ) ;
110
+
111
+ let period = Duration :: from_secs (
112
+ std:: env:: var_os ( OTEL_EXPORTER_INTERVAL_ENV )
113
+ . and_then ( |os_string| os_string. into_string ( ) . ok ( ) )
114
+ . and_then ( |str| str. parse ( ) . ok ( ) )
115
+ . unwrap_or ( 60 ) ,
116
+ ) ;
117
+
118
+ ( timeout, period)
119
+ }
120
+
118
121
#[ cfg( all(
119
122
feature = "otel-push-exporter-tokio" ,
120
123
not( any(
0 commit comments