Skip to content

Commit f959a84

Browse files
fix: telemetry
1 parent b7b482f commit f959a84

4 files changed

Lines changed: 37 additions & 20 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bins/nittei/Cargo.toml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ tracing-subscriber = { version = "0.3", features = [
4343
opentelemetry = { version = "0.32.0", default-features = false, features = [
4444
"trace",
4545
] }
46-
opentelemetry_sdk = { version = "0.32.0", features = ["rt-tokio"] }
46+
opentelemetry_sdk = { version = "0.32.0" }
4747
tracing-opentelemetry = "0.33.0"
48-
opentelemetry-otlp = { version = "0.32.0", features = [
49-
"reqwest-client",
50-
"reqwest-rustls",
51-
"http-proto",
52-
"tls",
48+
opentelemetry-otlp = { version = "0.32.0", features = ["http-proto", "tls"] }
49+
opentelemetry-datadog = { version = "0.20.0" }
50+
reqwest = { version = "0.13.4", default-features = false, features = [
51+
"http2",
52+
"json",
53+
"rustls",
54+
"blocking",
5355
] }
54-
opentelemetry-datadog = { version = "0.20.0", features = ["reqwest-client"] }
5556

5657
chrono = "0.4.39"
5758
chrono-tz = "0.10.1"

bins/nittei/src/main.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@ static GLOBAL: Jemalloc = Jemalloc;
1414
/// Main wraps the `run` function with a tokio runtime (flavor can be decided via the `NITTEI__TOKIO_RUNTIME_FLAVOR` env var)
1515
/// See crates/utils/src/config.rs for more details
1616
fn main() {
17+
// Allow eprintln! to be used here as logging/tracing has failed to initialize
18+
#[allow(clippy::unwrap_used, clippy::print_stderr)]
19+
init_subscriber()
20+
.map_err(|e| eprintln!("[init_subscriber] Error: {e}"))
21+
.unwrap();
22+
1723
// Install the custom panic hook
1824
nittei::backtrace::install_custom_panic_hook();
25+
// Initialize the subscriber for logging & tracing
26+
// Must be called inside the Tokio runtime: the batch exporter (rt-tokio) spawns
27+
// a background task and requires an active runtime at initialization time.
1928

2029
let runtime_flavor = nittei_utils::config::APP_CONFIG
2130
.tokio_runtime_flavor
@@ -67,15 +76,6 @@ fn main() {
6776

6877
/// The main function that will be run by the tokio runtime
6978
async fn run() -> anyhow::Result<()> {
70-
// Initialize the subscriber for logging & tracing
71-
// Must be called inside the Tokio runtime: the batch exporter (rt-tokio) spawns
72-
// a background task and requires an active runtime at initialization time.
73-
init_subscriber().inspect_err(
74-
// Allow eprintln! to be used here as logging/tracing has failed to initialize
75-
#[allow(clippy::print_stderr)]
76-
|e| eprintln!("[init_subscriber] Error: {e}"),
77-
)?;
78-
7979
print_runtime_info();
8080

8181
let context = setup_context()

bins/nittei/src/telemetry.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use std::time::Duration;
2+
13
use opentelemetry::{global, propagation::TextMapCompositePropagator, trace::TracerProvider};
24
use opentelemetry_datadog::{ApiVersion, DatadogPipelineBuilder, DatadogPropagator};
3-
use opentelemetry_otlp::WithExportConfig;
5+
use opentelemetry_otlp::{WithExportConfig, WithHttpConfig};
46
use opentelemetry_sdk::{
57
Resource,
68
propagation::TraceContextPropagator,
@@ -131,6 +133,7 @@ fn get_tracer_datadog(
131133
service_version: String,
132134
service_env: String,
133135
) -> anyhow::Result<SdkTracerProvider> {
136+
let http_client = get_http_client()?;
134137
let mut config = trace::Config::default();
135138
config.sampler = Box::new(get_sampler());
136139
config.id_generator = Box::new(RandomIdGenerator::default());
@@ -142,6 +145,7 @@ fn get_tracer_datadog(
142145
.with_api_version(ApiVersion::Version05)
143146
.with_agent_endpoint(datadog_endpoint)
144147
.with_trace_config(config)
148+
.with_http_client(http_client)
145149
.install_batch()
146150
.map_err(|e| e.into())
147151
}
@@ -154,8 +158,10 @@ fn get_tracer_otlp(
154158
service_version: String,
155159
service_env: String,
156160
) -> anyhow::Result<SdkTracerProvider> {
161+
let http_client = get_http_client()?;
157162
let otlp_exporter = opentelemetry_otlp::SpanExporter::builder()
158163
.with_http()
164+
.with_http_client(http_client)
159165
.with_protocol(opentelemetry_otlp::Protocol::HttpBinary)
160166
.with_endpoint(otlp_endpoint)
161167
.build()?;
@@ -198,3 +204,15 @@ fn get_sampler() -> Sampler {
198204
// (2) if no parent, then the trace id ratio
199205
Sampler::ParentBased(Box::new(Sampler::TraceIdRatioBased(ratio_to_sample)))
200206
}
207+
208+
/// Get the HTTP client to be used
209+
/// This is used to send traces to the tracing endpoint
210+
///
211+
/// The exporter runs on a different thread than the main thread, so we need to use a blocking client.
212+
fn get_http_client() -> anyhow::Result<reqwest::blocking::Client> {
213+
reqwest::blocking::Client::builder()
214+
.timeout(Duration::from_secs(10))
215+
.connect_timeout(Duration::from_secs(5))
216+
.build()
217+
.map_err(|e| anyhow::anyhow!("Failed to create HTTP client for telemetry: {}", e))
218+
}

0 commit comments

Comments
 (0)