Skip to content

Commit

Permalink
add default values for timeout and period in otel push exporter (#…
Browse files Browse the repository at this point in the history
…168)

* add default values for `timeout` and `period` in otel push exporter

* update changelog

* pr review

* fix conversion from `OsString` into `String`
  • Loading branch information
mellowagain authored Dec 15, 2023
1 parent 63ff836 commit 8aa7cf9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
53 changes: 28 additions & 25 deletions autometrics/src/otel_push_exporter.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String>) -> Result<OtelMeterProvider, MetricsError> {
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`.
Expand Down Expand Up @@ -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<String>) -> Result<OtelMeterProvider, MetricsError> {
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`.
Expand All @@ -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(
Expand Down

0 comments on commit 8aa7cf9

Please sign in to comment.