Skip to content

Commit 939044d

Browse files
committed
Add error test case.
1 parent 1558b50 commit 939044d

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

plugins/node/opentelemetry-instrumentation-express/test/express.test.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,54 @@ describe('ExpressInstrumentation', () => {
344344
);
345345
});
346346

347+
it('captures thrown endpoint errors', async () => {
348+
const rootSpan = tracer.startSpan('rootSpan');
349+
let finishListenerCount: number | undefined;
350+
const httpServer = await serverWithMiddleware(tracer, rootSpan, app => {
351+
app.use((req, res, next) => {
352+
res.on('finish', () => {
353+
finishListenerCount = res.listenerCount('finish');
354+
});
355+
next();
356+
});
357+
358+
app.get('/error', (req, res) => {
359+
throw new Error('message');
360+
});
361+
});
362+
server = httpServer.server;
363+
port = httpServer.port;
364+
assert.strictEqual(memoryExporter.getFinishedSpans().length, 0);
365+
366+
await context.with(
367+
trace.setSpan(context.active(), rootSpan),
368+
async () => {
369+
await httpRequest.get(`http://localhost:${port}/error`);
370+
rootSpan.end();
371+
assert.strictEqual(finishListenerCount, 2);
372+
373+
const errorSpan = memoryExporter
374+
.getFinishedSpans()
375+
.find(span => span.name.includes('request handler'));
376+
assert.notStrictEqual(errorSpan, undefined);
377+
378+
assert.deepStrictEqual(errorSpan!.status, {
379+
code: SpanStatusCode.ERROR,
380+
message: 'message',
381+
});
382+
assert.notStrictEqual(
383+
errorSpan!.events.find(event => event.name === 'exception'),
384+
undefined
385+
);
386+
}
387+
);
388+
});
389+
347390
it('should not create span because there are no parent', async () => {
348391
const app = express();
349392
app.use(express.json());
350393
app.use((req, res, next) => {
351-
for (let i = 0; i < 1000000; i++) {}
394+
for (let i = 0; i < 1000000; i++) { }
352395
return next();
353396
});
354397
const router = express.Router();

0 commit comments

Comments
 (0)