Skip to content
Merged
Show file tree
Hide file tree
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 must use notice as well functions to customize timeout and period
  • Loading branch information
mellowagain committed Sep 8, 2023
commit c8387811a8ec0b0009cdfa2395ff11a809df4a13
35 changes: 32 additions & 3 deletions autometrics/src/otel_push_exporter.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use opentelemetry_api::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 opentelemetry_sdk::runtime;
use std::ops::Deref;
use std::time::Duration;

#[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);

impl Deref for OtelMeterProvider {
Expand All @@ -26,34 +27,62 @@ impl Drop for OtelMeterProvider {

#[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),
Copy link
Contributor

@hatchan hatchan Sep 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This period is that the same as push interval? If so, then I think we should not use such a low value, not sure what would be a better value 🤔 Maybe something like 30 seconds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im actually thinking maybe we shouldnt set these at all. then otlp exporter would fall back to using the env variables OTEL_METRIC_EXPORT_TIMEOUT and OTEL_METRIC_EXPORT_INTERVAL and if not found fall back to the default value, currently 30 timeout and 60 interval

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have done so now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot about this, using 30 seconds is probably the better default, but also maybe it'll create struggles with the explorer with the build_info metric being looked for only on the last second. Didn't have time to test Brett's fix, maybe that's not an issue anymore

)
}

#[cfg(feature = "otel-push-exporter-http")]
pub fn init_http_with_timeout_period(
url: impl Into<String>,
timeout: Duration,
period: Duration,
) -> Result<OtelMeterProvider, MetricsError> {
runtime()
.with_exporter(
opentelemetry_otlp::new_exporter()
.http()
.with_export_config(ExportConfig {
endpoint: url.into(),
protocol: Protocol::HttpBinary,
timeout,
..Default::default()
}),
)
.with_period(Duration::from_secs(1))
.with_period(period)
.build()
.map(OtelMeterProvider)
}

#[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),
)
}

#[cfg(feature = "otel-push-exporter-grpc")]
pub fn init_grpc_with_timeout_period(
url: impl Into<String>,
timeout: Duration,
period: Duration,
) -> Result<OtelMeterProvider, MetricsError> {
runtime()
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_export_config(ExportConfig {
endpoint: url.into(),
protocol: Protocol::Grpc,
timeout,
..Default::default()
}),
)
.with_period(Duration::from_secs(1))
.with_period(period)
.build()
.map(OtelMeterProvider)
}
Expand Down
2 changes: 2 additions & 0 deletions examples/opentelemetry-push/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ async fn do_stuff() {

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
// NOTICE: the variable gets assigned to `_meter_provider` instead of just `_`, as the later case
// would cause it to be dropped immediately and thus shut down.
let _meter_provider = otel_push_exporter::init_http("http://0.0.0.0:4318")?;
// or: otel_push_exporter::init_grpc("http://0.0.0.0:4317");

Expand Down