diff --git a/src/instrumentation-client.ts b/src/instrumentation-client.ts index c2369152b29f5..45844711a9197 100644 --- a/src/instrumentation-client.ts +++ b/src/instrumentation-client.ts @@ -36,6 +36,37 @@ Sentry.init({ Sentry.consoleLoggingIntegration(), ], + // When a Chrome promise rejection is fired by a resource load failure, the + // rejection reason is the DOM Event whose target is the HTML element that + // failed (e.g. a stylesheet). Without extra instrumentation, the + // serialised event shows only "[object HTMLLinkElement]" and gives no clue + // which URL caused the failure. This hook extracts the resource URL and + // adds it as a searchable tag and as extra context so we can identify the + // failing asset. + beforeSend: (event, hint) => { + const originalException = hint?.originalException; + if ( + originalException instanceof Event && + originalException.target instanceof HTMLElement + ) { + const target = originalException.target as HTMLElement & { + href?: string; + src?: string; + }; + const resourceUrl = target.href ?? target.src ?? target.tagName; + event.tags = { + ...event.tags, + failing_resource_url: resourceUrl, + failing_resource_tag: target.tagName.toLowerCase(), + }; + event.extra = { + ...event.extra, + failing_resource_element: target.outerHTML?.slice(0, 300), + }; + } + return event; + }, + // Filter sensitive metric attributes (no PII in metrics) beforeSendMetric: metric => { // Remove any accidentally added PII attributes