Skip to content

Commit 82465ef

Browse files
committed
Conditions for upload
1 parent 9c03af0 commit 82465ef

File tree

2 files changed

+75
-17
lines changed

2 files changed

+75
-17
lines changed

datadog-crashtracker/src/crash_info/errors_intake.rs

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -203,23 +203,32 @@ impl ErrorsIntakeUploader {
203203
_crashtracker_metadata: &Metadata,
204204
telemetry_endpoint: &Option<Endpoint>,
205205
) -> anyhow::Result<Self> {
206-
let endpoint = Self::build_errors_intake_endpoint(telemetry_endpoint)?;
206+
// Check direct submission setting from environment (same as telemetry)
207+
let cfg = ddtelemetry::config::Config::from_env();
208+
let direct_submission_enabled = cfg.direct_submission_enabled;
209+
210+
let endpoint =
211+
Self::build_errors_intake_endpoint(telemetry_endpoint, direct_submission_enabled)?;
207212
eprintln!(
208213
"DEBUG: Created errors intake uploader with URL: {}",
209214
endpoint.url
210215
);
216+
eprintln!(
217+
"DEBUG: Direct submission enabled: {}",
218+
direct_submission_enabled
219+
);
211220
Ok(Self { endpoint })
212221
}
213222

214223
fn build_errors_intake_endpoint(
215224
telemetry_endpoint: &Option<Endpoint>,
225+
direct_submission_enabled: bool,
216226
) -> anyhow::Result<Endpoint> {
217227
match telemetry_endpoint {
218228
Some(endpoint) => {
219229
let mut errors_endpoint = endpoint.clone();
220230

221-
// Modify the endpoint based on whether we're using agent proxy or direct intake
222-
if endpoint.api_key.is_some() {
231+
if endpoint.api_key.is_some() && direct_submission_enabled {
223232
// Direct intake - change hostname to event-platform-intake
224233
let mut parts = endpoint.url.clone().into_parts();
225234

@@ -333,26 +342,38 @@ impl ErrorsIntakeUploader {
333342
return Ok(());
334343
}
335344

336-
// Build the HTTP request
337-
let mut req_builder = self
338-
.endpoint
339-
.to_request_builder(concat!("crashtracker/", env!("CARGO_PKG_VERSION")))?;
345+
eprintln!(
346+
"DEBUG: Building HTTP request for URL: {}",
347+
self.endpoint.url
348+
);
349+
350+
// Build the HTTP request manually to match telemetry approach
351+
let mut req_builder = http::Request::builder()
352+
.method(http::Method::POST)
353+
.uri(self.endpoint.url.clone())
354+
.header(
355+
http::header::CONTENT_TYPE,
356+
ddcommon::header::APPLICATION_JSON,
357+
)
358+
.header(
359+
http::header::USER_AGENT,
360+
concat!("crashtracker/", env!("CARGO_PKG_VERSION")),
361+
);
340362

341363
// Add errors intake specific headers
342364
if self.endpoint.api_key.is_some() {
343-
// Direct intake - DD-API-KEY is added by to_request_builder
365+
// Direct intake - add API key header
366+
if let Some(api_key) = &self.endpoint.api_key {
367+
req_builder = req_builder.header("DD-API-KEY", api_key.as_ref());
368+
}
344369
} else {
345370
// Agent proxy - add EvP subdomain header
346371
req_builder = req_builder.header("X-Datadog-EVP-Subdomain", ERROR_INTAKE_SUBDOMAIN);
347372
}
348373

349-
let req = req_builder
350-
.method(http::Method::POST)
351-
.header(
352-
http::header::CONTENT_TYPE,
353-
ddcommon::header::APPLICATION_JSON,
354-
)
355-
.body(serde_json::to_string(payload)?.into())?;
374+
let req = req_builder.body(serde_json::to_string(payload)?.into())?;
375+
376+
eprintln!("DEBUG: Built request with headers: {:?}", req.headers());
356377

357378
// Create HTTP client and send request
358379
let client = ddcommon::hyper_migration::new_client_periodic();
@@ -415,10 +436,12 @@ mod tests {
415436
});
416437

417438
let errors_endpoint =
418-
ErrorsIntakeUploader::build_errors_intake_endpoint(&telemetry_endpoint).unwrap();
439+
ErrorsIntakeUploader::build_errors_intake_endpoint(&telemetry_endpoint, false).unwrap();
419440
assert_eq!(errors_endpoint.url.path(), ERROR_INTAKE_AGENT_PATH);
420441
assert!(errors_endpoint.api_key.is_none());
421442

443+
// Verify the URL is correct for port 8126
444+
422445
// Test direct intake endpoint
423446
let telemetry_endpoint = Some(Endpoint {
424447
url: "https://instrumentation-telemetry-intake.datadoghq.com/api/v2/apmtelemetry"
@@ -428,14 +451,43 @@ mod tests {
428451
..Default::default()
429452
});
430453

454+
// Test with direct submission enabled
431455
let errors_endpoint =
432-
ErrorsIntakeUploader::build_errors_intake_endpoint(&telemetry_endpoint).unwrap();
456+
ErrorsIntakeUploader::build_errors_intake_endpoint(&telemetry_endpoint, true).unwrap();
433457
assert_eq!(errors_endpoint.url.path(), ERROR_INTAKE_DIRECT_PATH);
434458
assert!(errors_endpoint
435459
.url
436460
.host()
437461
.unwrap()
438462
.contains(ERROR_INTAKE_SUBDOMAIN));
439463
assert!(errors_endpoint.api_key.is_some());
464+
465+
// Test with direct submission disabled (should use agent proxy even with API key)
466+
let errors_endpoint_agent =
467+
ErrorsIntakeUploader::build_errors_intake_endpoint(&telemetry_endpoint, false).unwrap();
468+
assert_eq!(errors_endpoint_agent.url.path(), ERROR_INTAKE_AGENT_PATH);
469+
assert!(errors_endpoint_agent.api_key.is_some()); // API key is preserved but agent proxy is used
470+
471+
// Test with port 9126 (test environment)
472+
let telemetry_endpoint_9126 = Some(Endpoint {
473+
url: "http://localhost:9126/telemetry/proxy/api/v2/apmtelemetry"
474+
.parse()
475+
.unwrap(),
476+
api_key: None,
477+
..Default::default()
478+
});
479+
480+
let errors_endpoint_9126 =
481+
ErrorsIntakeUploader::build_errors_intake_endpoint(&telemetry_endpoint_9126, false)
482+
.unwrap();
483+
484+
// Verify the URL preserves the correct port
485+
assert_eq!(errors_endpoint_9126.url.host(), Some("localhost"));
486+
assert_eq!(errors_endpoint_9126.url.port_u16(), Some(9126));
487+
assert_eq!(errors_endpoint_9126.url.path(), ERROR_INTAKE_AGENT_PATH);
488+
489+
// This should be the URL that gets built for the test environment
490+
let expected_url = "http://localhost:9126/evp_proxy/v4/api/v2/errorsintake";
491+
assert_eq!(errors_endpoint_9126.url.to_string(), expected_url);
440492
}
441493
}

datadog-crashtracker/src/crash_info/telemetry.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,16 @@ impl TelemetryCrashUploader {
240240

241241
// Also send crash ping to errors intake if available
242242
if let Some(errors_uploader) = &self.errors_intake_uploader {
243+
eprintln!("DEBUG: Attempting to send crash ping to errors intake");
243244
let crash_metadata = self.telemetry_metadata_to_crashtracker_metadata();
244245
if let Err(e) = errors_uploader
245246
.send_crash_ping(crash_uuid, sig_info, &crash_metadata)
246247
.await
247248
{
248249
eprintln!("Failed to send crash ping to errors intake: {e}");
249250
}
251+
} else {
252+
eprintln!("DEBUG: No errors intake uploader available for crash ping");
250253
}
251254

252255
telemetry_result
@@ -294,9 +297,12 @@ impl TelemetryCrashUploader {
294297

295298
// Also send crash report to errors intake if available
296299
if let Some(errors_uploader) = &self.errors_intake_uploader {
300+
eprintln!("DEBUG: Attempting to send crash report to errors intake");
297301
if let Err(e) = errors_uploader.upload_to_errors_intake(crash_info).await {
298302
eprintln!("Failed to send crash report to errors intake: {e}");
299303
}
304+
} else {
305+
eprintln!("DEBUG: No errors intake uploader available for crash report");
300306
}
301307

302308
telemetry_result

0 commit comments

Comments
 (0)