Skip to content

Commit 5d4ab1a

Browse files
committed
move tracestate value creation to its own util function
1 parent aeb65ca commit 5d4ab1a

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

packages/tracing/src/transaction.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getCurrentHub, Hub } from '@sentry/hub';
22
import { Event, Measurements, Transaction as TransactionInterface, TransactionContext } from '@sentry/types';
3-
import { isInstanceOf, logger, unicodeToBase64 } from '@sentry/utils';
3+
import { computeTracestate, isInstanceOf, logger } from '@sentry/utils';
44

55
import { Span as SpanClass, SpanRecorder } from './span';
66

@@ -143,26 +143,6 @@ export class Transaction extends SpanClass implements TransactionInterface {
143143

144144
const { environment, release } = client.getOptions() || {};
145145

146-
const dataStr = JSON.stringify({
147-
trace_id: this.traceId,
148-
public_key: dsn.user,
149-
environment: environment || 'no environment specified',
150-
release: release || 'no release specified',
151-
});
152-
153-
// See https://www.w3.org/TR/trace-context/#tracestate-header-field-values
154-
// The spec for tracestate header values calls for a string of the form
155-
//
156-
// identifier1=value1,identifier2=value2,...
157-
//
158-
// which means the value can't include any equals signs, since they already have meaning. Equals signs are commonly
159-
// used to pad the end of base64 values though, so to avoid confusion, we strip them off. (Most languages' base64
160-
// decoding functions (including those in JS) are able to function without the padding.)
161-
try {
162-
return unicodeToBase64(dataStr).replace(/={1,2}$/, '');
163-
} catch (err) {
164-
logger.warn(err);
165-
return '';
166-
}
146+
return computeTracestate({ trace_id: this.traceId, environment, release, public_key: dsn.user });
167147
}
168148
}

packages/utils/src/misc.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import { Event, StackFrame, WrappedFunction } from '@sentry/types';
33

44
import { getGlobalObject } from './compat';
5-
import { snipLine } from './string';
5+
import { SentryError } from './error';
6+
import { snipLine, unicodeToBase64 } from './string';
67

78
/**
89
* Extended Window interface that allows for Crypto API usage in IE browsers
@@ -290,3 +291,33 @@ export function stripUrlQueryAndFragment(urlPath: string): string {
290291
// eslint-disable-next-line no-useless-escape
291292
return urlPath.split(/[\?#]/, 1)[0];
292293
}
294+
295+
/**
296+
* Compute the value of a tracestate header
297+
*
298+
* @throws SentryError (because using the logger creates a circular dependency)
299+
* @returns the base64-encoded header value
300+
*/
301+
export function computeTracestate(tracestateData: {
302+
trace_id: string;
303+
environment: string | undefined;
304+
release: string | undefined;
305+
public_key: string;
306+
}): string {
307+
tracestateData.environment = tracestateData.environment || '';
308+
tracestateData.release = tracestateData.release || '';
309+
310+
// See https://www.w3.org/TR/trace-context/#tracestate-header-field-values
311+
// The spec for tracestate header values calls for a string of the form
312+
//
313+
// identifier1=value1,identifier2=value2,...
314+
//
315+
// which means the value can't include any equals signs, since they already have meaning. Equals signs are commonly
316+
// used to pad the end of base64 values though, so to avoid confusion, we strip them off. (Most languages' base64
317+
// decoding functions (including those in JS) are able to function without the padding.)
318+
try {
319+
return unicodeToBase64(JSON.stringify(tracestateData)).replace(/={1,2}$/, '');
320+
} catch (err) {
321+
throw new SentryError(`[Tracing] Error creating tracestate header: ${err}`);
322+
}
323+
}

0 commit comments

Comments
 (0)