@@ -203,23 +203,32 @@ impl ErrorsIntakeUploader {
203
203
_crashtracker_metadata : & Metadata ,
204
204
telemetry_endpoint : & Option < Endpoint > ,
205
205
) -> 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) ?;
207
212
eprintln ! (
208
213
"DEBUG: Created errors intake uploader with URL: {}" ,
209
214
endpoint. url
210
215
) ;
216
+ eprintln ! (
217
+ "DEBUG: Direct submission enabled: {}" ,
218
+ direct_submission_enabled
219
+ ) ;
211
220
Ok ( Self { endpoint } )
212
221
}
213
222
214
223
fn build_errors_intake_endpoint (
215
224
telemetry_endpoint : & Option < Endpoint > ,
225
+ direct_submission_enabled : bool ,
216
226
) -> anyhow:: Result < Endpoint > {
217
227
match telemetry_endpoint {
218
228
Some ( endpoint) => {
219
229
let mut errors_endpoint = endpoint. clone ( ) ;
220
230
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 {
223
232
// Direct intake - change hostname to event-platform-intake
224
233
let mut parts = endpoint. url . clone ( ) . into_parts ( ) ;
225
234
@@ -333,26 +342,38 @@ impl ErrorsIntakeUploader {
333
342
return Ok ( ( ) ) ;
334
343
}
335
344
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
+ ) ;
340
362
341
363
// Add errors intake specific headers
342
364
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
+ }
344
369
} else {
345
370
// Agent proxy - add EvP subdomain header
346
371
req_builder = req_builder. header ( "X-Datadog-EVP-Subdomain" , ERROR_INTAKE_SUBDOMAIN ) ;
347
372
}
348
373
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( ) ) ;
356
377
357
378
// Create HTTP client and send request
358
379
let client = ddcommon:: hyper_migration:: new_client_periodic ( ) ;
@@ -415,10 +436,12 @@ mod tests {
415
436
} ) ;
416
437
417
438
let errors_endpoint =
418
- ErrorsIntakeUploader :: build_errors_intake_endpoint ( & telemetry_endpoint) . unwrap ( ) ;
439
+ ErrorsIntakeUploader :: build_errors_intake_endpoint ( & telemetry_endpoint, false ) . unwrap ( ) ;
419
440
assert_eq ! ( errors_endpoint. url. path( ) , ERROR_INTAKE_AGENT_PATH ) ;
420
441
assert ! ( errors_endpoint. api_key. is_none( ) ) ;
421
442
443
+ // Verify the URL is correct for port 8126
444
+
422
445
// Test direct intake endpoint
423
446
let telemetry_endpoint = Some ( Endpoint {
424
447
url : "https://instrumentation-telemetry-intake.datadoghq.com/api/v2/apmtelemetry"
@@ -428,14 +451,43 @@ mod tests {
428
451
..Default :: default ( )
429
452
} ) ;
430
453
454
+ // Test with direct submission enabled
431
455
let errors_endpoint =
432
- ErrorsIntakeUploader :: build_errors_intake_endpoint ( & telemetry_endpoint) . unwrap ( ) ;
456
+ ErrorsIntakeUploader :: build_errors_intake_endpoint ( & telemetry_endpoint, true ) . unwrap ( ) ;
433
457
assert_eq ! ( errors_endpoint. url. path( ) , ERROR_INTAKE_DIRECT_PATH ) ;
434
458
assert ! ( errors_endpoint
435
459
. url
436
460
. host( )
437
461
. unwrap( )
438
462
. contains( ERROR_INTAKE_SUBDOMAIN ) ) ;
439
463
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) ;
440
492
}
441
493
}
0 commit comments