Showing the changes to apps/cloud/src (3 of 9 files). Show all
Unified · Split
| @@ -16,7 +16,7 @@ import { Cause, Effect, Layer } from "effect"; · 15 unmodified lines | |||
| 16 | import type * as Tracer from "effect/Tracer"; | 16 | import type * as Tracer from "effect/Tracer"; |
| 17 | 17 | ||
| 18 | import { ErrorCapture } from "@executor-js/api"; | 18 | import { ErrorCapture } from "@executor-js/api"; |
| 19 | import { stableGroupingFingerprint, type GroupingEvent } from "@executor-js/sdk/sentry-grouping"; | 19 | import { withStableGroupingFingerprint } from "@executor-js/sdk/sentry-grouping"; |
| 20 | 20 | ||
| 21 | // Drizzle/postgres-js include the failing SQL (params + bound values) in | 21 | // Drizzle/postgres-js include the failing SQL (params + bound values) in |
| 22 | // their error message. For OpenAPI source inserts that's 1MB+ of spec | 22 | // their error message. For OpenAPI source inserts that's 1MB+ of spec |
| @@ -148,19 +148,13 @@ export const beforeSendWithOtelCorrelation = ( · 125 unmodified lines | |||
| 148 | }; | 148 | }; |
| 149 | 149 | ||
| 150 | /** | 150 | /** |
| 151 | * The single `beforeSend` the worker and its Durable Objects install. | ||
| 152 | * | ||
| 151 | * The worker ships as content-hashed chunks and its frames are not resolved | 153 | * The worker ships as content-hashed chunks and its frames are not resolved |
| 152 | * back to source, so Sentry's default grouping keys on names like | 154 | * back to source, so Sentry's default grouping keys on names like |
| 153 | * `execution-rate-limit-<hash>` and re-opens every issue on the next deploy. | 155 | * `execution-rate-limit-<hash>` and re-opens every issue on the next deploy. |
| 154 | * Pin a fingerprint with the hash normalized out; events with no hashed | 156 | * `withStableGroupingFingerprint` pins a key with the hash normalized out; |
| 155 | * grouping input are left on the default algorithm. | 157 | * events with no hashed grouping input are left on the default algorithm. |
| 156 | */ | ||
| 157 | export const withStableGroupingFingerprint = <T extends GroupingEvent>(event: T): T => { | ||
| 158 | const fingerprint = stableGroupingFingerprint(event); | ||
| 159 | return fingerprint ? { ...event, fingerprint: [...fingerprint] } : event; | ||
| 160 | }; | ||
| 161 | |||
| 162 | /** | ||
| 163 | * The single `beforeSend` the worker and its Durable Objects install. | ||
| 164 | * | 158 | * |
| 165 | * The two stages are independent and compose in this order: the capture-owner | 159 | * The two stages are independent and compose in this order: the capture-owner |
| 166 | * pass decides WHETHER the event is reported at all (a cause the Durable | 160 | * pass decides WHETHER the event is reported at all (a cause the Durable |
| @@ -176,6 +170,30 @@ export const beforeSendCloudEvent = ( · 9 unmodified lines | |||
| 176 | return reported === null ? null : withStableGroupingFingerprint(reported); | 170 | return reported === null ? null : withStableGroupingFingerprint(reported); |
| 177 | }; | 171 | }; |
| 178 | 172 | ||
| 173 | /** | ||
| 174 | * The Sentry options the worker and every Durable Object install. It lives | ||
| 175 | * beside the `beforeSend` it wires so the composition is covered by | ||
| 176 | * observability.test.ts; `server.ts` only passes this through. | ||
| 177 | * | ||
| 178 | * NOTE: do NOT enable `instrumentPrototypeMethods`. It walks the DO prototype | ||
| 179 | * and reads every property — including accessors — to find methods to wrap, | ||
| 180 | * which invokes the `sessionId` getter with `this` bound to the prototype | ||
| 181 | * (where `ctx` is undefined) and throws during construction, 500ing every | ||
| 182 | * session create / cold restore. The DO captures its own errors via the | ||
| 183 | * `captureCause` seam (→ Sentry) instead. | ||
| 184 | */ | ||
| 185 | export const cloudSentryOptions = (env: Env) => ({ | ||
| 186 | dsn: env.SENTRY_DSN, | ||
| 187 | tracesSampleRate: 0, | ||
| 188 | enableLogs: true, | ||
| 189 | sendDefaultPii: true, | ||
| 190 | skipOpenTelemetrySetup: true, | ||
| 191 | beforeSend: (event: ErrorEvent) => | ||
| 192 | beforeSendCloudEvent(event, { | ||
| 193 | logPayload: !env.SENTRY_DSN || env.SENTRY_OTEL_LOG_PAYLOAD === "true", | ||
| 194 | }), | ||
| 195 | }); | ||
| 196 | |||
| 179 | export const addCurrentOtelCorrelationTags = < | 197 | export const addCurrentOtelCorrelationTags = < |
| 180 | T extends { readonly tags?: Record<string, unknown> }, | 198 | T extends { readonly tags?: Record<string, unknown> }, |
| 181 | >( | 199 | >( |
| @@ -8,6 +8,7 @@ import { · 7 unmodified lines | |||
| 8 | addCurrentOtelCorrelationTags, | 8 | addCurrentOtelCorrelationTags, |
| 9 | beforeSendCloudEvent, | 9 | beforeSendCloudEvent, |
| 10 | beforeSendWithOtelCorrelation, | 10 | beforeSendWithOtelCorrelation, |
| 11 | cloudSentryOptions, | ||
| 11 | DO_CAUSE_OWNER_TAG, | 12 | DO_CAUSE_OWNER_TAG, |
| 12 | DO_CAUSE_OWNER_VALUE, | 13 | DO_CAUSE_OWNER_VALUE, |
| 13 | OTEL_SPAN_ID_TAG, | 14 | OTEL_SPAN_ID_TAG, |
| @@ -86,8 +87,10 @@ describe("sentryPayloadForCause", () => { · 72 unmodified lines | |||
| 86 | }); | 87 | }); |
| 87 | 88 | ||
| 88 | // Grouping keys are decided inside the Sentry SDK and never appear on any | 89 | // Grouping keys are decided inside the Sentry SDK and never appear on any |
| 89 | // product surface, so the e2e harness cannot observe them; the running | 90 | // product surface, so the e2e harness cannot observe them. The split is: |
| 90 | // beforeSend itself is covered by e2e/cloud/sentry-otel-correlation.test.ts. | 91 | // e2e/cloud/sentry-otel-correlation.test.ts proves the worker really installs |
| 92 | // `cloudSentryOptions.beforeSend` (its correlation payload only exists if that | ||
| 93 | // hook ran), and the tests here prove the hook it installs fingerprints. | ||
| 91 | describe("Sentry grouping", () => { | 94 | describe("Sentry grouping", () => { |
| 92 | // The worker bundle ships as content-hashed chunks, so the only module name | 95 | // The worker bundle ships as content-hashed chunks, so the only module name |
| 93 | // Sentry ever sees for a given frame changes on every deploy. | 96 | // Sentry ever sees for a given frame changes on every deploy. |
| @@ -143,6 +146,21 @@ describe("Sentry grouping", () => { · 49 unmodified lines | |||
| 143 | expect(sent).not.toBeNull(); | 146 | expect(sent).not.toBeNull(); |
| 144 | expect(sent?.fingerprint).toBeUndefined(); | 147 | expect(sent?.fingerprint).toBeUndefined(); |
| 145 | }); | 148 | }); |
| 149 | |||
| 150 | // The wiring check: this is the exact object handed to `Sentry.withSentry` | ||
| 151 | // and `instrumentDurableObjectWithSentry` in server.ts. If the normalizer is | ||
| 152 | // ever dropped from the hook the worker installs, this fails. | ||
| 153 | it("the options the worker and DOs install carry the fingerprinting hook", () => { | ||
| 154 | const options = cloudSentryOptions({ SENTRY_DSN: "https://public@example.invalid/1" } as Env); | ||
| 155 | const sent = options.beforeSend(workerEvent("BAuwphPA")); | ||
| 156 | |||
| 157 | expect(sent?.fingerprint).toEqual([ | ||
| 158 | "GateCheckTimeoutError", | ||
| 159 | "timeoutOrElse@execution-rate-limit", | ||
| 160 | ]); | ||
| 161 | // Same source, next deploy, new chunk hash — one issue, not two. | ||
| 162 | expect(options.beforeSend(workerEvent("DkcPBbWe"))?.fingerprint).toEqual(sent?.fingerprint); | ||
| 163 | }); | ||
| 146 | }); | 164 | }); |
| 147 | 165 | ||
| 148 | describe("Sentry OTel correlation", () => { | 166 | describe("Sentry OTel correlation", () => { |
| @@ -1,6 +1,5 @@ | |||
| 1 | import { DurableObject } from "cloudflare:workers"; | 1 | import { DurableObject } from "cloudflare:workers"; |
| 2 | import { SpanKind, SpanStatusCode, context, trace, type SpanContext } from "@opentelemetry/api"; | 2 | import { SpanKind, SpanStatusCode, context, trace, type SpanContext } from "@opentelemetry/api"; |
| 3 | import type { ErrorEvent } from "@sentry/cloudflare"; | ||
| 4 | import { | 3 | import { |
| 5 | ATTR_HTTP_REQUEST_METHOD, | 4 | ATTR_HTTP_REQUEST_METHOD, |
| 6 | ATTR_HTTP_RESPONSE_STATUS_CODE, | 5 | ATTR_HTTP_RESPONSE_STATUS_CODE, |
| @@ -19,7 +18,7 @@ import { classifyMcpPath, prepareMcpOrgScope } from "./mcp/mount"; · 12 unmodified lines | |||
| 19 | import { parseTraceparent } from "./mcp/traceparent"; | 18 | import { parseTraceparent } from "./mcp/traceparent"; |
| 20 | import { McpSessionDOSqlite as McpSessionDOBase } from "./mcp/session-durable-object"; | 19 | import { McpSessionDOSqlite as McpSessionDOBase } from "./mcp/session-durable-object"; |
| 21 | import { | 20 | import { |
| 22 | beforeSendCloudEvent, | 21 | cloudSentryOptions, |
| 23 | captureCause, | 22 | captureCause, |
| 24 | otelCorrelationContextFromOpenTelemetrySpan, | 23 | otelCorrelationContextFromOpenTelemetrySpan, |
| 25 | SENTRY_EVENT_ID_ATTRIBUTE, | 24 | SENTRY_EVENT_ID_ATTRIBUTE, |
| @@ -28,28 +27,6 @@ import { · 2 unmodified lines | |||
| 28 | import { browserTracesResponse } from "./observability/browser-traces"; | 27 | import { browserTracesResponse } from "./observability/browser-traces"; |
| 29 | import { flushTracerProvider, installTracerProvider } from "./observability/telemetry"; | 28 | import { flushTracerProvider, installTracerProvider } from "./observability/telemetry"; |
| 30 | 29 | ||
| 31 | // --------------------------------------------------------------------------- | ||
| 32 | // Sentry config | ||
| 33 | // --------------------------------------------------------------------------- | ||
| 34 | |||
| 35 | const sentryOptions = (env: Env) => ({ | ||
| 36 | dsn: env.SENTRY_DSN, | ||
| 37 | tracesSampleRate: 0, | ||
| 38 | enableLogs: true, | ||
| 39 | sendDefaultPii: true, | ||
| 40 | skipOpenTelemetrySetup: true, | ||
| 41 | beforeSend: (event: ErrorEvent) => | ||
| 42 | beforeSendCloudEvent(event, { | ||
| 43 | logPayload: !env.SENTRY_DSN || env.SENTRY_OTEL_LOG_PAYLOAD === "true", | ||
| 44 | }), | ||
| 45 | // NOTE: do NOT enable `instrumentPrototypeMethods`. It walks the DO prototype | ||
| 46 | // and reads every property — including accessors — to find methods to wrap, | ||
| 47 | // which invokes the `sessionId` getter with `this` bound to the prototype | ||
| 48 | // (where `ctx` is undefined) and throws during construction, 500ing every | ||
| 49 | // session create / cold restore. The DO captures its own errors via the | ||
| 50 | // `captureCause` seam (→ Sentry) instead. | ||
| 51 | }); | ||
| 52 | |||
| 53 | // --------------------------------------------------------------------------- | 30 | // --------------------------------------------------------------------------- |
| 54 | // Durable Object — wrapped with Sentry so DO errors land in Sentry (inits the | 31 | // Durable Object — wrapped with Sentry so DO errors land in Sentry (inits the |
| 55 | // client inside the DO isolate, which plain `Sentry.captureException` cannot | 32 | // client inside the DO isolate, which plain `Sentry.captureException` cannot |
| @@ -58,7 +35,7 @@ const sentryOptions = (env: Env) => ({ · 2 unmodified lines | |||
| 58 | // --------------------------------------------------------------------------- | 35 | // --------------------------------------------------------------------------- |
| 59 | 36 | ||
| 60 | export const McpSessionDOSqlite = Sentry.instrumentDurableObjectWithSentry( | 37 | export const McpSessionDOSqlite = Sentry.instrumentDurableObjectWithSentry( |
| 61 | sentryOptions, | 38 | cloudSentryOptions, |
| 62 | McpSessionDOBase, | 39 | McpSessionDOBase, |
| 63 | ); | 40 | ); |
| 64 | 41 | ||
| @@ -458,4 +435,4 @@ const cloudflareHandler: ExportedHandler<Env> = { · 393 unmodified lines | |||
| 458 | }, | 435 | }, |
| 459 | }; | 436 | }; |
| 460 | 437 | ||
| 461 | export default Sentry.withSentry(sentryOptions, cloudflareHandler); | 438 | export default Sentry.withSentry(cloudSentryOptions, cloudflareHandler); |