Skip to content
This repository has been archived by the owner on Mar 19, 2022. It is now read-only.

The sampled value should be a boolean, not a string. Currently the result is that the trace is always sampled. #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/zipkin-instrumentation-grpc/README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 🙂
4 changes: 2 additions & 2 deletions packages/zipkin-instrumentation-grpc/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const grpc = require('grpc');
const {
TraceId,
Annotation,
option: {Some}
option: {Some, None}
} = require('zipkin');

class ZipkinGrpcInterceptor {
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -100,6 +95,12 @@ class ZipkinGrpcInterceptor {
});
}

static _determineSampledValue(ctxSampledAsString) {
if (!ctxSampledAsString) {
return None;
}
return new Some(ctxSampledAsString === '1' || ctxSampledAsString === 'true');
}
}

module.exports = ZipkinGrpcInterceptor;
75 changes: 74 additions & 1 deletion packages/zipkin-instrumentation-grpc/test/integrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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();
});
});