fix workbench otel http span timing (#1815)
This commit is contained in:
@@ -131,23 +131,32 @@ export function createCloudApiServer(options = {}) {
|
||||
const requestContext = buildHttpRequestContext(request);
|
||||
attachHttpRequestContext(request, response, requestContext);
|
||||
const backendPerformance = backendPerformanceStore.beginHttpRequest(request, response);
|
||||
response.once("finish", () => {
|
||||
let otelHttpSpanEmitted = false;
|
||||
const emitHttpSpan = (closedBy) => {
|
||||
if (otelHttpSpanEmitted) return;
|
||||
otelHttpSpanEmitted = true;
|
||||
const httpCtx = request.hwlabHttpRequestContext;
|
||||
if (!httpCtx?.traceId) return;
|
||||
if (isOtelNoisyProbeRoute(httpCtx.route)) return;
|
||||
const lastError = httpCtx.lastHttpError;
|
||||
void emitHttpServerRequestSpan(httpCtx, env, {
|
||||
startTimeMs: backendPerformance.startedAt,
|
||||
startTimeMs: httpCtx.startedAtMs,
|
||||
endTimeMs: Date.now(),
|
||||
statusCode: Number(response.statusCode ?? backendPerformance.statusCode ?? 0),
|
||||
method: backendPerformance.method || httpCtx.method,
|
||||
route: backendPerformance.route || httpCtx.route,
|
||||
attributes: {
|
||||
"http.closed_by": closedBy,
|
||||
...(httpCtx.otelAttributes ?? {})
|
||||
},
|
||||
errorCode: lastError?.code,
|
||||
errorCategory: lastError?.category,
|
||||
errorLayer: lastError?.layer,
|
||||
statusMessage: lastError?.code
|
||||
}).catch(() => undefined);
|
||||
});
|
||||
};
|
||||
response.once("finish", () => emitHttpSpan("finish"));
|
||||
response.once("close", () => emitHttpSpan("close"));
|
||||
await withBackendPerformanceContext(backendPerformance, async () => {
|
||||
try {
|
||||
await routeRequest(request, response, { ...options, env, runtimeStore, gatewayRegistry, hwpodNodeWsRegistry, accessController, sessionRegistry, traceStore, codexStdioManager, codeAgentChatResults, webPerformanceStore, backendPerformanceStore, backendPerformance });
|
||||
@@ -178,6 +187,7 @@ function buildHttpRequestContext(request) {
|
||||
});
|
||||
return {
|
||||
requestId: safeRequestId(getHeader(request, "x-request-id")) || `req_${randomUUID()}`,
|
||||
startedAtMs: Date.now(),
|
||||
traceId: otelContext.traceId,
|
||||
parentSpanId: otelContext.parentSpanId,
|
||||
spanId: otelContext.spanId,
|
||||
|
||||
Reference in New Issue
Block a user