Skip to content

Commit 77fbd7b

Browse files
committed
strip ='s from tracestate rather than replacing them, reinflate tracestate in envelope header
1 parent 7688714 commit 77fbd7b

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

packages/core/src/request.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@ export function eventToSentryRequest(event: Event, api: API): SentryRequest {
3333
* @returns SentryRequest in envelope form
3434
*/
3535
export function transactionToSentryRequest(event: Event, api: API): SentryRequest {
36-
// because we send event.tracestate as a header whose values have meaningful equals signs, when we create it we have
37-
// to replace the padding character with something else; the `.replace` call here reverses that to make it valid
38-
// base64 before converting to unicode for parsing
39-
let tracestateJSON;
40-
try {
41-
tracestateJSON = event.tracestate ? base64ToUnicode(event.tracestate.replace('.', '=')) : undefined;
42-
} catch (err) {
43-
logger.warn(err);
44-
tracestateJSON = '';
36+
let tracestate = {};
37+
if (event.tracestate) {
38+
try {
39+
// the tracestate is stored in bas64-encoded JSON, but envelope header values are expected to be full JS values,
40+
// so we have to decode and reinflate it
41+
tracestate = JSON.parse(base64ToUnicode(event.tracestate));
42+
} catch (err) {
43+
logger.warn(err);
44+
}
4545
}
4646
delete event.tracestate;
4747

4848
const envelopeHeaders = JSON.stringify({
4949
event_id: event.event_id,
5050
sent_at: new Date().toISOString(),
5151
trace_id: event.contexts?.trace?.trace_id,
52-
trace: tracestateJSON, // trace context for dynamic sampling on relay
52+
trace: tracestate, // trace context for dynamic sampling on relay
5353
});
5454

5555
const itemHeaders = JSON.stringify({

packages/tracing/src/transaction.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ export class Transaction extends SpanClass implements TransactionInterface {
155155
// identifier1=value1,identifier2=value2,...
156156
//
157157
// which means the value can't include any equals signs, since they already have meaning. Equals signs are commonly
158-
// used to pad the end of base64 values though, so we have to make a substitution (periods are legal in the header
159-
// but not used in base64).
158+
// used to pad the end of base64 values though, so to avoid confusion, we strip them off. (Most languages' base64
159+
// decoding functions (including those in JS) are able to function without the padding.)
160160
try {
161-
return unicodeToBase64(dataStr).replace(/={1,2}$/, '.');
161+
return unicodeToBase64(dataStr).replace(/={1,2}$/, '');
162162
} catch (err) {
163163
logger.warn(err);
164164
return '';

0 commit comments

Comments
 (0)