Skip to content

Commit

Permalink
test: add HttpInstrumentation so an initial span is created, then Koa…
Browse files Browse the repository at this point in the history
… spans are created; add asserts for the expected spans
  • Loading branch information
trentm committed Dec 5, 2023
1 parent 325356b commit c90b67e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/context-async-hooks": "^1.8.0",
"@opentelemetry/contrib-test-utils": "^0.35.0",
"@opentelemetry/instrumentation-http": "^0.45.1",
"@opentelemetry/sdk-trace-base": "^1.8.0",
"@opentelemetry/sdk-trace-node": "^1.8.0",
"@types/mocha": "7.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

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

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

const sdk = createTestNodeSdk({
serviceName: 'use-koa',
instrumentations: [
new KoaInstrumentation()
new KoaInstrumentation(),
new HttpInstrumentation()
]
})
sdk.start();
Expand All @@ -35,7 +37,7 @@ import * as http from 'http';

const app = new Koa();

app.use(async function simpleResponse(ctx, next) {
app.use(async function simpleMiddleware(ctx, next) {
await next();
});

Expand Down
20 changes: 15 additions & 5 deletions plugins/node/opentelemetry-instrumentation-koa/test/koa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import * as KoaRouter from '@koa/router';
import { context, trace, Span } from '@opentelemetry/api';
import { context, trace, Span, SpanKind } from '@opentelemetry/api';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import * as testUtils from '@opentelemetry/contrib-test-utils';
Expand Down Expand Up @@ -724,11 +724,21 @@ describe('Koa Instrumentation', () => {
assert.ifError(err);
},
checkCollector: (collector: testUtils.TestCollector) => {
// use-koa.mjs creates a Koa app with a 'GET /post/:id' endpoint and
// a `simpleMiddleware`, then makes a single 'GET /post/0' request. We
// expect to see spans like this:
// span 'GET /post/:id'
// `- span 'middleware - simpleResponse'
// `- span 'router - /post/:id'
const spans = collector.sortedSpans;

console.log(JSON.stringify(spans, null, 2));

// TODO
assert.strictEqual(spans[0].name, 'GET /post/:id');
assert.strictEqual(spans[0].kind, SpanKind.CLIENT);
assert.strictEqual(spans[1].name, 'middleware - simpleMiddleware');
assert.strictEqual(spans[1].kind, SpanKind.SERVER);
assert.strictEqual(spans[1].parentSpanId, spans[0].spanId);
assert.strictEqual(spans[2].name, 'router - /post/:id');
assert.strictEqual(spans[2].kind, SpanKind.SERVER);
assert.strictEqual(spans[2].parentSpanId, spans[1].spanId);
},
});
});
Expand Down

0 comments on commit c90b67e

Please sign in to comment.