Skip to content

test(breadcrumb): Add test case for creating breadcrumbs for requests to the envelope endpoint #2603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions packages/browser/src/integrations/breadcrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,18 +313,38 @@ export class Breadcrumbs implements Integration {
function addSentryBreadcrumb(serializedData: string): void {
// There's always something that can go wrong with deserialization...
try {
const event = JSON.parse(serializedData);
getCurrentHub().addBreadcrumb(
{
category: `sentry.${event.type === 'transaction' ? 'transaction' : 'event'}`,
event_id: event.event_id,
level: event.level || Severity.fromString('error'),
message: getEventDescription(event),
},
{
event,
},
);
try {
const event = JSON.parse(serializedData);
getCurrentHub().addBreadcrumb(
{
category: `sentry.${event.type === 'transaction' ? 'transaction' : 'event'}`,
event_id: event.event_id,
level: event.level || Severity.fromString('error'),
message: getEventDescription(event),
},
{
event,
},
);
} catch (_oO) {
// We are dealing with an envelope here
// For simplicity we only deal with transactions
const envelopeLines = serializedData.split('\n');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HazAT Is it guaranteed that the none of the entries will contain newlines?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, per envelope spec.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HazAT
I read over the spec. There's a chance the item payload could contain newlines; and this could fail:

const item = JSON.parse(envelopeLines[2]);

Maybe something like this?

const [envelopeHeaderRaw, itemHeaderRaw, ...rest] = serializedData.split('\n');

const envelopeHeader = JSON.parse(envelopeHeaderRaw);
const itemHeader = JSON.parse(itemHeaderRaw);
const body = rest.join('\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 (_oO) {
logger.error('Error while adding sentry type breadcrumb');
}
Expand Down
1 change: 1 addition & 0 deletions packages/browser/test/integration/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
37 changes: 37 additions & 0 deletions packages/browser/test/integration/suites/breadcrumbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down