Skip to content

Commit 8e8de11

Browse files
HazATkamilogorek
authored andcommitted
fix: Sampled, parent span id and tests
1 parent fe382e7 commit 8e8de11

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

packages/hub/src/span.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class Span implements SpanInterface, SpanProps {
2525
/**
2626
* Has the sampling decision been made?
2727
*/
28-
public readonly sampled?: string;
28+
public readonly sampled?: boolean;
2929

3030
/**
3131
* Timestamp when the span was created.
@@ -125,12 +125,18 @@ export class Span implements SpanInterface, SpanProps {
125125
): Span | undefined {
126126
const matches = traceparent.match(TRACEPARENT_REGEXP);
127127
if (matches) {
128-
const [traceId, spanId, sampled] = matches;
128+
let sampled: boolean | undefined;
129+
if (matches[3] === '1') {
130+
sampled = true;
131+
} else if (matches[3] === '0') {
132+
sampled = false;
133+
}
134+
129135
return new Span({
130136
...spanProps,
137+
parentSpanId: matches[2],
131138
sampled,
132-
spanId,
133-
traceId,
139+
traceId: matches[1],
134140
});
135141
}
136142
return undefined;
@@ -148,7 +154,11 @@ export class Span implements SpanInterface, SpanProps {
148154
* @inheritDoc
149155
*/
150156
public toTraceparent(): string {
151-
return `${this._traceId}-${this._spanId}${this.sampled ? '-1' : '0'}`;
157+
let sampledString = '';
158+
if (this.sampled !== undefined) {
159+
sampledString = this.sampled ? '-1' : '-0';
160+
}
161+
return `${this._traceId}-${this._spanId}${sampledString}`;
152162
}
153163

154164
/**

packages/hub/test/span.test.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ describe('Span', () => {
88
describe('fromTraceparent', () => {
99
test('no sample', () => {
1010
const from = Span.fromTraceparent('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bbbbbbbbbbbbbbbb') as any;
11-
expect(from._parent._traceId).toEqual('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
12-
expect(from._parent._spanId).toEqual('bbbbbbbbbbbbbbbb');
11+
12+
expect(from._parentSpanId).toEqual('bbbbbbbbbbbbbbbb');
1313
expect(from._traceId).toEqual('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
1414
expect(from._spanId).not.toEqual('bbbbbbbbbbbbbbbb');
1515
expect(from.sampled).toBeUndefined();
@@ -30,16 +30,19 @@ describe('Span', () => {
3030
});
3131

3232
test('toJSON', () => {
33-
expect(JSON.stringify(new Span('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbb'))).toEqual(
34-
`{"span_id":"bbbbbbbbbbbbbbbb","trace_id":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}`,
33+
const span = JSON.parse(
34+
JSON.stringify(new Span({ traceId: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', spanId: 'bbbbbbbbbbbbbbbb' })),
3535
);
36+
expect(span).toHaveProperty('span_id', 'bbbbbbbbbbbbbbbb');
37+
expect(span).toHaveProperty('trace_id', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
3638
});
3739

3840
test('toJSON with parent', () => {
39-
const spanA = new Span('a', 'b');
40-
const spanB = new Span('c', 'd', false, spanA);
41-
expect(JSON.stringify(spanB)).toEqual(
42-
`{"parent":{"span_id":"b","trace_id":"a"},"sampled":false,"span_id":"d","trace_id":"c"}`,
43-
);
41+
const spanA = new Span({ traceId: 'a', spanId: 'b' }) as any;
42+
const spanB = new Span({ traceId: 'c', spanId: 'd', sampled: false, parentSpanId: spanA._spanId });
43+
const serialized = JSON.parse(JSON.stringify(spanB));
44+
expect(serialized).toHaveProperty('parent_span_id', 'b');
45+
expect(serialized).toHaveProperty('span_id', 'd');
46+
expect(serialized).toHaveProperty('trace_id', 'c');
4447
});
4548
});

packages/types/src/span.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface SpanProps {
1313
description?: string;
1414
op?: string;
1515
parentSpanId?: string;
16-
sampled?: string;
16+
sampled?: boolean;
1717
spanId?: string;
1818
traceId?: string;
1919
transaction?: string;

0 commit comments

Comments
 (0)