Skip to content

Commit 6b000cc

Browse files
Ashish AggarwalAshish Aggarwal
authored andcommitted
minor fixes + improving tests
1 parent 5809d5a commit 6b000cc

4 files changed

Lines changed: 22 additions & 15 deletions

File tree

src/dispatchers/remote.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,12 @@ export default class RemoteDispatcher implements Dispatcher {
108108
} else if (typeof tagValue === 'boolean') {
109109
protoTag.setVbool(tagValue);
110110
protoTag.setType(messages.Tag.TagType.BOOL);
111-
} else {
111+
} else if (typeof tagValue === 'string') {
112112
protoTag.setVstr(tagValue);
113113
protoTag.setType(messages.Tag.TagType.STRING);
114+
} else {
115+
protoTag.setVbytes(tagValue);
116+
protoTag.setType(messages.Tag.TagType.BINARY);
114117
}
115118

116119
return protoTag;

src/propagators/textmap_propagator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default class TextMapPropagator implements Propagator {
3535
const baggage = spanContext.baggage();
3636
for (const key in baggage) {
3737
if (baggage.hasOwnProperty(key)) {
38-
carrier[`${this._opts.baggageKeyPrefix()}${key}`] = this._codex.encode(spanContext.baggage[key]);
38+
carrier[`${this._opts.baggageKeyPrefix()}${key}`] = this._codex.encode(spanContext.baggage()[key]);
3939
}
4040
}
4141
}

src/tracer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export default class Tracer {
7575
}
7676
}
7777

78-
const ctx = this._createSpanContext(parent, fields.callerSpanContext);
78+
const ctx = Tracer._createSpanContext(parent, fields.callerSpanContext);
7979
return this._startSpan(operationName, ctx, startTime, references, spanTags);
8080
}
8181

@@ -90,7 +90,7 @@ export default class Tracer {
9090
return span;
9191
}
9292

93-
private _createSpanContext(parent: SpanContext, callerContext: SpanContext): SpanContext {
93+
static _createSpanContext(parent: SpanContext, callerContext: SpanContext): SpanContext {
9494
if (!parent || !parent.isValid) {
9595
if (callerContext) {
9696
return new SpanContext(callerContext.traceId(), callerContext.spanId(), callerContext.parentSpanId(), callerContext.baggage());

tests/tracer.spec.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import InMemoryDispatcher from '../src/dispatchers/in_memory';
2121
import StartSpanFields from '../src/start_span_fields';
2222
import {isUndefined} from 'util';
2323
import SpanContext from '../src/span_context';
24+
import Span from '../src/span';
2425

2526
const dummyServiceName = 'my-service';
2627
const dummyOperation = 'my-service-operation';
@@ -42,6 +43,12 @@ const expectSpansInStore = (inMemSpanStore: InMemoryDispatcher, expectedCount: n
4243
});
4344
};
4445

46+
const findSpan = (inMemSpanStore: InMemoryDispatcher, spanKind: string): Span => {
47+
return inMemSpanStore
48+
.spans()
49+
.filter(span => span.tags().filter(tag => tag.key === 'span.kind' && tag.value === spanKind).length > 0)[0];
50+
};
51+
4552
describe('Tracer tests', () => {
4653

4754
describe('Tracer#startSpan', () => {
@@ -74,21 +81,18 @@ describe('Tracer tests', () => {
7481

7582
expect(serverSpan.isFinished()).eq(false);
7683
expect(inMemSpanStore.spans().length).equal(0);
77-
serverSpan.finish();
7884
clientSpan.finish();
85+
serverSpan.finish();
7986

8087
expectSpansInStore(inMemSpanStore, 2);
8188

8289
expect(inMemSpanStore.spans().map(span => span.operationName())).includes(downstreamOperation);
8390
expect(inMemSpanStore.spans().map(span => span.operationName())).includes(dummyOperation);
8491

85-
const receivedClientSpan = inMemSpanStore.spans().filter(span => {
86-
return span.tags().filter(tag => tag.key === 'span.kind' && tag.value === 'client').length > 0
87-
})[0];
88-
const receivedServerSpan = inMemSpanStore.spans().filter(span => {
89-
return span.tags().filter(tag => tag.key === 'span.kind' && tag.value === 'server').length > 0
90-
})[0];
92+
const receivedClientSpan = findSpan(inMemSpanStore, 'client');
93+
const receivedServerSpan = findSpan(inMemSpanStore, 'server');
9194

95+
expect(receivedClientSpan.duration() <= receivedServerSpan.duration()).eq(true);
9296
expect(receivedClientSpan.context().parentSpanId()).eq(receivedServerSpan.context().spanId());
9397
expect(isUndefined(receivedServerSpan.context().parentSpanId())).eq(true);
9498
expect(receivedServerSpan.context().traceId()).eq(receivedClientSpan.context().traceId());
@@ -97,18 +101,18 @@ describe('Tracer tests', () => {
97101
it('should inject the span in the carrier', () => {
98102
const inMemSpanStore = new InMemoryDispatcher();
99103
const tracer = new Tracer(dummyServiceName, inMemSpanStore, commonTags);
100-
const spanContext = new SpanContext('a', 'b', 'c');
104+
const spanContext = new SpanContext('a', 'b', 'c', { myKey: 'myVal'});
101105
const carrier = {};
102106
tracer.inject(spanContext, opentracing.FORMAT_TEXT_MAP, carrier);
103-
expect(JSON.stringify(carrier)).eq('{"Trace-ID":"a","Span-ID":"b","Parent-ID":"c"}');
107+
expect(JSON.stringify(carrier)).eq('{"Trace-ID":"a","Span-ID":"b","Parent-ID":"c","Baggage-myKey":"myVal"}');
104108
});
105109

106110
it('should extract the span from the carrier', () => {
107111
const inMemSpanStore = new InMemoryDispatcher();
108112
const tracer = new Tracer(dummyServiceName, inMemSpanStore, commonTags);
109-
const carrier = {'Trace-ID': 'a', 'Span-ID': 'b', 'Parent-ID': 'c'};
113+
const carrier = {'Trace-ID': 'a' , 'Span-ID': 'b', 'Parent-ID': 'c', 'Baggage-myKey': 'myVal'};
110114
const spanContext = tracer.extract(opentracing.FORMAT_TEXT_MAP, carrier);
111-
expect(JSON.stringify(spanContext)).eq('{"_traceId":"a","_spanId":"b","_parentSpanId":"c","_baggage":{}}');
115+
expect(JSON.stringify(spanContext)).eq('{"_traceId":"a","_spanId":"b","_parentSpanId":"c","_baggage":{"myKey":"myVal"}}');
112116
});
113117
});
114118
});

0 commit comments

Comments
 (0)