Skip to content

Commit e16cd35

Browse files
committed
add method to generate outgoing trace headers
1 parent 18c2393 commit e16cd35

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-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 { Primitive, Span as SpanInterface, SpanContext, Transaction } from '@sentry/types';
2+
import { Primitive, Span as SpanInterface, SpanContext, TraceHeaders, Transaction } from '@sentry/types';
33
import { dropUndefinedKeys, timestampWithMs, uuid4 } from '@sentry/utils';
44

55
import { SpanStatus } from './spanstatus';
@@ -284,6 +284,19 @@ export class Span implements SpanInterface {
284284
return this;
285285
}
286286

287+
/**
288+
* @inheritDoc
289+
*/
290+
public getTraceHeaders(): TraceHeaders {
291+
// tracestates live on the transaction, so if this is a free-floating span, there won't be one
292+
const tracestate = this.transaction && `sentry=${this.transaction.tracestate}`; // TODO kmclb
293+
294+
return {
295+
'sentry-trace': this.toTraceparent(),
296+
...(tracestate && { tracestate }),
297+
};
298+
}
299+
287300
/**
288301
* @inheritDoc
289302
*/

packages/tracing/test/span.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,26 @@ describe('Span', () => {
101101
});
102102
});
103103

104+
describe('getTraceHeaders', () => {
105+
it('returns correct headers', () => {
106+
const hub = new Hub(
107+
new BrowserClient({
108+
dsn: 'https://[email protected]/12312012',
109+
tracesSampleRate: 1,
110+
release: 'off.leash.park',
111+
environment: 'dogpark',
112+
}),
113+
);
114+
const transaction = hub.startTransaction({ name: 'FETCH /ball' });
115+
const span = transaction.startChild({ op: 'dig.hole' });
116+
117+
const headers = span.getTraceHeaders();
118+
119+
expect(headers['sentry-trace']).toEqual(span.toTraceparent());
120+
expect(headers.tracestate).toEqual(`sentry=${transaction.tracestate}`);
121+
});
122+
});
123+
104124
describe('toJSON', () => {
105125
test('simple', () => {
106126
const span = JSON.parse(

packages/types/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export { SdkInfo } from './sdkinfo';
2323
export { SdkMetadata } from './sdkmetadata';
2424
export { Session, SessionContext, SessionStatus } from './session';
2525
export { Severity } from './severity';
26-
export { Span, SpanContext } from './span';
26+
export { Span, SpanContext, TraceHeaders } from './span';
2727
export { StackFrame } from './stackframe';
2828
export { Stacktrace } from './stacktrace';
2929
export { Status } from './status';

packages/types/src/span.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ export interface Span extends SpanContext {
158158
/** Updates the current span with a new `SpanContext` */
159159
updateWithContext(spanContext: SpanContext): this;
160160

161+
/**
162+
* Get headers to attach to any outgoing requests made by the operation corresponding to the current span
163+
*
164+
* @returns An object containing the headers
165+
*/
166+
getTraceHeaders(): TraceHeaders;
167+
161168
/** Convert the object to JSON for w. spans array info only */
162169
getTraceContext(): {
163170
data?: { [key: string]: any };
@@ -169,6 +176,7 @@ export interface Span extends SpanContext {
169176
tags?: { [key: string]: Primitive };
170177
trace_id: string;
171178
};
179+
172180
/** Convert the object to JSON */
173181
toJSON(): {
174182
data?: { [key: string]: any };
@@ -183,3 +191,8 @@ export interface Span extends SpanContext {
183191
trace_id: string;
184192
};
185193
}
194+
195+
export type TraceHeaders = {
196+
'sentry-trace': string;
197+
tracestate?: string;
198+
};

0 commit comments

Comments
 (0)