Skip to content

Commit b881254

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

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

packages/core/src/request.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,23 @@ export function transactionToSentryRequest(event: Event, api: API): SentryReques
3737
const { __sentry_samplingMethod: samplingMethod, __sentry_sampleRate: sampleRate, ...otherTags } = event.tags || {};
3838
event.tags = otherTags;
3939

40-
// because we send event.tracestate as a header whose values have meaningful equals signs, when we create it we have
41-
// to replace the padding character with something else; the `.replace` call here reverses that to make it valid
42-
// base64 before converting to unicode for parsing
43-
let tracestateJSON;
44-
try {
45-
tracestateJSON = event.tracestate ? base64ToUnicode(event.tracestate.replace('.', '=')) : undefined;
46-
} catch (err) {
47-
logger.warn(err);
48-
tracestateJSON = '';
40+
let tracestate = {};
41+
if (event.tracestate) {
42+
try {
43+
// the tracestate is stored in bas64-encoded JSON, but envelope header values are expected to be full JS values,
44+
// so we have to decode and reinflate it
45+
tracestate = JSON.parse(base64ToUnicode(event.tracestate));
46+
} catch (err) {
47+
logger.warn(err);
48+
}
4949
}
5050
delete event.tracestate;
5151

5252
const envelopeHeaders = JSON.stringify({
5353
event_id: event.event_id,
5454
sent_at: new Date().toISOString(),
55-
trace: tracestateJSON, // trace context for dynamic sampling on relay
55+
trace_id: event.contexts?.trace?.trace_id,
56+
trace: tracestate, // trace context for dynamic sampling on relay
5657
});
5758

5859
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)