Skip to content

Commit d5c352c

Browse files
Ashish AggarwalAshish Aggarwal
authored andcommitted
adding opentracing api compatibility test spec
1 parent 78649af commit d5c352c

9 files changed

Lines changed: 109 additions & 57 deletions

File tree

examples/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ const clientChildSpan = tracer.startSpan('downstream-service-call', {
9292
clientChildSpan.setTag(opentracing.Tags.ERROR, true);
9393
clientChildSpan.setTag(opentracing.Tags.HTTP_STATUS_CODE, 503);
9494
clientChildSpan.addTags({'child-custom-tag-1': 1, 'child-custom-tag-2': 'someval'});
95+
clientChildSpan.log({
96+
eventCode: 1001
97+
});
9598

9699
serverSpan.setTag(opentracing.Tags.ERROR, true);
97100
serverSpan.setTag('my-custom-tag', 10.5);

src/dispatchers/remote.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ export default class RemoteDispatcher implements Dispatcher {
2727
_logger: any;
2828

2929
constructor(agentHost: string, agentPort: number, logger = new NullLogger()) {
30-
logger.info(`Initializing the remote dispatcher, connecting at ${agentHost}:${agentPort}`);
3130
agentHost = agentHost || 'haystack-agent';
3231
agentPort = agentPort || 35000;
32+
logger.info(`Initializing the remote dispatcher, connecting at ${agentHost}:${agentPort}`);
3333
this._client = new services.SpanAgentClient(`${agentHost}:${agentPort}`, grpc.credentials.createInsecure());
3434
this._logger = logger;
3535
}
@@ -63,9 +63,9 @@ export default class RemoteDispatcher implements Dispatcher {
6363
const protoSpan = new messages.Span();
6464
protoSpan.setServicename(span.serviceName());
6565
protoSpan.setOperationname(span.operationName());
66-
protoSpan.setTraceid(span.context().traceId());
67-
protoSpan.setSpanid(span.context().spanId());
68-
protoSpan.setParentspanid(span.context().parentSpanId());
66+
protoSpan.setTraceid(span.context().traceId);
67+
protoSpan.setSpanid(span.context().spanId);
68+
protoSpan.setParentspanid(span.context().parentSpanId);
6969
protoSpan.setStarttime(span.startTime());
7070
protoSpan.setDuration(span.duration());
7171

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2018 Expedia, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {Propagator} from './propagator';
18+
import SpanContext from '../span_context';
19+
20+
export default class BinaryPropagator implements Propagator {
21+
inject(spanContext: SpanContext, carrier: any): void {
22+
}
23+
24+
extract(carrier: any): SpanContext {
25+
return null;
26+
}
27+
}

src/propagators/textmap_propagator.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ export default class TextMapPropagator implements Propagator {
2828
}
2929

3030
inject(spanContext: SpanContext, carrier: any): void {
31-
carrier[this._opts.traceIdKey()] = spanContext.traceId();
32-
carrier[this._opts.spanIdKey()] = spanContext.spanId();
33-
carrier[this._opts.parentSpanIdKey()] = spanContext.parentSpanId();
31+
carrier[this._opts.traceIdKey()] = spanContext.traceId;
32+
carrier[this._opts.spanIdKey()] = spanContext.spanId;
33+
carrier[this._opts.parentSpanIdKey()] = spanContext.parentSpanId;
3434

35-
const baggage = spanContext.baggage();
35+
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/span.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export default class Span extends opentracing.Span {
169169
}
170170

171171
protected _getBaggageItem(key: string): string | any {
172-
return this._spanContext.baggage()[key];
172+
return this._spanContext.baggage[key];
173173
}
174174

175175
protected _context(): SpanContext {

src/span_context.ts

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,61 +18,45 @@ import Utils from './utils';
1818
import * as opentracing from 'opentracing';
1919

2020
export default class SpanContext extends opentracing.SpanContext {
21-
_traceId: string;
22-
_spanId: string;
23-
_parentSpanId: string;
24-
_baggage: any;
21+
traceId: string;
22+
spanId: string;
23+
parentSpanId: string;
24+
baggage: any;
2525

2626
constructor(
2727
traceId,
2828
spanId,
2929
parentSpanId,
3030
baggage = {}) {
3131
super();
32-
this._traceId = traceId;
33-
this._spanId = spanId;
34-
this._parentSpanId = parentSpanId;
35-
this._baggage = baggage;
36-
}
37-
38-
traceId(): string {
39-
return this._traceId;
40-
}
41-
42-
spanId(): string {
43-
return this._spanId;
44-
}
45-
46-
parentSpanId(): string {
47-
return this._parentSpanId;
48-
}
49-
50-
baggage(): any {
51-
return this._baggage;
32+
this.traceId = traceId;
33+
this.spanId = spanId;
34+
this.parentSpanId = parentSpanId;
35+
this.baggage = baggage;
5236
}
5337

5438
setTraceId(traceId: string): void {
55-
this._traceId = traceId;
39+
this.traceId = traceId;
5640
}
5741

5842
setSpanId(spanId: string): void {
59-
this._spanId = spanId;
43+
this.spanId = spanId;
6044
}
6145

6246
setParentSpanId(parentSpanId: string): void {
63-
this._parentSpanId = parentSpanId;
47+
this.parentSpanId = parentSpanId;
6448
}
6549

6650
addBaggageItem(key: string, value: any): SpanContext {
67-
const newBaggage = Utils.assign(this._baggage, key, value);
51+
const newBaggage = Utils.assign(this.baggage, key, value);
6852
return new SpanContext(
69-
this._traceId,
70-
this._spanId,
71-
this._parentSpanId,
53+
this.traceId,
54+
this.spanId,
55+
this.parentSpanId,
7256
newBaggage);
7357
}
7458

7559
isValid(): boolean {
76-
return !!(this._traceId && this._spanId);
60+
return !!(this.traceId && this.spanId);
7761
}
7862
}

src/tracer.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import PropagationRegistry from './propagators/propagation_registry';
2727
import TextMapPropagator from './propagators/textmap_propagator';
2828
import URLCodex from './propagators/url_codex';
2929
import StartSpanFields from './start_span_fields';
30+
import BinaryPropagator from './propagators/binary_propagator';
3031

3132
export default class Tracer extends opentracing.Tracer {
3233
_serviceName: string;
@@ -46,6 +47,7 @@ export default class Tracer extends opentracing.Tracer {
4647
this._logger = logger;
4748
this._registry = new PropagationRegistry();
4849
this._registry.register(opentracing.FORMAT_TEXT_MAP, new TextMapPropagator());
50+
this._registry.register(opentracing.FORMAT_BINARY, new BinaryPropagator());
4951
this._registry.register(opentracing.FORMAT_HTTP_HEADERS, new TextMapPropagator(new URLCodex()));
5052
}
5153

@@ -98,13 +100,13 @@ export default class Tracer extends opentracing.Tracer {
98100
static _createSpanContext(parent: SpanContext, callerContext: SpanContext): SpanContext {
99101
if (!parent || !parent.isValid) {
100102
if (callerContext) {
101-
return new SpanContext(callerContext.traceId(), callerContext.spanId(), callerContext.parentSpanId(), callerContext.baggage());
103+
return new SpanContext(callerContext.traceId, callerContext.spanId, callerContext.parentSpanId, callerContext.baggage);
102104
} else {
103-
const parentBaggage = parent && parent.baggage();
105+
const parentBaggage = parent && parent.baggage;
104106
return new SpanContext(Utils.randomUUID(), Utils.randomUUID(), parentBaggage);
105107
}
106108
} else {
107-
return new SpanContext(parent.traceId(), Utils.randomUUID(), parent.spanId(), parent.baggage());
109+
return new SpanContext(parent.traceId, Utils.randomUUID(), parent.spanId, parent.baggage);
108110
}
109111
}
110112

@@ -142,7 +144,7 @@ export default class Tracer extends opentracing.Tracer {
142144

143145
const propagator = this._registry.propagator(format);
144146
if (!propagator) {
145-
throw new Error('injector for the given format is not supported');
147+
throw new Error('injector is not supported for format=' + format);
146148
}
147149

148150
propagator.inject(spanContext, carrier);
@@ -155,7 +157,7 @@ export default class Tracer extends opentracing.Tracer {
155157

156158
const propagator = this._registry.propagator(format);
157159
if (!propagator) {
158-
throw new Error('extractor for the given format is not supported');
160+
throw new Error('extractor is not supported for format=' + format);
159161
}
160162

161163
return propagator.extract(carrier);

tests/api-compat.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2018 Expedia, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import Tracer from '../src/tracer';
18+
19+
describe('OpenTracing Api Compatibility Tests', () => {
20+
21+
describe('OpenTracing Api', () => {
22+
it("should be compatible", () => {
23+
const apiCompatibilityChecks = require('opentracing/lib/test/api_compatibility.js').default;
24+
apiCompatibilityChecks(() => new Tracer('my-service'));
25+
});
26+
});
27+
});
28+

tests/tracer.spec.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ const expectSpansInStore = (inMemSpanStore: InMemoryDispatcher, expectedCount: n
3737
const versionTagValue = receivedSpan.tags()['version'];
3838
expect(versionTagValue).eq('1.1');
3939
expect(receivedSpan.serviceName()).eq(dummyServiceName);
40-
expect(isUndefined(receivedSpan.context().traceId())).eq(false);
41-
expect(isUndefined(receivedSpan.context().spanId())).eq(false);
40+
expect(isUndefined(receivedSpan.context().traceId)).eq(false);
41+
expect(isUndefined(receivedSpan.context().spanId)).eq(false);
4242
});
4343
};
4444

@@ -62,7 +62,7 @@ describe('Tracer tests', () => {
6262
expectSpansInStore(inMemSpanStore, 1);
6363
const receivedSpan = inMemSpanStore.spans()[0];
6464
expect(receivedSpan.operationName()).eq(dummyOperation);
65-
expect(isUndefined(receivedSpan.context().parentSpanId())).eq(true);
65+
expect(isUndefined(receivedSpan.context().parentSpanId)).eq(true);
6666
});
6767

6868
it('should start and dispatch server and client spans', () => {
@@ -77,6 +77,9 @@ describe('Tracer tests', () => {
7777
startClientSpanFields.childOf = serverSpan.context();
7878
startClientSpanFields.tags = { 'span.kind': 'client' };
7979
const clientSpan = tracer.startSpan(downstreamOperation, startClientSpanFields);
80+
clientSpan.log({
81+
eventCode: 100
82+
});
8083

8184
expect(serverSpan.isFinished()).eq(false);
8285
expect(inMemSpanStore.spans().length).equal(0);
@@ -85,16 +88,21 @@ describe('Tracer tests', () => {
8588

8689
expectSpansInStore(inMemSpanStore, 2);
8790

88-
expect(inMemSpanStore.spans().map(span => span.operationName())).includes(downstreamOperation);
89-
expect(inMemSpanStore.spans().map(span => span.operationName())).includes(dummyOperation);
90-
9191
const receivedClientSpan = findSpan(inMemSpanStore, 'client');
9292
const receivedServerSpan = findSpan(inMemSpanStore, 'server');
9393

94+
expect(receivedClientSpan.operationName()).eq(downstreamOperation);
95+
expect(receivedServerSpan.operationName()).eq(dummyOperation);
9496
expect(receivedClientSpan.duration() <= receivedServerSpan.duration()).eq(true);
95-
expect(receivedClientSpan.context().parentSpanId()).eq(receivedServerSpan.context().spanId());
96-
expect(isUndefined(receivedServerSpan.context().parentSpanId())).eq(true);
97-
expect(receivedServerSpan.context().traceId()).eq(receivedClientSpan.context().traceId());
97+
expect(receivedClientSpan.context().parentSpanId).eq(receivedServerSpan.context().spanId);
98+
expect(isUndefined(receivedServerSpan.context().parentSpanId)).eq(true);
99+
expect(receivedServerSpan.context().traceId).eq(receivedClientSpan.context().traceId);
100+
101+
expect(receivedClientSpan.logs().length).eq(1);
102+
receivedClientSpan.logs().forEach(log => {
103+
expect(log.keyValuePairs['eventCode']).eq(100);
104+
expect(log.timestamp <= (Date.now() * 1000)).eq(true);
105+
})
98106
});
99107

100108
it('should inject the span in the carrier', () => {
@@ -111,7 +119,7 @@ describe('Tracer tests', () => {
111119
const tracer = new Tracer(dummyServiceName, inMemSpanStore, commonTags);
112120
const carrier = {'Trace-ID': 'a' , 'Span-ID': 'b', 'Parent-ID': 'c', 'Baggage-myKey': 'myVal'};
113121
const spanContext = tracer.extract(opentracing.FORMAT_TEXT_MAP, carrier);
114-
expect(JSON.stringify(spanContext)).eq('{"_traceId":"a","_spanId":"b","_parentSpanId":"c","_baggage":{"myKey":"myVal"}}');
122+
expect(JSON.stringify(spanContext)).eq('{"traceId":"a","spanId":"b","parentSpanId":"c","baggage":{"myKey":"myVal"}}');
115123
});
116124
});
117125
});

0 commit comments

Comments
 (0)