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
172 const response = await mcpAgentHandler(new Request(forwarded, { headers }), env, ctx);172 const response = await mcpAgentHandler(new Request(forwarded, { headers }), env, ctx);
173 span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, response.status);173 span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, response.status);
174 if (response.status >= 500) {174 if (response.status >= 500) {
175 span.setStatus({ code: SpanStatusCode.ERROR });175 span.setStatus({ code: SpanStatusCode.ERROR, message: `HTTP ${response.status}` });
176 }176 }
177 return response;177 return response;
178 } catch (err) {178 } 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 });
180 // oxlint-disable-next-line executor/no-try-catch-or-throw -- adapter boundary; preserve original error to Cloudflare runtime187 // oxlint-disable-next-line executor/no-try-catch-or-throw -- adapter boundary; preserve original error to Cloudflare runtime
181 throw err;188 throw err;
182 } finally {189 } finally {
@@ -237,11 +244,18 @@ const cloudflareHandler: ExportedHandler<Env> = { · 54 unmodified lines
237 const response = await fetchHandler(request, env, ctx);244 const response = await fetchHandler(request, env, ctx);
238 span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, response.status);245 span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, response.status);
239 if (response.status >= 500) {246 if (response.status >= 500) {
240 span.setStatus({ code: SpanStatusCode.ERROR });247 span.setStatus({ code: SpanStatusCode.ERROR, message: `HTTP ${response.status}` });
241 }248 }
242 return response;249 return response;
243 } catch (err) {250 } 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 });
245 // oxlint-disable-next-line executor/no-try-catch-or-throw -- adapter boundary; preserve original error to Cloudflare runtime259 // oxlint-disable-next-line executor/no-try-catch-or-throw -- adapter boundary; preserve original error to Cloudflare runtime
246 throw err;260 throw err;
247 } finally {261 } 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