Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ export class ExpressInstrumentation extends InstrumentationBase<ExpressInstrumen
attributes: Object.assign(attributes, metadata.attributes),
});

const parentContext = context.active();
let currentContext = trace.setSpan(parentContext, span);

const { requestHook } = instrumentation.getConfig();
if (requestHook) {
safeExecuteInTheMiddle(
Expand All @@ -234,12 +237,15 @@ export class ExpressInstrumentation extends InstrumentationBase<ExpressInstrumen
}

let spanHasEnded = false;
// TODO: Fix router spans (getRouterPath does not work properly) to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you expand on what you mean by not working properly or create an issue so this doesn't get lost?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can see in the nested routers test that the created router spans never include the /posts section of the route. If I were to propagate context and nest them we'd end up with /api/user/:id -> /:postId -> /api/user/posts/:postId which is really confusing.

// have useful names before removing this branch
if (
metadata.attributes[AttributeNames.EXPRESS_TYPE] !==
ExpressLayerType.MIDDLEWARE
metadata.attributes[AttributeNames.EXPRESS_TYPE] ===
ExpressLayerType.ROUTER
) {
span.end();
spanHasEnded = true;
currentContext = parentContext;
}
// listener for response.on('finish')
const onResponseFinish = () => {
Expand Down Expand Up @@ -278,12 +284,12 @@ export class ExpressInstrumentation extends InstrumentationBase<ExpressInstrumen
(req[_LAYERS_STORE_PROPERTY] as string[]).pop();
}
const callback = args[callbackIdx] as Function;
return callback.apply(this, arguments);
return context.bind(parentContext, callback).apply(this, arguments);
};
}

try {
return original.apply(this, arguments);
return context.bind(currentContext, original).apply(this, arguments);
} catch (anyError) {
const [error, message] = asErrorAndMessage(anyError);
span.recordException(error);
Expand All @@ -296,7 +302,7 @@ export class ExpressInstrumentation extends InstrumentationBase<ExpressInstrumen
/**
* At this point if the callback wasn't called, that means either the
* layer is asynchronous (so it will call the callback later on) or that
* the layer directly end the http response, so we'll hook into the "finish"
* the layer directly ends the http response, so we'll hook into the "finish"
* event to handle the later case.
*/
if (!spanHasEnded) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('ExpressInstrumentation', () => {
);
assert.strictEqual(response, 'tata');
rootSpan.end();
assert.strictEqual(finishListenerCount, 2);
assert.strictEqual(finishListenerCount, 3);
assert.notStrictEqual(
memoryExporter
.getFinishedSpans()
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('ExpressInstrumentation', () => {
);
assert.strictEqual(response, 'tata');
rootSpan.end();
assert.strictEqual(finishListenerCount, 2);
assert.strictEqual(finishListenerCount, 3);
assert.notStrictEqual(
memoryExporter
.getFinishedSpans()
Expand Down