Skip to content

Commit aa11052

Browse files
committed
add getTraceHeaders method to span
1 parent 87acc50 commit aa11052

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

packages/tracing/src/span.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable max-lines */
2-
import { Span as SpanInterface, SpanContext, Transaction } from '@sentry/types';
2+
import { Span as SpanInterface, SpanContext, TraceHeadersArr, TraceHeadersObj, Transaction } from '@sentry/types';
33
import { dropUndefinedKeys, timestampWithMs, uuid4 } from '@sentry/utils';
44

55
import { SpanStatus } from './spanstatus';
@@ -246,6 +246,31 @@ export class Span implements SpanInterface {
246246
return `${this.traceId}-${this.spanId}${sampledString}`;
247247
}
248248

249+
/**
250+
* @inheritDoc
251+
*/
252+
public getTraceHeaders(format: 'array' | 'object'): TraceHeadersArr | TraceHeadersObj {
253+
let headers;
254+
const traceparent = this.toTraceparent();
255+
// tracestates live on the transaction, so if this is a free-floating span, there won't be one
256+
const tracestate = this.transaction?.tracestate;
257+
258+
if (format === 'array') {
259+
// headers = [['sentry-trace', traceparent], ...(tracestate && [['tracestate', tracestate]])];
260+
headers = [['sentry-trace', traceparent]];
261+
if (tracestate) {
262+
headers.push(['tracestate', tracestate]);
263+
}
264+
265+
return headers as TraceHeadersArr;
266+
}
267+
268+
return {
269+
'sentry-trace': traceparent,
270+
...(tracestate && { tracestate }),
271+
} as TraceHeadersObj;
272+
}
273+
249274
/**
250275
* @inheritDoc
251276
*/

packages/types/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export { CaptureContext, Scope, ScopeContext } from './scope';
2121
export { SdkInfo } from './sdkinfo';
2222
export { Session, SessionContext, SessionStatus } from './session';
2323
export { Severity } from './severity';
24-
export { Span, SpanContext } from './span';
24+
export { Span, SpanContext, TraceHeadersArr, TraceHeadersObj } from './span';
2525
export { StackFrame } from './stackframe';
2626
export { Stacktrace } from './stacktrace';
2727
export { Status } from './status';

packages/types/src/span.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ export interface Span extends SpanContext {
148148
/** Return a traceparent compatible header string */
149149
toTraceparent(): string;
150150

151+
/**
152+
* Get headers to attach to any outgoing requests made by the operation which created the span
153+
*
154+
* @returns An object containing the headers
155+
*/
156+
getTraceHeaders(format: 'array' | 'object'): TraceHeadersArr | TraceHeadersObj;
157+
151158
/** Convert the object to JSON for w. spans array info only */
152159
getTraceContext(): {
153160
data?: { [key: string]: any };
@@ -159,6 +166,7 @@ export interface Span extends SpanContext {
159166
tags?: { [key: string]: string };
160167
trace_id: string;
161168
};
169+
162170
/** Convert the object to JSON */
163171
toJSON(): {
164172
data?: { [key: string]: any };
@@ -173,3 +181,10 @@ export interface Span extends SpanContext {
173181
trace_id: string;
174182
};
175183
}
184+
185+
export type TraceHeadersArr = [['sentry-trace', string], ['tracestate', string]?];
186+
187+
export type TraceHeadersObj = {
188+
'sentry-trace': string;
189+
tracestate?: string;
190+
};

0 commit comments

Comments
 (0)