Skip to content

Commit 0744be4

Browse files
committed
prefix tracestate header with sentry=, create regex
1 parent 24d9aa7 commit 0744be4

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

packages/tracing/src/span.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ export class Span implements SpanInterface {
260260
let headers;
261261
const traceparent = this.toTraceparent();
262262
// tracestates live on the transaction, so if this is a free-floating span, there won't be one
263-
const tracestate = this.transaction?.tracestate;
263+
const tracestate = this.transaction && `sentry=${this.transaction.tracestate}`;
264264

265265
if (format === 'array') {
266266
// headers = [['sentry-trace', traceparent], ...(tracestate && [['tracestate', tracestate]])];

packages/tracing/test/browser/request.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ import {
1313
import { addExtensionMethods } from '../../src/hubextensions';
1414
import * as tracingUtils from '../../src/utils';
1515

16+
// This is a normal base64 regex, modified to reflect that fact that we strip the trailing = or == off
17+
const BASE64_MISSING_EQUALS = '([a-zA-Z0-9+/]{4})*(|[a-zA-Z0-9+/]{2,3})';
18+
19+
const TRACESTATE_HEADER_REGEX = new RegExp(
20+
`sentry=(${BASE64_MISSING_EQUALS})` + // our part of the header - should be the only part or at least the first part
21+
`(,\\w+=\\w+)*`, // any number of copies of a comma followed by `name=value`
22+
);
23+
1624
beforeAll(() => {
1725
addExtensionMethods();
1826
// @ts-ignore need to override global Request because it's not in the jest environment (even with an
@@ -235,7 +243,7 @@ describe('callbacks', () => {
235243
'sentry-trace',
236244
expect.stringMatching(tracingUtils.TRACEPARENT_REGEXP),
237245
);
238-
expect(setRequestHeader).toHaveBeenCalledWith('tracestate', expect.any(String));
246+
expect(setRequestHeader).toHaveBeenCalledWith('tracestate', expect.stringMatching(TRACESTATE_HEADER_REGEX));
239247
});
240248

241249
it('creates and finishes XHR span on active transaction', () => {

packages/tracing/test/span.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ describe('Span', () => {
118118
const headers = span.getTraceHeaders('object') as TraceHeadersObj;
119119

120120
expect(headers['sentry-trace']).toEqual(span.toTraceparent());
121-
expect(headers.tracestate).toEqual(transaction.tracestate);
121+
expect(headers.tracestate).toEqual(`sentry=${transaction.tracestate}`);
122122
});
123123

124124
it('returns the same data either as an object or an array', () => {

packages/utils/src/string.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import { getGlobalObject } from './compat';
22
import { SentryError } from './error';
33
import { isRegExp, isString } from './is';
44

5-
// credit to https://rgxdb.com/; see https://tools.ietf.org/html/rfc4648#section-4 for base64 spec
6-
// eslint-disable-next-line no-useless-escape
7-
export const BASE64_REGEX = /^(?:[a-zA-Z0-9+\/]{4})*(?:|(?:[a-zA-Z0-9+\/]{3}=)|(?:[a-zA-Z0-9+\/]{2}==)|(?:[a-zA-Z0-9+\/]{1}===))$/;
8-
95
/**
106
* Truncates given string to the maximum characters count
117
*

packages/utils/test/string.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { BASE64_REGEX, base64ToUnicode, isMatchingPattern, truncate, unicodeToBase64 } from '../src/string';
1+
import { base64ToUnicode, isMatchingPattern, truncate, unicodeToBase64 } from '../src/string';
2+
3+
// See https://tools.ietf.org/html/rfc4648#section-4 for base64 spec
4+
// eslint-disable-next-line no-useless-escape
5+
const BASE64_REGEX = /([a-zA-Z0-9+/]{4})*(|([a-zA-Z0-9+/]{3}=)|([a-zA-Z0-9+/]{2}==))/;
26

37
describe('truncate()', () => {
48
test('it works as expected', () => {

0 commit comments

Comments
 (0)