History

Record exceptions on worker http.server error spans

The worker-boundary catch set ERROR with no message and no exception event, leaving those spans with zero diagnostic content. Record the exception and a status message, and stamp HTTP <status> on the 5xx path.

Rhys Sullivan committed 3 months ago

Commit b004f6a1 parent fdd3ad41 file · +18 −4Browse files

Unified · Split

apps/cloud/src/server.ts+18−4
@@ -172,11 +172,18 @@ const cloudflareHandler: ExportedHandler<Env> = { · 171 unmodified lines
172172 const response = await mcpAgentHandler(new Request(forwarded, { headers }), env, ctx);
173173 span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, response.status);
174174 if (response.status >= 500) {
175− span.setStatus({ code: SpanStatusCode.ERROR });
175+ span.setStatus({ code: SpanStatusCode.ERROR, message: `HTTP ${response.status}` });
176176 }
177177 return response;
178178 } catch (err) {
179− span.setStatus({ code: SpanStatusCode.ERROR });
179+ // Record the exception itself, not just the status bit: without it
180+ // these spans are ERROR with zero diagnostic content.
181+ // oxlint-disable-next-line executor/no-instanceof-error, executor/no-unknown-error-message -- adapter boundary: Cloudflare's fetch callback throws untyped; normalized only for the OTel span record, the original error is rethrown below
182+ const cause = err instanceof Error ? err : String(err);
183+ span.recordException(cause);
184+ // oxlint-disable-next-line executor/no-unknown-error-message -- adapter boundary: same normalization as the recordException line above
185+ const message = typeof cause === "string" ? cause : cause.message;
186+ span.setStatus({ code: SpanStatusCode.ERROR, message });
180187 // oxlint-disable-next-line executor/no-try-catch-or-throw -- adapter boundary; preserve original error to Cloudflare runtime
181188 throw err;
182189 } finally {
@@ -237,11 +244,18 @@ const cloudflareHandler: ExportedHandler<Env> = { · 54 unmodified lines
237244 const response = await fetchHandler(request, env, ctx);
238245 span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, response.status);
239246 if (response.status >= 500) {
240− span.setStatus({ code: SpanStatusCode.ERROR });
247+ span.setStatus({ code: SpanStatusCode.ERROR, message: `HTTP ${response.status}` });
241248 }
242249 return response;
243250 } catch (err) {
244− span.setStatus({ code: SpanStatusCode.ERROR });
251+ // Record the exception itself, not just the status bit: without it
252+ // these spans are ERROR with zero diagnostic content.
253+ // oxlint-disable-next-line executor/no-instanceof-error, executor/no-unknown-error-message -- adapter boundary: Cloudflare's fetch callback throws untyped; normalized only for the OTel span record, the original error is rethrown below
254+ const cause = err instanceof Error ? err : String(err);
255+ span.recordException(cause);
256+ // oxlint-disable-next-line executor/no-unknown-error-message -- adapter boundary: same normalization as the recordException line above
257+ const message = typeof cause === "string" ? cause : cause.message;
258+ span.setStatus({ code: SpanStatusCode.ERROR, message });
245259 // oxlint-disable-next-line executor/no-try-catch-or-throw -- adapter boundary; preserve original error to Cloudflare runtime
246260 throw err;
247261 } finally {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t