Skip to content

Commit 2cb48d2

Browse files
authored
fix: add organization_id to provider error and stream error logs (#522)
* fix: add organization_id to provider error and stream error logs Add organization_id to all log statements in: - map_provider_error(): all 17 error/warn logs now include org_id for correlating provider failures to specific tenants - create_signature_future(): attestation storage errors now include org_id and model_id for debugging E2EE issues - Route handler streaming errors: serialization failures, stream errors, and multi-error completions now include org_id This enables filtering Datadog logs by organization to identify whether failures are tenant-specific or systemic. * fix: address review comments on org_id logging - Rename org_id_for_stream → organization_id for tracing shorthand (%organization_id) - Use .0 on OrganizationId for raw UUID in log fields - Clone model string into chain closure instead of capturing full request - Use structured error field (error = ?e) instead of {:?} in message for signature storage log * chore: gitignore repro scripts * fix: sort mod general alphabetically in e2e_all/main.rs * fix: add missing organization_id arg to NoPubKeyProvider test
1 parent 93dae5f commit 2cb48d2

3 files changed

Lines changed: 95 additions & 42 deletions

File tree

crates/api/src/routes/completions.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ pub async fn chat_completions(
394394

395395
if inference_id.is_none() {
396396
tracing::warn!(
397+
organization_id = %api_key.organization.id.0,
398+
model = %request.model,
397399
"Could not extract inference ID from first chunk for chat completion (streaming)"
398400
);
399401
}
@@ -407,6 +409,7 @@ pub async fn chat_completions(
407409
let chat_id_clone = chat_id_state.clone();
408410
let error_count_clone = stream_error_count.clone();
409411
let request_model = request.model.clone();
412+
let organization_id = api_key.organization.id.0;
410413

411414
// Convert to raw bytes stream with proper SSE formatting
412415
let byte_stream = peekable_stream
@@ -432,6 +435,7 @@ pub async fn chat_completions(
432435
let json_data = serde_json::to_string(&event.chunk)
433436
.unwrap_or_else(|e| {
434437
tracing::error!(
438+
%organization_id,
435439
"Failed to serialize stream chunk: {e}"
436440
);
437441
"{}".to_string()
@@ -447,6 +451,7 @@ pub async fn chat_completions(
447451
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
448452
if count == 0 {
449453
tracing::error!(
454+
%organization_id,
450455
model = %model_for_err,
451456
error_type = %completion_stream_error_category(&e),
452457
"Completion stream error"
@@ -459,24 +464,29 @@ pub async fn chat_completions(
459464
}
460465
}
461466
})
462-
.chain(futures::stream::once(async move {
463-
let error_count_final =
464-
stream_error_count.load(std::sync::atomic::Ordering::Relaxed);
465-
if error_count_final > 1 {
466-
tracing::error!(
467-
model = %request.model,
468-
total_stream_errors = error_count_final,
469-
"Completion stream ended with multiple errors"
470-
);
471-
}
467+
.chain(futures::stream::once({
468+
let organization_id = api_key.organization.id.0;
469+
let model_name = request.model.clone();
470+
async move {
471+
let error_count_final =
472+
stream_error_count.load(std::sync::atomic::Ordering::Relaxed);
473+
if error_count_final > 1 {
474+
tracing::error!(
475+
%organization_id,
476+
model = %model_name,
477+
total_stream_errors = error_count_final,
478+
"Completion stream ended with multiple errors"
479+
);
480+
}
472481

473-
let done_bytes = Bytes::from_static(b"data: [DONE]\n\n");
474-
accumulated_bytes
475-
.lock()
476-
.await
477-
.extend_from_slice(&done_bytes);
482+
let done_bytes = Bytes::from_static(b"data: [DONE]\n\n");
483+
accumulated_bytes
484+
.lock()
485+
.await
486+
.extend_from_slice(&done_bytes);
478487

479-
Ok::<Bytes, Infallible>(done_bytes)
488+
Ok::<Bytes, Infallible>(done_bytes)
489+
}
480490
}));
481491

482492
// Return raw streaming response with SSE headers

crates/api/tests/e2e_all/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ mod error_msg;
2525
mod external_providers;
2626
mod files;
2727
mod function_tools;
28+
mod general;
2829
mod mcp;
2930
mod mcp_server;
3031
mod message_metadata;
@@ -40,7 +41,6 @@ mod rerank;
4041
mod response_signature_verification;
4142
mod score;
4243
mod signature_verification;
43-
mod general;
4444
mod usage_chat_completions;
4545
mod usage_recording;
4646
mod usage_responses;

0 commit comments

Comments
 (0)