Skip to content

Commit b138430

Browse files
committed
add getTraceHeaders method to span
1 parent 3b881d9 commit b138430

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

packages/tracing/src/span.ts

Lines changed: 14 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, TraceHeaders, Transaction } from '@sentry/types';
33
import { dropUndefinedKeys, timestampWithMs, uuid4 } from '@sentry/utils';
44

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

249+
/**
250+
* @inheritDoc
251+
*/
252+
public getTraceHeaders(): TraceHeaders {
253+
const traceparent = this.toTraceparent();
254+
const tracestate = this.transaction?.tracestate;
255+
256+
return {
257+
'sentry-trace': traceparent,
258+
...(tracestate && { tracestate }),
259+
};
260+
}
261+
249262
/**
250263
* @inheritDoc
251264
*/

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, TraceHeaders } from './span';
2525
export { StackFrame } from './stackframe';
2626
export { Stacktrace } from './stacktrace';
2727
export { Status } from './status';

packages/types/src/span.ts

Lines changed: 13 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(): TraceHeaders;
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,8 @@ export interface Span extends SpanContext {
173181
trace_id: string;
174182
};
175183
}
184+
185+
export interface TraceHeaders {
186+
'sentry-trace': string;
187+
tracestate?: string;
188+
}

0 commit comments

Comments
 (0)