Skip to content

Commit

Permalink
pg spans disconnected from parent (open-telemetry#225)
Browse files Browse the repository at this point in the history
* chore: gitignore

* chore: fixing context propagation for koa middleware layer
  • Loading branch information
obecny authored Oct 28, 2020
1 parent b10fa3a commit fa137d5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
9 changes: 6 additions & 3 deletions plugins/node/opentelemetry-koa-instrumentation/src/koa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,12 @@ export class KoaInstrumentation extends BasePlugin<typeof koa> {
const span = this._tracer.startSpan(metadata.name, {
attributes: metadata.attributes,
});
const result = await middlewareLayer(context, next);
span.end();
return result;

return this._tracer.withSpan(span, async () => {
const result = await middlewareLayer(context, next);
span.end();
return result;
});
};
}
}
Expand Down
21 changes: 20 additions & 1 deletion plugins/node/opentelemetry-koa-instrumentation/test/koa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ describe('Koa Instrumentation - Core Tests', () => {
await next();
};

const spanCreateMiddleware: koa.Middleware = async (ctx, next) => {
const span = tracer.startSpan('foo');
span.end();
await next();
};

const asyncMiddleware: koa.Middleware = async (ctx, next) => {
const start = Date.now();
await next();
Expand All @@ -106,11 +112,12 @@ describe('Koa Instrumentation - Core Tests', () => {
app.use((ctx, next) => tracer.withSpan(rootSpan, next));
app.use(customMiddleware);
app.use(simpleResponse);
app.use(spanCreateMiddleware);

await tracer.withSpan(rootSpan, async () => {
await httpRequest.get(`http://localhost:${port}`);
rootSpan.end();
assert.deepStrictEqual(memoryExporter.getFinishedSpans().length, 5);
assert.deepStrictEqual(memoryExporter.getFinishedSpans().length, 8);

assert.notStrictEqual(
memoryExporter
Expand All @@ -119,6 +126,18 @@ describe('Koa Instrumentation - Core Tests', () => {
undefined
);

const fooParentSpan = memoryExporter
.getFinishedSpans()
.find(span => span.name.includes('spanCreateMiddleware'));
assert.notStrictEqual(fooParentSpan, undefined);

const fooSpan = memoryExporter.getFinishedSpans().find(span => 'foo');
assert.notStrictEqual(fooSpan, undefined);
assert.strictEqual(
fooSpan!.parentSpanId,
fooParentSpan!.spanContext.spanId
);

const simpleResponseSpan = memoryExporter
.getFinishedSpans()
.find(span => span.name.includes('simpleResponse'));
Expand Down

0 comments on commit fa137d5

Please sign in to comment.