From d07d8937574a8cc3e29ab50c141a4f38408ecd91 Mon Sep 17 00:00:00 2001 From: philicious Date: Wed, 27 May 2020 21:10:11 +0200 Subject: [PATCH] Fix CoreTracerBase not respecting propagated trace options (#811) --- .../opencensus-core/src/trace/model/tracer-base.ts | 12 ++++++++---- packages/opencensus-core/test/test-tracer-base.ts | 3 ++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/opencensus-core/src/trace/model/tracer-base.ts b/packages/opencensus-core/src/trace/model/tracer-base.ts index 826498587..7d9f63561 100644 --- a/packages/opencensus-core/src/trace/model/tracer-base.ts +++ b/packages/opencensus-core/src/trace/model/tracer-base.ts @@ -262,11 +262,15 @@ export class CoreTracerBase implements types.TracerBase { } let propagatedSample = null; // if there is a context propagation, keep the decision - if (options && options.spanContext && options.spanContext.options) { + if ( + options && + options.spanContext && + options.spanContext.options !== undefined + ) { propagatedSample = (options.spanContext.options & this.IS_SAMPLED) !== 0; + return !!propagatedSample; } - - // Propagated sample or use the default global sampler - return !!propagatedSample || this.sampler.shouldSample(traceId); + // default global sampler + return this.sampler.shouldSample(traceId); } } diff --git a/packages/opencensus-core/test/test-tracer-base.ts b/packages/opencensus-core/test/test-tracer-base.ts index 06c47a5be..c92eab55c 100644 --- a/packages/opencensus-core/test/test-tracer-base.ts +++ b/packages/opencensus-core/test/test-tracer-base.ts @@ -225,12 +225,13 @@ describe('Tracer Base', () => { }); }); - it('should create the new RootSpan with no propagation (options bit is not set)', () => { + it('should create the new NoRecordRootSpan with propagation options bit set to not-sample)', () => { const tracer = new CoreTracerBase(); tracer.start(defaultConfig); traceOptions.spanContext!.options = 0x0; tracer.startRootSpan(traceOptions, rootSpan => { assert.ok(rootSpan); + assert.ok(rootSpan instanceof NoRecordRootSpan); assert.strictEqual(rootSpan.name, traceOptions.name); assert.strictEqual(rootSpan.kind, traceOptions.kind); assert.strictEqual(rootSpan.traceId, spanContextPropagated.traceId);