diff --git a/packages/browser/src/integrations/breadcrumbs.ts b/packages/browser/src/integrations/breadcrumbs.ts index 528baa92df41..f522fbf25057 100644 --- a/packages/browser/src/integrations/breadcrumbs.ts +++ b/packages/browser/src/integrations/breadcrumbs.ts @@ -325,7 +325,35 @@ function addSentryBreadcrumb(serializedData: string): void { event, }, ); - } catch (_oO) { - logger.error('Error while adding sentry type breadcrumb'); + } catch (error) { + logger.error('Error while adding sentry type breadcrumb will try envelope', error); + addSentryBreadcrumbWithEnvelope(serializedData); + } +} + +/** + * Does the same as {@link addSentryBreadcrumb} but works with envelope + */ +function addSentryBreadcrumbWithEnvelope(envelope: string): void { + try { + // We are dealing with an envelope here + // For simplicity we only deal with transactions + const envelopeLines = envelope.split('\n'); + const envelopeHeader = JSON.parse(envelopeLines[0]); + const itemHeader = JSON.parse(envelopeLines[1]); + const item = JSON.parse(envelopeLines[2]); + getCurrentHub().addBreadcrumb( + { + category: `sentry.${itemHeader.type}`, + event_id: envelopeHeader.event_id, + level: item.level, + message: getEventDescription(item), + }, + { + item, + }, + ); + } catch (error) { + logger.error('Error while adding sentry type breadcrumb', error); } } diff --git a/packages/browser/test/integration/karma.conf.js b/packages/browser/test/integration/karma.conf.js index 0a65088f5884..2f802487c3ba 100644 --- a/packages/browser/test/integration/karma.conf.js +++ b/packages/browser/test/integration/karma.conf.js @@ -73,6 +73,7 @@ module.exports = config => { "/base/variants/123": "/base/subjects/123", // Supresses warnings "/api/1/store/": "/", + "/api/1/envelope/": "/", }, frameworks: ["mocha", "chai", "sinon"], files, diff --git a/packages/browser/test/integration/suites/breadcrumbs.js b/packages/browser/test/integration/suites/breadcrumbs.js index 2997ca626685..187460772378 100644 --- a/packages/browser/test/integration/suites/breadcrumbs.js +++ b/packages/browser/test/integration/suites/breadcrumbs.js @@ -156,6 +156,43 @@ describe("breadcrumbs", function() { } ); + it( + optional( + "should transform XMLHttpRequests with transactions type to the Sentry envelope endpoint as sentry.transaction type breadcrumb", + IS_LOADER + ), + function() { + return runInSandbox(sandbox, { manual: true }, function() { + var envelope = + document.location.protocol + + "//" + + document.location.hostname + + (document.location.port ? ":" + document.location.port : "") + + "/api/1/envelope/" + + "?sentry_key=1337"; + + var xhr = new XMLHttpRequest(); + xhr.open("POST", envelope); + xhr.send( + '{"event_id": "aa3ff046696b4bc6b609ce6d28fde9e2","sent_at": "2020-05-19T15:44:49.028Z"}\n{"type": "transaction"}\n{"message":"someMessage","transaction":"wat","level":"warning"}' + ); + waitForXHR(xhr, function() { + Sentry.captureMessage("test"); + window.finalizeManualTest(); + }); + }).then(function(summary) { + // The async loader doesn't wrap XHR + if (IS_LOADER) { + return; + } + assert.equal(summary.breadcrumbs.length, 1); + assert.equal(summary.breadcrumbs[0].category, "sentry.transaction"); + assert.equal(summary.breadcrumbs[0].level, "warning"); + assert.equal(summary.breadcrumbs[0].message, "someMessage"); + }); + } + ); + it( optional( "should not transform XMLHttpRequests with transactions attribute to the Sentry store endpoint as sentry.transaction type breadcrumb",