From 73212b8469f7e47d889cdb6ec1812de7523fa59f Mon Sep 17 00:00:00 2001 From: Mayur Kale Date: Thu, 23 Apr 2020 15:16:11 -0700 Subject: [PATCH] fix(B3Propagator): B3 sampled causing gRPC error (#977) * fix(B3Propagator): B3 sampled causing gRPC error * fix: review comment --- .../src/context/propagation/B3Propagator.ts | 8 +++++++- .../opentelemetry-core/test/context/B3Propagator.test.ts | 4 ++-- .../opentelemetry-core/test/context/composite.test.ts | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/opentelemetry-core/src/context/propagation/B3Propagator.ts b/packages/opentelemetry-core/src/context/propagation/B3Propagator.ts index ce68edc3e2..9a6f38f67d 100644 --- a/packages/opentelemetry-core/src/context/propagation/B3Propagator.ts +++ b/packages/opentelemetry-core/src/context/propagation/B3Propagator.ts @@ -57,7 +57,13 @@ export class B3Propagator implements HttpTextPropagator { // We set the header only if there is an existing sampling decision. // Otherwise we will omit it => Absent. if (spanContext.traceFlags !== undefined) { - setter(carrier, X_B3_SAMPLED, Number(spanContext.traceFlags)); + setter( + carrier, + X_B3_SAMPLED, + (TraceFlags.SAMPLED & spanContext.traceFlags) === TraceFlags.SAMPLED + ? '1' + : '0' + ); } } } diff --git a/packages/opentelemetry-core/test/context/B3Propagator.test.ts b/packages/opentelemetry-core/test/context/B3Propagator.test.ts index 1eb6759c16..f403bc72a0 100644 --- a/packages/opentelemetry-core/test/context/B3Propagator.test.ts +++ b/packages/opentelemetry-core/test/context/B3Propagator.test.ts @@ -60,7 +60,7 @@ describe('B3Propagator', () => { 'd4cda95b652f4a1592b449d5929fda1b' ); assert.deepStrictEqual(carrier[X_B3_SPAN_ID], '6e0c63257de34c92'); - assert.deepStrictEqual(carrier[X_B3_SAMPLED], TraceFlags.SAMPLED); + assert.deepStrictEqual(carrier[X_B3_SAMPLED], '1'); }); it('should set b3 traceId and spanId headers - ignore tracestate', () => { @@ -82,7 +82,7 @@ describe('B3Propagator', () => { 'd4cda95b652f4a1592b449d5929fda1b' ); assert.deepStrictEqual(carrier[X_B3_SPAN_ID], '6e0c63257de34c92'); - assert.deepStrictEqual(carrier[X_B3_SAMPLED], TraceFlags.NONE); + assert.deepStrictEqual(carrier[X_B3_SAMPLED], '0'); }); it('should not inject empty spancontext', () => { diff --git a/packages/opentelemetry-core/test/context/composite.test.ts b/packages/opentelemetry-core/test/context/composite.test.ts index 10a21af1f3..080b802154 100644 --- a/packages/opentelemetry-core/test/context/composite.test.ts +++ b/packages/opentelemetry-core/test/context/composite.test.ts @@ -80,7 +80,7 @@ describe('Composite Propagator', () => { assert.strictEqual(carrier[X_B3_TRACE_ID], traceId); assert.strictEqual(carrier[X_B3_SPAN_ID], spanId); - assert.strictEqual(carrier[X_B3_SAMPLED], 1); + assert.strictEqual(carrier[X_B3_SAMPLED], '1'); assert.strictEqual( carrier[TRACE_PARENT_HEADER], `00-${traceId}-${spanId}-01`