diff --git a/packages/zipkin-instrumentation-grpc/README.md b/packages/zipkin-instrumentation-grpc/README.md index 104a74e6..2c865edc 100644 --- a/packages/zipkin-instrumentation-grpc/README.md +++ b/packages/zipkin-instrumentation-grpc/README.md @@ -1,5 +1,6 @@ -# zipkin-instrumentation-grpc +# zipkin-instrumentation-grpc-wrd +This lib contains just a bugfix for the sample flag on the library created by @lnshi's: zipkin-instrumentation-grpc. This lib is the interceptor for [Zipkin](https://github.com/openzipkin/zipkin) to intercept your [GRPC](https://github.com/grpc/grpc) calls(unary/stream). ## Project test status @@ -138,6 +139,3 @@ This lib is the interceptor for [Zipkin](https://github.com/openzipkin/zipkin) t } ``` -## Maintainer - -Feel free to `@lnshi` on Github for any issue of this lib 🙂 diff --git a/packages/zipkin-instrumentation-grpc/package.json b/packages/zipkin-instrumentation-grpc/package.json index f436db13..14a18d87 100644 --- a/packages/zipkin-instrumentation-grpc/package.json +++ b/packages/zipkin-instrumentation-grpc/package.json @@ -1,6 +1,6 @@ { - "name": "zipkin-instrumentation-grpc", - "version": "0.0.6", + "name": "zipkin-instrumentation-grpc-wrd", + "version": "0.0.1", "description": "Interceptor for instrumenting GRPC calls.", "main": "lib/index.js", "scripts": { diff --git a/packages/zipkin-instrumentation-grpc/src/ZipkinGrpcInterceptor.js b/packages/zipkin-instrumentation-grpc/src/ZipkinGrpcInterceptor.js index 3554ea4a..cb9563e1 100644 --- a/packages/zipkin-instrumentation-grpc/src/ZipkinGrpcInterceptor.js +++ b/packages/zipkin-instrumentation-grpc/src/ZipkinGrpcInterceptor.js @@ -3,7 +3,7 @@ const grpc = require('grpc'); const { TraceId, Annotation, - option: {Some} + option: {Some, None} } = require('zipkin'); class ZipkinGrpcInterceptor { @@ -61,16 +61,11 @@ class ZipkinGrpcInterceptor { return; } - let ctxSampled = grpcMetadataFromIncomingCtx.get('x-b3-sampled')[0]; - if (!(ctxSampled in ['0', '1'])) { - ctxSampled = '0'; - } - const ctxTraceInfo = new TraceId({ traceId: new Some(ctxTraceId), parentId: new Some(ctxParentId), spanId: ctxSpanId, - sampled: new Some(ctxSampled) + sampled: ZipkinGrpcInterceptor._determineSampledValue(grpcMetadataFromIncomingCtx.get('x-b3-sampled')[0]) }); this.tracer.scoped(() => { @@ -100,6 +95,12 @@ class ZipkinGrpcInterceptor { }); } + static _determineSampledValue(ctxSampledAsString) { + if (!ctxSampledAsString) { + return None; + } + return new Some(ctxSampledAsString === '1' || ctxSampledAsString === 'true'); + } } module.exports = ZipkinGrpcInterceptor; diff --git a/packages/zipkin-instrumentation-grpc/test/integrationTest.js b/packages/zipkin-instrumentation-grpc/test/integrationTest.js index 95a1ac94..f5b9721e 100644 --- a/packages/zipkin-instrumentation-grpc/test/integrationTest.js +++ b/packages/zipkin-instrumentation-grpc/test/integrationTest.js @@ -4,7 +4,7 @@ const zipkinBaseUrl = 'http://faked:64800'; const CLSContext = require('zipkin-context-cls'); -const {Tracer, BatchRecorder} = require('zipkin'); +const {Tracer, BatchRecorder, option: {Some, None}} = require('zipkin'); const {HttpLogger} = require('zipkin-transport-http'); const recorder = new BatchRecorder({ @@ -53,4 +53,77 @@ describe('Zipkin GRPC interceptor basic test', () => { done(); }); + + it("Pass in 'grpc.Metadata' with no sampled value, should set sampled to None", (done) => { + const metadata = new grpc.Metadata(); + metadata.add('x-b3-traceid', '1AAA'); + metadata.add('x-b3-parentspanid', '1BBB'); + metadata.add('x-b3-spanid', '1CCC'); + + ZIPKIN_GRPC_INTCP.uponServerRecvGrpcCall({ serviceName: 'test-service1', grpcMetadataFromIncomingCtx: metadata}); + expect(ZIPKIN_GRPC_INTCP.traceId.traceId).to.equal('1AAA'); + expect(ZIPKIN_GRPC_INTCP.traceId.sampled).to.equal(None); + + done(); + }); + + it("Pass in 'grpc.Metadata' with sampled '0', should set sampled to Some(false)", (done) => { + const metadata = new grpc.Metadata(); + metadata.add('x-b3-traceid', '2AAA'); + metadata.add('x-b3-parentspanid', '2BBB'); + metadata.add('x-b3-spanid', '2CCC'); + metadata.add('x-b3-sampled', '0'); + + ZIPKIN_GRPC_INTCP.uponServerRecvGrpcCall({ serviceName: 'test-service2', grpcMetadataFromIncomingCtx: metadata}); + expect(ZIPKIN_GRPC_INTCP.traceId.traceId).to.equal('2AAA'); + expect(ZIPKIN_GRPC_INTCP.traceId.sampled).to.be.instanceOf(Some); + expect(ZIPKIN_GRPC_INTCP.traceId.sampled.value).to.equal(false); + + done(); + }); + + it("Pass in 'grpc.Metadata' with sampled '1', should set sampled to Some(true)", (done) => { + const metadata = new grpc.Metadata(); + metadata.add('x-b3-traceid', '3AAA'); + metadata.add('x-b3-parentspanid', '3BBB'); + metadata.add('x-b3-spanid', '3CCC'); + metadata.add('x-b3-sampled', '1'); + + ZIPKIN_GRPC_INTCP.uponServerRecvGrpcCall({ serviceName: 'test-service3', grpcMetadataFromIncomingCtx: metadata}); + expect(ZIPKIN_GRPC_INTCP.traceId.traceId).to.equal('3AAA'); + expect(ZIPKIN_GRPC_INTCP.traceId.sampled).to.be.instanceOf(Some); + expect(ZIPKIN_GRPC_INTCP.traceId.sampled.value).to.equal(true); + + done(); + }); + + it("Pass in 'grpc.Metadata' with sampled 'false', should set sampled to Some(false)", (done) => { + const metadata = new grpc.Metadata(); + metadata.add('x-b3-traceid', '4AAA'); + metadata.add('x-b3-parentspanid', '4BBB'); + metadata.add('x-b3-spanid', '4CCC'); + metadata.add('x-b3-sampled', 'false'); + + ZIPKIN_GRPC_INTCP.uponServerRecvGrpcCall({ serviceName: 'test-service4', grpcMetadataFromIncomingCtx: metadata}); + expect(ZIPKIN_GRPC_INTCP.traceId.traceId).to.equal('4AAA'); + expect(ZIPKIN_GRPC_INTCP.traceId.sampled).to.be.instanceOf(Some); + expect(ZIPKIN_GRPC_INTCP.traceId.sampled.value).to.equal(false); + + done(); + }); + + it("Pass in 'grpc.Metadata' with sampled 'true', should set sampled to Some(true)", (done) => { + const metadata = new grpc.Metadata(); + metadata.add('x-b3-traceid', '5AAA'); + metadata.add('x-b3-parentspanid', '5BBB'); + metadata.add('x-b3-spanid', '5CCC'); + metadata.add('x-b3-sampled', 'true'); + + ZIPKIN_GRPC_INTCP.uponServerRecvGrpcCall({ serviceName: 'test-service5', grpcMetadataFromIncomingCtx: metadata}); + expect(ZIPKIN_GRPC_INTCP.traceId.traceId).to.equal('5AAA'); + expect(ZIPKIN_GRPC_INTCP.traceId.sampled).to.be.instanceOf(Some); + expect(ZIPKIN_GRPC_INTCP.traceId.sampled.value).to.equal(true); + + done(); + }); });