Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(instrumentation-express): propagate context and measure full handler spans #2638

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
// 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 @@ -96,7 +96,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 @@ -201,7 +201,7 @@ describe('ExpressInstrumentation', () => {
);
assert.strictEqual(response, 'tata');
rootSpan.end();
assert.strictEqual(finishListenerCount, 2);
assert.strictEqual(finishListenerCount, 3);
assert.notStrictEqual(
memoryExporter
.getFinishedSpans()
Expand Down
Loading