Skip to content

Commit 28a9fd0

Browse files
committed
Add new merged tests to v5 and fix paths
1 parent 39c4dcc commit 28a9fd0

File tree

5 files changed

+223
-2
lines changed

5 files changed

+223
-2
lines changed

plugins/node/opentelemetry-instrumentation-express/test/v4/fixtures/use-express-nested-router.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { promisify } from 'util';
1818
import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils';
1919

2020
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
21-
import { ExpressInstrumentation } from '../../build/src/index.js';
21+
import { ExpressInstrumentation } from '../../../build/src/index.js';
2222

2323
const sdk = createTestNodeSdk({
2424
serviceName: 'use-express-nested',

plugins/node/opentelemetry-instrumentation-express/test/v4/fixtures/use-express-router.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { promisify } from 'util';
1818
import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils';
1919

2020
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
21-
import { ExpressInstrumentation } from '../../build/src/index.js';
21+
import { ExpressInstrumentation } from '../../../build/src/index.js';
2222

2323
const sdk = createTestNodeSdk({
2424
serviceName: 'use-express-nested',

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,77 @@ describe('ExpressInstrumentation', () => {
633633
});
634634
});
635635

636+
it('should work with Express routers', async () => {
637+
await testUtils.runTestFixture({
638+
cwd: __dirname,
639+
argv: ['fixtures/use-express-router.mjs'],
640+
env: {
641+
NODE_OPTIONS:
642+
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
643+
NODE_NO_WARNINGS: '1',
644+
},
645+
checkResult: (err, stdout, stderr) => {
646+
assert.ifError(err);
647+
},
648+
checkCollector: (collector: testUtils.TestCollector) => {
649+
const spans = collector.sortedSpans;
650+
651+
assert.strictEqual(spans[0].name, 'GET');
652+
assert.strictEqual(spans[0].kind, testUtils.OtlpSpanKind.CLIENT);
653+
assert.strictEqual(spans[1].name, 'GET /api/user/:id');
654+
assert.strictEqual(spans[1].kind, testUtils.OtlpSpanKind.SERVER);
655+
assert.strictEqual(spans[1].parentSpanId, spans[0].spanId);
656+
assert.strictEqual(spans[2].name, 'middleware - simpleMiddleware');
657+
assert.strictEqual(spans[2].kind, testUtils.OtlpSpanKind.INTERNAL);
658+
assert.strictEqual(spans[2].parentSpanId, spans[1].spanId);
659+
assert.strictEqual(spans[3].name, 'router - /api/user/:id');
660+
assert.strictEqual(spans[3].kind, testUtils.OtlpSpanKind.INTERNAL);
661+
assert.strictEqual(spans[3].parentSpanId, spans[1].spanId);
662+
assert.strictEqual(spans[4].name, 'request handler - /api/user/:id');
663+
assert.strictEqual(spans[4].kind, testUtils.OtlpSpanKind.INTERNAL);
664+
assert.strictEqual(spans[4].parentSpanId, spans[1].spanId);
665+
},
666+
});
667+
});
668+
669+
it('should work with nested Express routers', async () => {
670+
await testUtils.runTestFixture({
671+
cwd: __dirname,
672+
argv: ['fixtures/use-express-nested-router.mjs'],
673+
env: {
674+
NODE_OPTIONS:
675+
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
676+
NODE_NO_WARNINGS: '1',
677+
},
678+
checkResult: (err, stdout, stderr) => {
679+
assert.ifError(err);
680+
},
681+
checkCollector: (collector: testUtils.TestCollector) => {
682+
const spans = collector.sortedSpans;
683+
684+
assert.strictEqual(spans[0].name, 'GET');
685+
assert.strictEqual(spans[0].kind, testUtils.OtlpSpanKind.CLIENT);
686+
assert.strictEqual(spans[1].name, 'GET /api/user/:id/posts/:postId');
687+
assert.strictEqual(spans[1].kind, testUtils.OtlpSpanKind.SERVER);
688+
assert.strictEqual(spans[2].name, 'middleware - simpleMiddleware');
689+
assert.strictEqual(spans[2].kind, testUtils.OtlpSpanKind.INTERNAL);
690+
assert.strictEqual(spans[2].parentSpanId, spans[1].spanId);
691+
assert.strictEqual(spans[3].name, 'router - /api/user/:id');
692+
assert.strictEqual(spans[3].kind, testUtils.OtlpSpanKind.INTERNAL);
693+
assert.strictEqual(spans[3].parentSpanId, spans[1].spanId);
694+
assert.strictEqual(spans[4].name, 'router - /:postId');
695+
assert.strictEqual(spans[4].kind, testUtils.OtlpSpanKind.INTERNAL);
696+
assert.strictEqual(spans[4].parentSpanId, spans[1].spanId);
697+
assert.strictEqual(
698+
spans[5].name,
699+
'request handler - /api/user/:id/posts/:postId'
700+
);
701+
assert.strictEqual(spans[5].kind, testUtils.OtlpSpanKind.INTERNAL);
702+
assert.strictEqual(spans[5].parentSpanId, spans[1].spanId);
703+
},
704+
});
705+
});
706+
636707
it('should set a correct transaction name for routes specified in RegEx', async () => {
637708
await testUtils.runTestFixture({
638709
cwd: __dirname,
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { promisify } from 'util';
18+
import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils';
19+
20+
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
21+
import { ExpressInstrumentation } from '../../../build/src/index.js';
22+
23+
const sdk = createTestNodeSdk({
24+
serviceName: 'use-express-nested',
25+
instrumentations: [
26+
new HttpInstrumentation(),
27+
new ExpressInstrumentation()
28+
]
29+
})
30+
31+
sdk.start();
32+
33+
import express from 'express';
34+
import * as http from 'http';
35+
36+
const app = express();
37+
38+
app.use(async function simpleMiddleware(req, res, next) {
39+
// Wait a short delay to ensure this "middleware - ..." span clearly starts
40+
// before the "router - ..." span. The test rely on a start-time-based sort
41+
// of the produced spans. If they start in the same millisecond, then tests
42+
// can be flaky.
43+
await promisify(setTimeout)(10);
44+
next();
45+
});
46+
47+
const userRouter = express.Router();
48+
const postsRouter = express.Router();
49+
50+
postsRouter.get('/:postId', (req, res, next) => {
51+
res.json({ hello: 'yes' });
52+
res.end();
53+
next();
54+
});
55+
56+
userRouter.get('/api/user/:id', (req, res, next) => {
57+
res.json({ hello: 'yes' });
58+
res.end();
59+
next();
60+
});
61+
62+
userRouter.use('/api/user/:id/posts', postsRouter);
63+
64+
app.use(userRouter);
65+
66+
const server = http.createServer(app);
67+
await new Promise(resolve => server.listen(0, resolve));
68+
const port = server.address().port;
69+
70+
71+
await new Promise(resolve => {
72+
http.get(`http://localhost:${port}/api/user/123/posts/321`, (res) => {
73+
res.resume();
74+
res.on('end', data => {
75+
resolve(data);
76+
});
77+
})
78+
});
79+
80+
await new Promise(resolve => server.close(resolve));
81+
await sdk.shutdown();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { promisify } from 'util';
18+
import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils';
19+
20+
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
21+
import { ExpressInstrumentation } from '../../../build/src/index.js';
22+
23+
const sdk = createTestNodeSdk({
24+
serviceName: 'use-express-nested',
25+
instrumentations: [new HttpInstrumentation(), new ExpressInstrumentation()],
26+
});
27+
28+
sdk.start();
29+
30+
import express from 'express';
31+
import * as http from 'http';
32+
33+
const app = express();
34+
35+
app.use(async function simpleMiddleware(req, res, next) {
36+
// Wait a short delay to ensure this "middleware - ..." span clearly starts
37+
// before the "router - ..." span. The test rely on a start-time-based sort
38+
// of the produced spans. If they start in the same millisecond, then tests
39+
// can be flaky.
40+
await promisify(setTimeout)(10);
41+
next();
42+
});
43+
44+
const router = express.Router();
45+
46+
router.get('/api/user/:id', (req, res, next) => {
47+
res.json({ hello: 'yes' });
48+
res.end();
49+
next();
50+
});
51+
52+
app.use(router);
53+
54+
const server = http.createServer(app);
55+
await new Promise(resolve => server.listen(0, resolve));
56+
const port = server.address().port;
57+
58+
59+
await new Promise(resolve => {
60+
http.get(`http://localhost:${port}/api/user/123`, (res) => {
61+
res.resume();
62+
res.on('end', data => {
63+
resolve(data);
64+
});
65+
})
66+
});
67+
68+
await new Promise(resolve => server.close(resolve));
69+
await sdk.shutdown();

0 commit comments

Comments
 (0)