Skip to content

Commit 8aa7cf9

Browse files
authored
add default values for timeout and period in otel push exporter (#168)
* add default values for `timeout` and `period` in otel push exporter * update changelog * pr review * fix conversion from `OsString` into `String`
1 parent 63ff836 commit 8aa7cf9

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
## Unreleased
1111

1212
- Update `http` to `1.0`. This fixes compatibility with `axum 0.7` (#167)
13+
- Explicitly set default timeout and period for the OTEL push exporter (#168)
1314

1415
## [1.0.0](https://github.com/autometrics-dev/autometrics-rs/releases/tag/v1.0.0) - 2023-12-01
1516

autometrics/src/otel_push_exporter.rs

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use opentelemetry::metrics::MetricsError;
2-
use opentelemetry_otlp::OtlpMetricPipeline;
32
use opentelemetry_otlp::{ExportConfig, Protocol, WithExportConfig};
3+
use opentelemetry_otlp::{OtlpMetricPipeline, OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT};
44
use opentelemetry_sdk::metrics::MeterProvider;
55
use std::ops::Deref;
66
use std::time::Duration;
@@ -33,18 +33,8 @@ impl Drop for OtelMeterProvider {
3333
/// from within code, consider using [`init_http_with_timeout_period`].
3434
#[cfg(feature = "otel-push-exporter-http")]
3535
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)
4838
}
4939

5040
/// Initialize the OpenTelemetry push exporter using HTTP transport with customized `timeout` and `period`.
@@ -78,18 +68,8 @@ pub fn init_http_with_timeout_period(
7868
/// from within code, consider using [`init_grpc_with_timeout_period`].
7969
#[cfg(feature = "otel-push-exporter-grpc")]
8070
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)
9373
}
9474

9575
/// Initialize the OpenTelemetry push exporter using gRPC transport with customized `timeout` and `period`.
@@ -115,6 +95,29 @@ pub fn init_grpc_with_timeout_period(
11595
.map(OtelMeterProvider)
11696
}
11797

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+
118121
#[cfg(all(
119122
feature = "otel-push-exporter-tokio",
120123
not(any(

0 commit comments

Comments
 (0)