/
index.js
677 lines (562 loc) · 20.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
const opentracing = require("opentracing");
const Tracer = require("../index");
const zipkin = require("zipkin");
const {
sampler: { Sampler }
} = zipkin;
describe("Opentracing interface", () => {
it("should fail to init tracer without a service name", () => {
expect(() => {
new Tracer();
}).toThrowErrorMatchingSnapshot();
});
it("should fail to init tracer without a recorder", () => {
expect(() => {
new Tracer({
serviceName: "MyService"
});
}).toThrowErrorMatchingSnapshot();
});
it("should fail to init tracer without a kind", () => {
expect(() => {
new Tracer({
serviceName: "MyService",
recorder: {}
});
}).toThrowErrorMatchingSnapshot();
});
it("should fail to init tracer with a malformed kind", () => {
expect(() => {
new Tracer({
serviceName: "MyService",
recorder: {},
kind: "mobile"
});
}).toThrowErrorMatchingSnapshot();
});
it("should create a tracer", () => {
const tracer = new Tracer({
serviceName: "MyService",
recorder: {},
kind: "server"
});
expect(tracer).toBeInstanceOf(Tracer);
});
it("should initialize a zipkin tracer", () => {
const tracer = new Tracer({
serviceName: "MyService",
recorder: {},
kind: "server"
});
expect(zipkin.Tracer).toHaveBeenCalled();
});
it("should set a global tracer", () => {
opentracing.initGlobalTracer(
new Tracer({
serviceName: "MyService",
recorder: {},
kind: "server"
})
);
const tracer = opentracing.globalTracer();
expect(tracer.startSpan).toBeInstanceOf(Function);
});
it("should be have kind server, client and local ", () => {
expect(() => {
new Tracer({
serviceName: "MyService",
recorder: {},
kind: "client"
});
}).not.toThrowError();
expect(() => {
new Tracer({
serviceName: "MyService",
recorder: {},
kind: "server"
});
}).not.toThrowError();
expect(() => {
new Tracer({
serviceName: "MyService",
recorder: {},
kind: "local"
});
}).not.toThrowError();
expect(() => {
new Tracer({
serviceName: "MyService",
recorder: {},
kind: "peter"
});
}).toThrowErrorMatchingSnapshot();
});
it("should support 128bit trace Ids lengths");
describe("spans", () => {
let tracer;
let zipkinTracer;
beforeEach(() => {
tracer = new Tracer({
serviceName: "MyService",
recorder: {},
kind: "server"
});
zipkinTracer = tracer._zipkinTracer;
zipkinTracer.scoped.mockReset();
});
it("should not start a span without an operation name", () => {
expect(() => {
tracer.startSpan();
}).toThrowError();
});
// used for extracted spans
it("should start a span with no name, provided an empty string", () => {
tracer.startSpan("");
// should do it in a scope
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(zipkinTracer.recordBinary).not.toHaveBeenCalled();
});
it("startSpan should start a span", () => {
zipkinTracer.createRootId.mockImplementationOnce(() => 42);
tracer.startSpan("MyName");
// should do it in a scope
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(zipkinTracer.createRootId).toHaveBeenCalled();
expect(zipkinTracer.setId).toHaveBeenCalledWith(42);
expect(zipkin.Annotation.Rpc).toHaveBeenCalledWith("MyName");
expect(zipkinTracer.recordServiceName).toHaveBeenCalledWith("MyService");
});
it("should not fail with parent span set to null", () => {
const child = tracer.startSpan("ChildSpan", {
childOf: null,
kind: "server"
});
// Constructor of child span
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
zipkinTracer.scoped.mockReset();
expect(zipkinTracer.createRootId).toHaveBeenCalled();
});
it("should start a span with parent span", () => {
const parent = tracer.startSpan("ParentSpan");
// Constructor of parent span
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
zipkinTracer.scoped.mockReset();
// make sure we have the right ids set at the parent
expect(parent.id.traceId).toBeTruthy();
expect(parent.id.parentId).toBeTruthy();
const child = tracer.startSpan("ChildSpan", {
childOf: parent,
kind: "server"
});
// Constructor of child span
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
zipkinTracer.scoped.mockReset();
expect(zipkin.TraceId).toHaveBeenCalled();
// Uses TraceId to get a new TraceId
const call = zipkin.TraceId.mock.calls[0][0];
expect(call.parentId.present).toBeTruthy();
});
it("should send a server receive annotation if tracer is of kind server", () => {
tracer = new Tracer({
serviceName: "MyService",
recorder: {},
kind: "server"
});
zipkinTracer = tracer._zipkinTracer;
zipkinTracer.scoped.mockReset();
tracer.startSpan("MyName");
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(zipkin.Annotation.ServerRecv).toHaveBeenCalled();
expect(zipkinTracer.recordAnnotation).toHaveBeenCalledTimes(2);
});
it("should send a client send annotation if tracer is of kind client", () => {
tracer = new Tracer({
serviceName: "MyService",
recorder: {},
kind: "client"
});
zipkinTracer = tracer._zipkinTracer;
zipkinTracer.scoped.mockReset();
tracer.startSpan("MyName");
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(zipkin.Annotation.ClientSend).toHaveBeenCalled();
expect(zipkinTracer.recordAnnotation).toHaveBeenCalledTimes(2);
});
describe("span object", () => {
let span;
beforeEach(() => {
span = tracer.startSpan("Ponyfoo");
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
zipkinTracer.scoped.mockReset();
zipkinTracer.recordAnnotation.mockReset();
});
it("should expose spanId", () => {
expect(span.id).toBeDefined();
// Test if mock is sufficient
expect(span.id.traceId).toBeDefined();
expect(span.id.parentId).toBeDefined();
expect(span.id.spanId).toBeDefined();
expect(span.id.sampled).toBeDefined();
});
it("should log data", () => {
span.log({ event: "data_received", data: "42" });
// should do it in a scope
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(zipkinTracer.recordBinary).toHaveBeenCalledWith(
"event",
"data_received"
);
expect(zipkinTracer.recordBinary).toHaveBeenCalledWith("data", "42");
});
it("should use the right id in log", () => {
const otherSpan = tracer.startSpan("Other Span", {
kind: "client"
});
zipkinTracer.scoped.mockReset();
zipkinTracer.setId.mockReset();
span.log({ event: "other event" });
// should do it in a scope
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(zipkinTracer.setId).toHaveBeenLastCalledWith(span.id);
zipkinTracer.scoped.mockReset();
zipkinTracer.setId.mockReset();
otherSpan.log({ event: "yet another event" });
// should do it in a scope
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(zipkinTracer.setId).toHaveBeenLastCalledWith(otherSpan.id);
});
it("should not fail without an argument", () => {
expect(() => {
span.log();
// should do it in a scope
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
}).not.toThrowError();
});
it("should finish a span", () => {
span.finish();
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(zipkinTracer.setId).toHaveBeenCalled();
});
it("should send a server send annotation on finish if tracer is of kind server", () => {
// Start a tracer
tracer = new Tracer({
serviceName: "MyService",
recorder: {},
kind: "server"
});
zipkinTracer = tracer._zipkinTracer;
zipkinTracer.scoped.mockReset();
// Start a span
span = tracer.startSpan("Ponyfoo");
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
zipkinTracer.scoped.mockReset();
zipkinTracer.recordAnnotation.mockReset();
span.finish();
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(zipkin.Annotation.ServerSend).toHaveBeenCalled();
expect(zipkinTracer.recordAnnotation).toHaveBeenCalledTimes(1);
});
it("should send a client receive annotation on finish if tracer is of kind client", () => {
// Start a tracer
tracer = new Tracer({
serviceName: "MyService",
recorder: {},
kind: "client"
});
zipkinTracer = tracer._zipkinTracer;
zipkinTracer.scoped.mockReset();
// Start a span
span = tracer.startSpan("Ponyfoo");
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
zipkinTracer.scoped.mockReset();
zipkinTracer.recordAnnotation.mockReset();
span.finish();
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(zipkin.Annotation.ClientRecv).toHaveBeenCalled();
expect(zipkinTracer.recordAnnotation).toHaveBeenCalledTimes(1);
});
it("should use the right id in finish", () => {
const otherSpan = tracer.startSpan("Other Span", {
kind: "server"
});
zipkinTracer.scoped.mockReset();
zipkinTracer.setId.mockReset();
span.finish();
// should do it in a scope
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(zipkinTracer.setId).toHaveBeenLastCalledWith(span.id);
zipkinTracer.scoped.mockReset();
zipkinTracer.setId.mockReset();
otherSpan.finish();
// should do it in a scope
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(zipkinTracer.setId).toHaveBeenLastCalledWith(otherSpan.id);
});
// TODO: supported tags: https://github.com/opentracing/specification/blob/master/semantic_conventions.md#span-tags-table
// We might need to extend them in the future
it("should set tag with an ip address", () => {
span.setTag("peer.address", "128.167.178.4:5678");
// should do it in a scope
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(zipkin.Annotation.ServerAddr).toHaveBeenLastCalledWith({
serviceName: "MyService",
host: new zipkin.InetAddress("128.167.178.4"),
port: 5678
});
expect(zipkinTracer.recordAnnotation).toHaveBeenCalledTimes(1);
});
it("should set tag with an ip address with default port", () => {
span.setTag("peer.address", "128.167.178.4");
// should do it in a scope
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(zipkin.Annotation.ServerAddr).toHaveBeenLastCalledWith({
serviceName: "MyService",
host: new zipkin.InetAddress("128.167.178.4"),
port: 80
});
expect(zipkinTracer.recordAnnotation).toHaveBeenCalledTimes(1);
});
it("should set arbitrary key/value tags", () => {
span.setTag("ponies", 42);
// should do it in a scope
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(zipkin.Annotation.BinaryAnnotation).toHaveBeenLastCalledWith(
"ponies",
42
);
expect(zipkinTracer.recordAnnotation).toHaveBeenCalledTimes(1);
});
it("should fail set tag if key has wrong type of value", () => {
expect(() => {
span.setTag("peer.address", 42);
// should do it in a scope
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
}).toThrowErrorMatchingSnapshot();
});
it("should set tag for address on client", () => {
// Set tracer
tracer = new Tracer({
serviceName: "MyService",
recorder: {},
kind: "client"
});
zipkinTracer = tracer._zipkinTracer;
zipkinTracer.scoped.mockReset();
// Set span
span = tracer.startSpan("Ponyfoo");
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
zipkinTracer.scoped.mockReset();
zipkinTracer.recordAnnotation.mockReset();
// Actual test
span.setTag("peer.address", "128.167.178.4:5678");
// should do it in a scope
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(zipkin.Annotation.ClientAddr).toHaveBeenLastCalledWith({
serviceName: "MyService",
host: new zipkin.InetAddress("128.167.178.4"),
port: 5678
});
expect(zipkinTracer.recordAnnotation).toHaveBeenCalledTimes(1);
});
});
it("should expose inject/extract formats", () => {
expect(Tracer.FORMAT_TEXT_MAP).toBeDefined();
expect(Tracer.FORMAT_HTTP_HEADERS).toBeDefined();
expect(Tracer.FORMAT_BINARY).toBeDefined();
});
});
describe("usage of sampler", () => {
it("tracer should be initialize zipkin with a sampler");
it("span should be aware of sampler");
it("startSpan with parent should use parents sample value");
});
describe("usage of recorder", () => {
it("should initialize zipkin with the recorder provided", () => {
new Tracer({
serviceName: "MyService",
recorder: { id: 42 },
kind: "server"
});
expect(zipkin.Tracer).toHaveBeenCalledWith({
ctxImpl: {},
recorder: { id: 42 },
sampler: expect.any(Sampler)
});
});
});
describe("inject (serialize)", () => {
let tracer;
let zipkinTracer;
beforeEach(() => {
tracer = new Tracer({
serviceName: "MyService",
recorder: {},
kind: "server"
});
zipkinTracer = tracer._zipkinTracer;
});
// not relevant for us
describe("Text Map", () => {
it("should throw an error, because it is unsupported", () => {
const span = tracer.startSpan("Span");
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(() => {
const carrier = {};
tracer.inject(span, Tracer.FORMAT_TEXT_MAP, carrier);
}).toThrowErrorMatchingSnapshot();
});
});
describe("HTTP Headers", () => {
it("should throw without a span", () => {
expect(() => {
const carrier = {};
tracer.inject(undefined, Tracer.FORMAT_HTTP_HEADERS, carrier);
}).toThrowErrorMatchingSnapshot();
});
it("should throw without a carrier", () => {
const span = tracer.startSpan("Span");
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
expect(() => {
tracer.inject(span, Tracer.FORMAT_HTTP_HEADERS);
}).toThrowErrorMatchingSnapshot();
});
it("should set headers", () => {
const span = tracer.startSpan("Span");
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
const carrier = { couldBe: "anything request related" };
tracer.inject(span, Tracer.FORMAT_HTTP_HEADERS, carrier);
expect(carrier["x-b3-spanid"]).toBe(span.id.spanId);
});
});
// not relevant for us
describe("Binary", () => {
it("should throw an error, because it is unsupported", () => {
const span = tracer.startSpan("Span");
expect(() => {
const carrier = {};
tracer.inject(span, Tracer.FORMAT_BINARY, carrier);
}).toThrowErrorMatchingSnapshot();
});
});
});
describe("extract (deserialize)", () => {
let tracer;
let zipkinTracer;
beforeEach(() => {
tracer = new Tracer({
serviceName: "MyService",
recorder: {},
kind: "client"
});
zipkinTracer = tracer._zipkinTracer;
});
// not relevant for us
describe("Text Map", () => {
it("should throw an error, because it is unsupported", () => {
expect(() => {
const carrier = {};
tracer.extract(Tracer.FORMAT_TEXT_MAP, carrier);
}).toThrowErrorMatchingSnapshot();
});
});
describe("HTTP Headers", () => {
it("should fail with an invalid carrier", () => {
expect(() => {
tracer.extract(Tracer.FORMAT_HTTP_HEADERS, true);
}).toThrowErrorMatchingSnapshot();
});
it("should return null without http headers", () => {
expect(() => {
const span = tracer.extract(Tracer.FORMAT_HTTP_HEADERS, {});
expect(span).toBe(null);
}).not.toThrowError();
});
it("should handle child spans correctly", () => {
const httpHeaders = {
"x-b3-traceid": "myTraceId",
"x-b3-spanid": "mySpanId",
"x-b3-sampled": "1"
};
const span = tracer.extract(Tracer.FORMAT_HTTP_HEADERS, httpHeaders);
expect(zipkinTracer.scoped).not.toHaveBeenCalled();
expect(zipkinTracer.recordAnnotation).not.toHaveBeenCalled();
expect(zipkinTracer.recordServiceName).not.toHaveBeenCalled();
expect(span.id.traceId.value).toBe("myTraceId");
expect(span.id.spanId).toBe("mySpanId");
expect(span.id.sampled.value).toBe("1");
expect(span.log).toBeInstanceOf(Function);
expect(span.finish).toBeInstanceOf(Function);
});
it("should handle parent spans correctly", () => {
const httpHeaders = {
"x-b3-traceid": "myTraceId",
"x-b3-spanid": "mySpanId",
"x-b3-parentspanid": "myParentSpanId",
"x-b3-sampled": "1"
};
const span = tracer.extract(Tracer.FORMAT_HTTP_HEADERS, httpHeaders);
expect(zipkinTracer.scoped).not.toHaveBeenCalled();
expect(zipkinTracer.recordAnnotation).not.toHaveBeenCalled();
expect(zipkinTracer.recordServiceName).not.toHaveBeenCalled();
expect(span.id.traceId.value).toBe("myTraceId");
expect(span.id.spanId).toBe("mySpanId");
expect(span.id.parentId.value).toBe("myParentSpanId");
expect(span.id.sampled.value).toBe("1");
expect(span.log).toBeInstanceOf(Function);
expect(span.finish).toBeInstanceOf(Function);
});
});
// not relevant for us
describe("Binary", () => {
expect(() => {
const carrier = {};
tracer.extract(Tracer.FORMAT_BINARY, carrier);
}).toThrowErrorMatchingSnapshot();
});
});
describe("helpers", () => {
describe("makeOptional", () => {
let makeOptional;
beforeEach(() => {
makeOptional = Tracer.makeOptional;
});
it("should return none if null is given", () => {
expect(makeOptional(null).toString()).toBe("None");
});
it("should return some if something is given", () => {
expect(makeOptional(3).toString()).toBe("Some(3)");
});
it("should not fail on double call", () => {
expect(makeOptional(makeOptional(3)).toString()).toBe("Some(3)");
});
});
});
});