Skip to content

Commit c90b67e

Browse files
committed
test: add HttpInstrumentation so an initial span is created, then Koa spans are created; add asserts for the expected spans
1 parent 325356b commit c90b67e

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

package-lock.json

Lines changed: 2 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
@@ -50,6 +50,7 @@
5050
"@opentelemetry/api": "^1.3.0",
5151
"@opentelemetry/context-async-hooks": "^1.8.0",
5252
"@opentelemetry/contrib-test-utils": "^0.35.0",
53+
"@opentelemetry/instrumentation-http": "^0.45.1",
5354
"@opentelemetry/sdk-trace-base": "^1.8.0",
5455
"@opentelemetry/sdk-trace-node": "^1.8.0",
5556
"@types/mocha": "7.0.2",

plugins/node/opentelemetry-instrumentation-koa/test/fixtures/use-koa.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919

2020
import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils';
2121

22+
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
2223
import { KoaInstrumentation } from '../../build/src/index.js';
2324

2425
const sdk = createTestNodeSdk({
2526
serviceName: 'use-koa',
2627
instrumentations: [
27-
new KoaInstrumentation()
28+
new KoaInstrumentation(),
29+
new HttpInstrumentation()
2830
]
2931
})
3032
sdk.start();
@@ -35,7 +37,7 @@ import * as http from 'http';
3537

3638
const app = new Koa();
3739

38-
app.use(async function simpleResponse(ctx, next) {
40+
app.use(async function simpleMiddleware(ctx, next) {
3941
await next();
4042
});
4143

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import * as KoaRouter from '@koa/router';
18-
import { context, trace, Span } from '@opentelemetry/api';
18+
import { context, trace, Span, SpanKind } from '@opentelemetry/api';
1919
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
2020
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
2121
import * as testUtils from '@opentelemetry/contrib-test-utils';
@@ -724,11 +724,21 @@ describe('Koa Instrumentation', () => {
724724
assert.ifError(err);
725725
},
726726
checkCollector: (collector: testUtils.TestCollector) => {
727+
// use-koa.mjs creates a Koa app with a 'GET /post/:id' endpoint and
728+
// a `simpleMiddleware`, then makes a single 'GET /post/0' request. We
729+
// expect to see spans like this:
730+
// span 'GET /post/:id'
731+
// `- span 'middleware - simpleResponse'
732+
// `- span 'router - /post/:id'
727733
const spans = collector.sortedSpans;
728-
729-
console.log(JSON.stringify(spans, null, 2));
730-
731-
// TODO
734+
assert.strictEqual(spans[0].name, 'GET /post/:id');
735+
assert.strictEqual(spans[0].kind, SpanKind.CLIENT);
736+
assert.strictEqual(spans[1].name, 'middleware - simpleMiddleware');
737+
assert.strictEqual(spans[1].kind, SpanKind.SERVER);
738+
assert.strictEqual(spans[1].parentSpanId, spans[0].spanId);
739+
assert.strictEqual(spans[2].name, 'router - /post/:id');
740+
assert.strictEqual(spans[2].kind, SpanKind.SERVER);
741+
assert.strictEqual(spans[2].parentSpanId, spans[1].spanId);
732742
},
733743
});
734744
});

0 commit comments

Comments
 (0)