Skip to content

Commit cb293bf

Browse files
fix: move telemetry to "warning" level + add timeouts and use non-blocking client (#402)
1 parent 6dc7ab2 commit cb293bf

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

bins/nittei/src/telemetry.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::time::Duration;
2+
13
use opentelemetry::{global, propagation::TextMapCompositePropagator, trace::TracerProvider};
24
use opentelemetry_datadog::{ApiVersion, DatadogPipelineBuilder, DatadogPropagator};
35
use opentelemetry_otlp::{WithExportConfig, WithHttpConfig};
@@ -14,7 +16,10 @@ use tracing_subscriber::{EnvFilter, Registry, layer::SubscriberExt};
1416
/// It should only be called once!
1517
pub fn init_subscriber() -> anyhow::Result<()> {
1618
// Filter the spans that are shown based on the RUST_LOG env var or the default value ("info")
17-
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
19+
let env_filter = EnvFilter::try_from_default_env()
20+
.unwrap_or_else(|_| EnvFilter::new("info"))
21+
// Downgrade opentelemetry_sdk errors to warnings
22+
.add_directive("opentelemetry_sdk=warn".parse()?);
1823

1924
// If the binary is compiled in debug mode (aka for development)
2025
// use the compact format for logs
@@ -125,7 +130,8 @@ fn get_tracer_datadog(
125130
config.sampler = Box::new(get_sampler());
126131
config.id_generator = Box::new(RandomIdGenerator::default());
127132

128-
let http_client = reqwest::blocking::Client::new();
133+
let http_client = get_http_client()?;
134+
129135
DatadogPipelineBuilder::default()
130136
.with_http_client(http_client)
131137
.with_service_name(service_name)
@@ -146,7 +152,7 @@ fn get_tracer_otlp(
146152
service_version: String,
147153
service_env: String,
148154
) -> anyhow::Result<SdkTracerProvider> {
149-
let http_client = reqwest::blocking::Client::new();
155+
let http_client = get_http_client()?;
150156
let otlp_exporter = opentelemetry_otlp::SpanExporter::builder()
151157
.with_http()
152158
.with_http_client(http_client)
@@ -192,3 +198,13 @@ fn get_sampler() -> Sampler {
192198
// (2) if no parent, then the trace id ratio
193199
Sampler::ParentBased(Box::new(Sampler::TraceIdRatioBased(ratio_to_sample)))
194200
}
201+
202+
/// Get the HTTP client to be used
203+
/// This is used to send traces to the tracing endpoint
204+
fn get_http_client() -> anyhow::Result<reqwest::Client> {
205+
reqwest::Client::builder()
206+
.timeout(Duration::from_secs(10))
207+
.connect_timeout(Duration::from_secs(5))
208+
.build()
209+
.map_err(|e| anyhow::anyhow!("Failed to create HTTP client for telemetry: {}", e))
210+
}

0 commit comments

Comments
 (0)