-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracer_test.js
145 lines (118 loc) · 3.53 KB
/
tracer_test.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
import { suite, test, before, after, beforeEach, afterEach } from "node:test";
import assert from "node:assert";
import { Tracing, Span } from "./tracer.js";
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
suite("Span", () => {
test("new span", () => {
let span = new Span("test");
span.setAttributes({
foo: "bar",
baz: "potato",
});
assert.ok(span.startTime);
span.end();
assert(span.name == "test");
assert(span.attributes.get("foo") == "bar");
assert(span.attributes.get("baz") == "potato");
assert.ok(span.stopTime);
});
test("inherit span context", () => {
let ctx = {
traceID: "12345",
spanID: "45678",
};
let span = new Span("test", ctx);
assert.equal(span.traceID, "12345");
assert.equal(span.parentSpanID, "45678");
});
});
suite("Tracing", async () => {
let prevExporter;
let spans = [];
before(() => {
prevExporter = Tracing.exporter;
});
after(() => {
Tracing.exporter = prevExporter;
});
beforeEach(() => {
Tracing.exporter = (span) => {
spans.push(span);
};
});
afterEach(() => {
// clear the array, but keep the reference
spans.splice(0, spans.length);
});
test("start span", () => {
Tracing.startSpan("parent", (span) => {
assert.equal(span, Tracing.getCurrentSpan());
assert.equal("parent", Tracing.getCurrentSpan().name);
});
assert.equal(undefined, Tracing.getCurrentSpan());
});
test("start span nested", () => {
Tracing.startSpan("parent", (span) => {
assert.equal(span, Tracing.getCurrentSpan());
Tracing.startSpan("child", (span2) => {
assert.equal(span2, Tracing.getCurrentSpan());
assert.equal(span2.parentSpanID, span.spanID);
});
assert.equal(span, Tracing.getCurrentSpan());
});
assert.equal(undefined, Tracing.getCurrentSpan());
});
test("spans captured", () => {
// Because we must also support async use cases, this triggers an
// await internal to startSpan, which yields to the event loop.
// Exporting spans then becomes async even in the synchronous use-case,
// so we must also yield to the event loop before we can make this
// assertion.
//
// In practice, this difference can be ignored since we only care
// that spans are emitted soon after execution
let promise = Tracing.startSpan("parent", (span) => {
Tracing.startSpan("child", (span2) => {});
});
Promise.resolve(promise).then(() => {
assert.equal(2, spans.length);
// the child is the first span to close, so it should be first
assert.equal("child", spans[0].name);
assert.equal("parent", spans[1].name);
});
});
});
suite("async tracing", () => {
let prevExporter;
let spans = [];
before(() => {
prevExporter = Tracing.exporter;
});
after(() => {
Tracing.exporter = prevExporter;
});
beforeEach(() => {
Tracing.exporter = (span) => {
spans.push(span);
};
});
afterEach(() => {
// clear the array, but keep the reference
spans.splice(0, spans.length);
});
test("async spans captured", async () => {
await Tracing.startSpan("parent", async (span) => {
await sleep(10);
await Tracing.startSpan("child", async (span2) => {
await sleep(10);
});
await sleep(10);
});
assert.equal(2, spans.length);
// the child is the first span to close, so it should be first
assert.equal("child", spans[0].name);
assert.equal("parent", spans[1].name);
});
});