Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add default values for timeout and period in otel push exporter #168

Merged
merged 4 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
mellowagain marked this conversation as resolved.
Show resolved Hide resolved
);

(timeout, period)
}

#[cfg(all(
feature = "otel-push-exporter-tokio",
not(any(
Expand Down
Loading