This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathmock_tracer.ts
75 lines (60 loc) · 2.12 KB
/
mock_tracer.ts
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
// TODO: Move mock-tracer to its own NPM package once it is complete and tested.
import {SpanOptions, Tracer} from '../tracer';
import MockContext from './mock_context';
import MockReport from './mock_report';
import MockSpan from './mock_span';
/**
* OpenTracing Tracer implementation designed for use in unit tests.
*/
export class MockTracer extends Tracer {
private _spans: MockSpan[];
//------------------------------------------------------------------------//
// OpenTracing implementation
//------------------------------------------------------------------------//
protected _startSpan(name: string, fields: SpanOptions): MockSpan {
// _allocSpan is given it's own method so that derived classes can
// allocate any type of object they want, but not have to duplicate
// the other common logic in startSpan().
const span = this._allocSpan();
span.setOperationName(name);
this._spans.push(span);
if (fields.references) {
for (const ref of fields.references) {
span.addReference(ref);
}
}
// Capture the stack at the time the span started
span._startStack = new Error().stack;
return span;
}
protected _inject(span: MockContext, format: any, carrier: any): never {
throw new Error('NOT YET IMPLEMENTED');
}
protected _extract(format: any, carrier: any): never {
throw new Error('NOT YET IMPLEMENTED');
}
//------------------------------------------------------------------------//
// MockTracer-specific
//------------------------------------------------------------------------//
constructor() {
super();
this._spans = [];
}
private _allocSpan(): MockSpan {
return new MockSpan(this);
}
/**
* Discard any buffered data.
*/
clear(): void {
this._spans = [];
}
/**
* Return the buffered data in a format convenient for making unit test
* assertions.
*/
report(): MockReport {
return new MockReport(this._spans);
}
}
export default MockTracer;