Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add documentation
  • Loading branch information
mellowagain committed Sep 12, 2023
commit 957de34d0ab797626b1d23d646b145d328d9efa1
53 changes: 41 additions & 12 deletions autometrics/src/otel_push_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use opentelemetry_sdk::runtime;
use std::ops::Deref;
use std::time::Duration;

/// Newtype struct holding a [`MeterProvider`] with a custom `Drop` implementation to automatically clean up itself
#[repr(transparent)]
#[must_use = "Assign this to a unused variable instead: `let _meter = ...` (NOT `let _ = ...`), as else it will be dropped immediately - which will cause it to be shut down"]
pub struct OtelMeterProvider(MeterProvider);
Expand All @@ -25,15 +26,29 @@ impl Drop for OtelMeterProvider {
}
}

/// Initialize the OpenTelemetry push exporter using HTTP transport.
///
/// # Interval and timeout
/// This function uses the environment variables `OTEL_METRIC_EXPORT_TIMEOUT` and `OTEL_METRIC_EXPORT_INTERVAL`
/// to configure the timeout and interval respectively. If you want to customize those
/// 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> {
init_http_with_timeout_period(
url,
Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
Duration::from_secs(1),
)
runtime()
.with_exporter(
opentelemetry_otlp::new_exporter()
.http()
.with_export_config(ExportConfig {
endpoint: url.into(),
protocol: Protocol::HttpBinary,
..Default::default()
}),
)
.build()
.map(OtelMeterProvider)
}

/// Initialize the OpenTelemetry push exporter using HTTP transport with customized `timeout` and `period`.
#[cfg(feature = "otel-push-exporter-http")]
pub fn init_http_with_timeout_period(
url: impl Into<String>,
Expand All @@ -56,15 +71,29 @@ pub fn init_http_with_timeout_period(
.map(OtelMeterProvider)
}

/// Initialize the OpenTelemetry push exporter using gRPC transport.
///
/// # Interval and timeout
/// This function uses the environment variables `OTEL_METRIC_EXPORT_TIMEOUT` and `OTEL_METRIC_EXPORT_INTERVAL`
/// to configure the timeout and interval respectively. If you want to customize those
/// 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> {
init_grpc_with_timeout_period(
url,
Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
Duration::from_secs(1),
)
runtime()
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_export_config(ExportConfig {
endpoint: url.into(),
protocol: Protocol::Grpc,
..Default::default()
}),
)
.build()
.map(OtelMeterProvider)
}

/// Initialize the OpenTelemetry push exporter using gRPC transport with customized `timeout` and `period`.
#[cfg(feature = "otel-push-exporter-grpc")]
pub fn init_grpc_with_timeout_period(
url: impl Into<String>,
Expand All @@ -89,10 +118,10 @@ pub fn init_grpc_with_timeout_period(

#[cfg(all(
feature = "otel-push-exporter-tokio",
not(any(
any(
feature = "otel-push-exporter-tokio-current-thread",
feature = "otel-push-exporter-async-std"
))
)
))]
fn runtime() -> OtlpMetricPipeline<opentelemetry_sdk::runtime::Tokio> {
return opentelemetry_otlp::new_pipeline().metrics(opentelemetry_sdk::runtime::Tokio);
Expand Down