From 8aa7cf98fb7fe8b8cafd89d24fadc158a4e88416 Mon Sep 17 00:00:00 2001 From: Mari Date: Fri, 15 Dec 2023 12:03:15 +0100 Subject: [PATCH] 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` --- CHANGELOG.md | 1 + autometrics/src/otel_push_exporter.rs | 53 ++++++++++++++------------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 451e1b3..86415bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased - Update `http` to `1.0`. This fixes compatibility with `axum 0.7` (#167) +- Explicitly set default timeout and period for the OTEL push exporter (#168) ## [1.0.0](https://github.com/autometrics-dev/autometrics-rs/releases/tag/v1.0.0) - 2023-12-01 diff --git a/autometrics/src/otel_push_exporter.rs b/autometrics/src/otel_push_exporter.rs index a55b298..4bf5c74 100644 --- a/autometrics/src/otel_push_exporter.rs +++ b/autometrics/src/otel_push_exporter.rs @@ -1,6 +1,6 @@ use opentelemetry::metrics::MetricsError; -use opentelemetry_otlp::OtlpMetricPipeline; use opentelemetry_otlp::{ExportConfig, Protocol, WithExportConfig}; +use opentelemetry_otlp::{OtlpMetricPipeline, OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT}; use opentelemetry_sdk::metrics::MeterProvider; use std::ops::Deref; use std::time::Duration; @@ -33,18 +33,8 @@ impl Drop for OtelMeterProvider { /// from within code, consider using [`init_http_with_timeout_period`]. #[cfg(feature = "otel-push-exporter-http")] pub fn init_http(url: impl Into) -> Result { - runtime() - .with_exporter( - opentelemetry_otlp::new_exporter() - .http() - .with_export_config(ExportConfig { - endpoint: url.into(), - protocol: Protocol::HttpBinary, - ..Default::default() - }), - ) - .build() - .map(OtelMeterProvider) + let (timeout, period) = timeout_and_period_from_env_or_default(); + init_http_with_timeout_period(url, timeout, period) } /// Initialize the OpenTelemetry push exporter using HTTP transport with customized `timeout` and `period`. @@ -78,18 +68,8 @@ pub fn init_http_with_timeout_period( /// from within code, consider using [`init_grpc_with_timeout_period`]. #[cfg(feature = "otel-push-exporter-grpc")] pub fn init_grpc(url: impl Into) -> Result { - runtime() - .with_exporter( - opentelemetry_otlp::new_exporter() - .tonic() - .with_export_config(ExportConfig { - endpoint: url.into(), - protocol: Protocol::Grpc, - ..Default::default() - }), - ) - .build() - .map(OtelMeterProvider) + let (timeout, period) = timeout_and_period_from_env_or_default(); + init_grpc_with_timeout_period(url, timeout, period) } /// Initialize the OpenTelemetry push exporter using gRPC transport with customized `timeout` and `period`. @@ -115,6 +95,29 @@ pub fn init_grpc_with_timeout_period( .map(OtelMeterProvider) } +/// returns timeout and period from their respective environment variables +/// or the default, if they are not set or set to an invalid value +fn timeout_and_period_from_env_or_default() -> (Duration, Duration) { + const OTEL_EXPORTER_TIMEOUT_ENV: &str = "OTEL_METRIC_EXPORT_TIMEOUT"; + const OTEL_EXPORTER_INTERVAL_ENV: &str = "OTEL_METRIC_EXPORT_INTERVAL"; + + let timeout = Duration::from_secs( + std::env::var_os(OTEL_EXPORTER_TIMEOUT_ENV) + .and_then(|os_string| os_string.into_string().ok()) + .and_then(|str| str.parse().ok()) + .unwrap_or(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT), + ); + + let period = Duration::from_secs( + std::env::var_os(OTEL_EXPORTER_INTERVAL_ENV) + .and_then(|os_string| os_string.into_string().ok()) + .and_then(|str| str.parse().ok()) + .unwrap_or(60), + ); + + (timeout, period) +} + #[cfg(all( feature = "otel-push-exporter-tokio", not(any(