-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathtelemetry.test.ts
More file actions
235 lines (189 loc) · 6.67 KB
/
telemetry.test.ts
File metadata and controls
235 lines (189 loc) · 6.67 KB
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
import { InMemorySpanExporter, ReadableSpan, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { NodeTracerProvider } from './nodetraceprovider';
import { DBOS } from '../src';
import Koa from 'koa';
import Router from '@koa/router';
import { context, trace, SpanStatusCode } from '@opentelemetry/api';
import { isTraceContextWorking } from '../src/telemetry/traces';
import { AddressInfo } from 'net';
import { globalParams } from '../src/utils';
async function tracedStep() {
return Promise.resolve();
}
async function doSomethingTraced_internal() {
const span = trace.getSpan(context.active());
if (span) {
span.setAttribute('my-lib.didSomething', true);
}
if (globalParams.tracingEnabled) {
expect(DBOS.span).toBe(trace.getSpan(context.active()));
}
await DBOS.runStep(tracedStep, { name: 'tracedStep' });
return Promise.resolve('Done');
}
const doSomethingTraced = DBOS.registerWorkflow(doSomethingTraced_internal);
function createApp() {
const app = new Koa();
const router = new Router();
app.use(async (ctx, next) => {
const current = trace.getSpan(context.active());
if (current) {
return next() as Promise<unknown>;
}
const tracer = trace.getTracer('manual');
const span = tracer.startSpan(`manual-span-for-${ctx.method} ${ctx.path}`);
try {
await context.with(trace.setSpan(context.active(), span), async () => {
await next();
if (ctx.status >= 400) {
span.setStatus({ code: SpanStatusCode.ERROR, message: ctx.message });
} else {
span.setStatus({ code: SpanStatusCode.OK });
}
});
} catch (err) {
span.setStatus({ code: SpanStatusCode.ERROR, message: (err as Error).message });
throw err;
} finally {
span.end();
}
});
router.get('/test', async (ctx) => {
await doSomethingTraced();
ctx.body = 'OK';
});
app.use(router.routes());
app.use(router.allowedMethods());
return app;
}
function getParentSpanID(span: ReadableSpan) {
const ctx = span.parentSpanContext;
return ctx ? ctx.spanId : undefined;
}
describe('trace spans propagate', () => {
const memoryExporter = new InMemorySpanExporter();
beforeAll(async () => {
const provider = new NodeTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});
provider.register();
DBOS.setConfig({ name: 'trace-span-propagate', enableOTLP: true });
await DBOS.launch();
});
afterAll(async () => {
await DBOS.shutdown();
trace.disable();
context.disable();
});
test('from-outside-into-DBOS-calls', async () => {
expect(isTraceContextWorking()).toBe(true);
const app = createApp();
const server = app.listen(0);
const { port } = server.address() as AddressInfo;
const res = await fetch(`http://localhost:${port}/test`);
expect(res.status).toBe(200);
server.close();
const spans = memoryExporter.getFinishedSpans();
expect(spans.length).toBe(5);
console.debug(
spans.map((span) => ({
name: span.name,
traceId: span.spanContext().traceId,
spanId: span.spanContext().spanId,
parentSpanId: getParentSpanID(span),
attributes: span.attributes,
})),
);
// First two spans are probes, ignore them
const stepSpan = spans[2];
const workflowspan = spans[3];
const httpSpan = spans[4];
expect(getParentSpanID(stepSpan)).toBe(workflowspan?.spanContext().spanId);
expect(stepSpan?.spanContext().traceId).toBe(workflowspan?.spanContext().traceId);
expect(getParentSpanID(workflowspan)).toBe(httpSpan?.spanContext().spanId);
expect(workflowspan?.spanContext().traceId).toBe(httpSpan?.spanContext().traceId);
expect(workflowspan.attributes['my-lib.didSomething']).toBeTruthy();
});
});
describe('disable-otlp', () => {
const memoryExporter = new InMemorySpanExporter();
beforeAll(async () => {
const provider = new NodeTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});
provider.register();
DBOS.setConfig({ name: 'trace-span-propagate' });
await DBOS.launch();
});
afterAll(async () => {
await DBOS.shutdown();
trace.disable();
context.disable();
});
test('disable-otlp', async () => {
expect(isTraceContextWorking()).toBe(false);
const app = createApp();
const server = app.listen(0);
const { port } = server.address() as AddressInfo;
const res = await fetch(`http://localhost:${port}/test`);
expect(res.status).toBe(200);
server.close();
// With OTLP disabled, only the HTTP span is present
const spans = memoryExporter.getFinishedSpans();
expect(spans.length).toBe(1);
});
});
describe('external-provider-span-propagation', () => {
const memoryExporter = new InMemorySpanExporter();
beforeAll(async () => {
const provider = new NodeTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});
provider.register();
DBOS.setConfig({ name: 'external-provider-test', tracingEnabled: true });
await DBOS.launch();
});
afterAll(async () => {
await DBOS.shutdown();
trace.disable();
context.disable();
});
test('spans-flow-through-external-provider', async () => {
expect(isTraceContextWorking()).toBe(true);
const app = createApp();
const server = app.listen(0);
const { port } = server.address() as AddressInfo;
const res = await fetch(`http://localhost:${port}/test`);
expect(res.status).toBe(200);
server.close();
const spans = memoryExporter.getFinishedSpans();
const realSpans = spans.filter((s) => s.name !== 'probe');
expect(realSpans.length).toBe(3);
const stepSpan = realSpans[0];
const workflowSpan = realSpans[1];
const httpSpan = realSpans[2];
expect(getParentSpanID(stepSpan)).toBe(workflowSpan?.spanContext().spanId);
expect(stepSpan?.spanContext().traceId).toBe(workflowSpan?.spanContext().traceId);
expect(getParentSpanID(workflowSpan)).toBe(httpSpan?.spanContext().spanId);
expect(workflowSpan?.spanContext().traceId).toBe(httpSpan?.spanContext().traceId);
});
});
describe('dbos-standalone-tracing', () => {
beforeAll(async () => {
// No external provider — DBOS sets up its own BasicTracerProvider and context manager
DBOS.setConfig({ name: 'standalone-tracing-test', enableOTLP: true });
await DBOS.launch();
});
afterAll(async () => {
await DBOS.shutdown();
trace.disable();
context.disable();
});
test('context-propagation-works', () => {
expect(isTraceContextWorking()).toBe(true);
});
test('workflows-produce-real-spans', async () => {
const result = await doSomethingTraced();
expect(result).toBe('Done');
});
});