Skip to content

Commit 325356b

Browse files
committed
test(koa): add wip test
1 parent 29aca29 commit 325356b

File tree

5 files changed

+94
-4
lines changed

5 files changed

+94
-4
lines changed

package-lock.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/node/opentelemetry-instrumentation-koa/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"@koa/router": "12.0.0",
5050
"@opentelemetry/api": "^1.3.0",
5151
"@opentelemetry/context-async-hooks": "^1.8.0",
52+
"@opentelemetry/contrib-test-utils": "^0.35.0",
5253
"@opentelemetry/sdk-trace-base": "^1.8.0",
5354
"@opentelemetry/sdk-trace-node": "^1.8.0",
5455
"@types/mocha": "7.0.2",

plugins/node/opentelemetry-instrumentation-koa/src/instrumentation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ export class KoaInstrumentation extends InstrumentationBase<typeof koa> {
5555
return new InstrumentationNodeModuleDefinition<typeof koa>(
5656
'koa',
5757
['^2.0.0'],
58-
module => {
59-
const moduleExports =
58+
(module: any) => {
59+
const moduleExports: typeof koa =
6060
module[Symbol.toStringTag] === 'Module'
6161
? module.default // ESM
6262
: module; // CommonJS
@@ -74,8 +74,8 @@ export class KoaInstrumentation extends InstrumentationBase<typeof koa> {
7474
);
7575
return module;
7676
},
77-
module => {
78-
const moduleExports =
77+
(module: any) => {
78+
const moduleExports: typeof koa =
7979
module[Symbol.toStringTag] === 'Module'
8080
? module.default // ESM
8181
: module; // CommonJS
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
// Use koa from an ES module:
18+
// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs use-koa.mjs
19+
20+
import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils';
21+
22+
import { KoaInstrumentation } from '../../build/src/index.js';
23+
24+
const sdk = createTestNodeSdk({
25+
serviceName: 'use-koa',
26+
instrumentations: [
27+
new KoaInstrumentation()
28+
]
29+
})
30+
sdk.start();
31+
32+
import Koa from 'koa';
33+
import KoaRouter from '@koa/router';
34+
import * as http from 'http';
35+
36+
const app = new Koa();
37+
38+
app.use(async function simpleResponse(ctx, next) {
39+
await next();
40+
});
41+
42+
const router = new KoaRouter();
43+
router.get('/post/:id', ctx => {
44+
ctx.body = `Post id: ${ctx.params.id}`;
45+
});
46+
47+
app.use(router.routes());
48+
49+
const server = http.createServer(app.callback());
50+
await new Promise(resolve => server.listen(0, resolve));
51+
const port = server.address().port;
52+
53+
await new Promise(resolve => {
54+
http.get(`http://localhost:${port}/post/0`, (res) => {
55+
res.resume();
56+
res.on('end', () => {
57+
resolve();
58+
});
59+
})
60+
});
61+
62+
await new Promise(resolve => server.close(resolve));
63+
await sdk.shutdown();

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as KoaRouter from '@koa/router';
1818
import { context, trace, Span } from '@opentelemetry/api';
1919
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
2020
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
21+
import * as testUtils from '@opentelemetry/contrib-test-utils';
2122
import {
2223
InMemorySpanExporter,
2324
SimpleSpanProcessor,
@@ -709,4 +710,26 @@ describe('Koa Instrumentation', () => {
709710
);
710711
});
711712
});
713+
714+
it('should work with ESM usage', async () => {
715+
await testUtils.runTestFixture({
716+
cwd: __dirname,
717+
argv: ['fixtures/use-koa.mjs'],
718+
env: {
719+
NODE_OPTIONS:
720+
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
721+
NODE_NO_WARNINGS: '1',
722+
},
723+
checkResult: (err, stdout, stderr) => {
724+
assert.ifError(err);
725+
},
726+
checkCollector: (collector: testUtils.TestCollector) => {
727+
const spans = collector.sortedSpans;
728+
729+
console.log(JSON.stringify(spans, null, 2));
730+
731+
// TODO
732+
},
733+
});
734+
});
712735
});

0 commit comments

Comments
 (0)