|
2 | 2 | import { Event, StackFrame, WrappedFunction } from '@sentry/types';
|
3 | 3 |
|
4 | 4 | import { getGlobalObject } from './compat';
|
5 |
| -import { snipLine } from './string'; |
| 5 | +import { SentryError } from './error'; |
| 6 | +import { snipLine, unicodeToBase64 } from './string'; |
6 | 7 |
|
7 | 8 | /**
|
8 | 9 | * Extended Window interface that allows for Crypto API usage in IE browsers
|
@@ -290,3 +291,33 @@ export function stripUrlQueryAndFragment(urlPath: string): string {
|
290 | 291 | // eslint-disable-next-line no-useless-escape
|
291 | 292 | return urlPath.split(/[\?#]/, 1)[0];
|
292 | 293 | }
|
| 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