mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 23:51:13 -05:00
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:
@@ -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(),
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user