Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 1e144d4

Browse files
authored
Fix: handle infinite loop caused by get traceState() (#530)
1 parent afc3268 commit 1e144d4

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

packages/opencensus-core/src/trace/model/no-record/no-record-span.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class NoRecordSpan implements types.Span {
8787

8888
/** Gets the trace state */
8989
get traceState(): types.TraceState|undefined {
90-
return this.root.traceState;
90+
return undefined;
9191
}
9292

9393
/** Gets the ID of the parent span. */

packages/opencensus-core/test/test-no-record-root-span.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import * as assert from 'assert';
1718
import {CanonicalCode, CoreTracer, LinkType, MessageEventType, SpanKind} from '../src';
1819
import {NoRecordRootSpan} from '../src/trace/model/no-record/no-record-root-span';
1920

@@ -32,5 +33,6 @@ describe('NoRecordRootSpan()', () => {
3233
noRecordRootSpan.addAttribute('my_first_attribute', 'foo');
3334
noRecordRootSpan.setStatus(CanonicalCode.OK);
3435
noRecordRootSpan.startChildSpan();
36+
assert.equal(noRecordRootSpan.traceState, undefined);
3537
});
3638
});

packages/opencensus-core/test/test-no-record-span.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import * as assert from 'assert';
1718
import {CanonicalCode, LinkType, MessageEventType} from '../src';
1819
import {NoRecordSpan} from '../src/trace/model/no-record/no-record-span';
1920

@@ -28,5 +29,6 @@ describe('NoRecordSpan()', () => {
2829
noRecordSpan.addMessageEvent(MessageEventType.RECEIVED, 1, 123456789);
2930
noRecordSpan.addAttribute('my_first_attribute', 'foo');
3031
noRecordSpan.setStatus(CanonicalCode.OK);
32+
assert.equal(noRecordSpan.traceState, undefined);
3133
});
3234
});

packages/opencensus-core/test/test-root-span.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,19 @@ describe('RootSpan', () => {
324324
assert.ok(instanceOfLink(rootSpan.messageEvents[0]));
325325
});
326326
});
327+
328+
describe('get traceState()', () => {
329+
it('should handle optional / undefined traceState', () => {
330+
const root = new RootSpan(tracer, name, kind, traceId, parentSpanId);
331+
assert.ok(root instanceof RootSpan);
332+
assert.equal(root.traceState, undefined);
333+
});
334+
335+
it('should create a RootSpan instance with traceState', () => {
336+
const root =
337+
new RootSpan(tracer, name, kind, traceId, parentSpanId, 'traceState');
338+
assert.ok(root instanceof RootSpan);
339+
assert.equal(root.traceState, 'traceState');
340+
});
341+
});
327342
});

packages/opencensus-core/test/test-span.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,4 +370,27 @@ describe('Span', () => {
370370
assert.equal(span.status.message, 'This is an error');
371371
});
372372
});
373+
374+
describe('get traceState()', () => {
375+
it('should return the traceState', () => {
376+
const rootSpan =
377+
new RootSpan(tracer, name, kind, traceId, parentSpanId, 'traceState');
378+
rootSpan.start();
379+
assert.equal(rootSpan.traceState, 'traceState');
380+
381+
const span = new Span(tracer, rootSpan);
382+
assert.equal(span.traceId, rootSpan.traceId);
383+
assert.equal(span.traceState, 'traceState');
384+
});
385+
386+
it('should handle optional / undefined traceState', () => {
387+
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
388+
rootSpan.start();
389+
assert.equal(rootSpan.traceState, undefined);
390+
391+
const span = new Span(tracer, rootSpan);
392+
assert.equal(span.traceId, rootSpan.traceId);
393+
assert.equal(span.traceState, undefined);
394+
});
395+
});
373396
});

0 commit comments

Comments
 (0)