Capture backend response metadata

Update edge function wrapper to emit backend metadata headers (X-Request-Id, X-Span-Id, X-Trace-Id, X-Duration-Ms) on responses; adjust logging to include duration and headers. Enhance edgeFunctionTracking to extract and propagate backend metadata from responses and errors; extend errorHandler to capture and log backend metadata for improved observability.
This commit is contained in:
gpt-engineer-app[bot]
2025-11-11 04:18:20 +00:00
parent 08926610b9
commit ca6e95f4f8
3 changed files with 140 additions and 8 deletions

View File

@@ -175,19 +175,35 @@ export function wrapEdgeFunction(
statusText: response.statusText
});
const duration = span.endTime ? span.duration : Date.now() - span.startTime;
if (logResponses) {
edgeLogger.info('Request completed', {
requestId,
action: name,
status: response.status,
duration: span.endTime ? span.duration : Date.now() - span.startTime,
duration,
});
}
endSpan(span, 'ok');
logSpan(span);
return response;
// Clone response to add tracking headers
const responseBody = await response.text();
const enhancedResponse = new Response(responseBody, {
status: response.status,
statusText: response.statusText,
headers: {
...Object.fromEntries(response.headers.entries()),
'X-Request-Id': requestId,
'X-Span-Id': span.spanId,
'X-Trace-Id': span.traceId,
'X-Duration-Ms': duration.toString(),
},
});
return enhancedResponse;
} catch (error) {
// ====================================================================
@@ -206,6 +222,8 @@ export function wrapEdgeFunction(
endSpan(span, 'error', error);
logSpan(span);
const duration = span.endTime ? span.duration : Date.now() - span.startTime;
return new Response(
JSON.stringify({
error: error.message,
@@ -216,7 +234,14 @@ export function wrapEdgeFunction(
}),
{
status: 400,
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
headers: {
...corsHeaders,
'Content-Type': 'application/json',
'X-Request-Id': requestId,
'X-Span-Id': span.spanId,
'X-Trace-Id': span.traceId,
'X-Duration-Ms': duration.toString(),
},
}
);
}
@@ -267,6 +292,8 @@ export function wrapEdgeFunction(
endSpan(span, 'error', error);
logSpan(span);
const duration = span.endTime ? span.duration : Date.now() - span.startTime;
return new Response(
JSON.stringify({
error: message,
@@ -276,7 +303,14 @@ export function wrapEdgeFunction(
}),
{
status,
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
headers: {
...corsHeaders,
'Content-Type': 'application/json',
'X-Request-Id': requestId,
'X-Span-Id': span.spanId,
'X-Trace-Id': span.traceId,
'X-Duration-Ms': duration.toString(),
},
}
);
}
@@ -300,6 +334,8 @@ export function wrapEdgeFunction(
endSpan(span, 'error', error);
logSpan(span);
const duration = span.endTime ? span.duration : Date.now() - span.startTime;
return new Response(
JSON.stringify({
error: 'Internal server error',
@@ -308,7 +344,14 @@ export function wrapEdgeFunction(
}),
{
status: 500,
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
headers: {
...corsHeaders,
'Content-Type': 'application/json',
'X-Request-Id': requestId,
'X-Span-Id': span.spanId,
'X-Trace-Id': span.traceId,
'X-Duration-Ms': duration.toString(),
},
}
);
}