Skip to content

Commit c8cb2c3

Browse files
committed
[WIP] feat: Send transactions in envelopes
The new Envelope endpoint and format is what we want to use for transactions going forward. TODO: include rationale.
1 parent ea41928 commit c8cb2c3

File tree

3 files changed

+70
-14
lines changed

3 files changed

+70
-14
lines changed

packages/browser/src/transports/fetch.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,41 @@ export class FetchTransport extends BaseTransport {
2222
});
2323
}
2424

25-
const defaultOptions: RequestInit = {
26-
body: JSON.stringify(event),
25+
let payload = JSON.stringify(event);
26+
let url = this.url;
27+
let headers = this.options.headers || {};
28+
29+
if (event.type === 'transaction') {
30+
url = url.replace('/store', '/envelope');
31+
headers['content-type'] = 'application/x-sentry-envelope';
32+
33+
const envelopeHeaders = JSON.stringify({
34+
event_id: event.event_id,
35+
sent_at: null,
36+
});
37+
const itemHeaders = JSON.stringify({
38+
content_type: 'application/json',
39+
type: event.type,
40+
});
41+
const envelope = `${envelopeHeaders}\n${itemHeaders}\n${payload}\n`;
42+
payload = envelope;
43+
}
44+
45+
const options: RequestInit = {
46+
body: payload,
2747
method: 'POST',
2848
// Despite all stars in the sky saying that Edge supports old draft syntax, aka 'never', 'always', 'origin' and 'default
2949
// https://caniuse.com/#feat=referrer-policy
3050
// It doesn't. And it throw exception instead of ignoring this parameter...
3151
// REF: https://github.com/getsentry/raven-js/issues/1233
3252
referrerPolicy: (supportsReferrerPolicy() ? 'origin' : '') as ReferrerPolicy,
53+
headers: headers,
3354
};
3455

35-
if (this.options.headers !== undefined) {
36-
defaultOptions.headers = this.options.headers;
37-
}
38-
3956
return this._buffer.add(
4057
new SyncPromise<Response>((resolve, reject) => {
4158
global
42-
.fetch(this.url, defaultOptions)
59+
.fetch(url, options)
4360
.then(response => {
4461
const status = Status.fromHttpCode(response.status);
4562

packages/browser/src/transports/xhr.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@ export class XHRTransport extends BaseTransport {
2020
});
2121
}
2222

23+
let payload = JSON.stringify(event);
24+
let url = this.url;
25+
let headers = this.options.headers || {};
26+
27+
if (event.type === 'transaction') {
28+
url = url.replace('/store', '/envelope');
29+
headers['content-type'] = 'application/x-sentry-envelope';
30+
31+
const envelopeHeaders = JSON.stringify({
32+
event_id: event.event_id,
33+
sent_at: null,
34+
});
35+
const itemHeaders = JSON.stringify({
36+
content_type: 'application/json',
37+
type: event.type,
38+
});
39+
const envelope = `${envelopeHeaders}\n${itemHeaders}\n${payload}\n`;
40+
payload = envelope;
41+
}
42+
2343
return this._buffer.add(
2444
new SyncPromise<Response>((resolve, reject) => {
2545
const request = new XMLHttpRequest();
@@ -45,13 +65,13 @@ export class XHRTransport extends BaseTransport {
4565
reject(request);
4666
};
4767

48-
request.open('POST', this.url);
49-
for (const header in this.options.headers) {
50-
if (this.options.headers.hasOwnProperty(header)) {
51-
request.setRequestHeader(header, this.options.headers[header]);
68+
request.open('POST', url);
69+
for (const header in headers) {
70+
if (headers.hasOwnProperty(header)) {
71+
request.setRequestHeader(header, headers[header]);
5272
}
5373
}
54-
request.send(JSON.stringify(event));
74+
request.send(payload);
5575
}),
5676
);
5777
}

packages/node/src/transports/base.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,26 @@ export abstract class BaseTransport implements Transport {
8484
}
8585
return this._buffer.add(
8686
new Promise<Response>((resolve, reject) => {
87-
const req = httpModule.request(this._getRequestOptions(), (res: http.IncomingMessage) => {
87+
const options = this._getRequestOptions();
88+
let payload = JSON.stringify(event);
89+
90+
if (event.type === 'transaction') {
91+
options.path = (options.path || '').replace('/store', '/envelope');
92+
(options.headers || {})['content-type'] = 'application/x-sentry-envelope';
93+
94+
const envelopeHeaders = JSON.stringify({
95+
event_id: event.event_id,
96+
sent_at: null,
97+
});
98+
const itemHeaders = JSON.stringify({
99+
content_type: 'application/json',
100+
type: event.type,
101+
});
102+
const envelope = `${envelopeHeaders}\n${itemHeaders}\n${payload}\n`;
103+
payload = envelope;
104+
}
105+
106+
const req = httpModule.request(options, (res: http.IncomingMessage) => {
88107
const statusCode = res.statusCode || 500;
89108
const status = Status.fromHttpCode(statusCode);
90109

@@ -118,7 +137,7 @@ export abstract class BaseTransport implements Transport {
118137
});
119138
});
120139
req.on('error', reject);
121-
req.end(JSON.stringify(event));
140+
req.end(payload);
122141
}),
123142
);
124143
}

0 commit comments

Comments
 (0)