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

fix: correctly disable Express instrumentation #972

Merged
merged 2 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -101,9 +101,10 @@ export class ExpressInstrumentation extends InstrumentationBase<
(moduleExports, moduleVersion) => {
if (moduleExports === undefined) return;
diag.debug(`Removing patch for express@${moduleVersion}`);
this._unwrap(moduleExports.Router.prototype, 'route');
this._unwrap(moduleExports.Router.prototype, 'use');
this._unwrap(moduleExports.application.prototype, 'use');
const routerProto = moduleExports.Router as unknown as express.Router;
this._unwrap(routerProto, 'route');
this._unwrap(routerProto, 'use');
this._unwrap(moduleExports.application, 'use');
}
),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,12 @@ describe('ExpressInstrumentation', () => {
describe('Instrumenting normal get operations', () => {
it('should create a child span for middlewares', async () => {
const rootSpan = tracer.startSpan('rootSpan');
const app = express();
rauno56 marked this conversation as resolved.
Show resolved Hide resolved
app.use((req, res, next) =>
context.with(trace.setSpan(context.active(), rootSpan), next)
);
app.use(express.json());
const customMiddleware: express.RequestHandler = (req, res, next) => {
for (let i = 0; i < 1000000; i++) {
continue;
}
return next();
};
app.use(customMiddleware);
const router = express.Router();
app.use('/toto', router);
let finishListenerCount: number | undefined;
const { server, port } = await serverWithMiddleware(
tracer,
Expand Down Expand Up @@ -289,22 +281,30 @@ describe('ExpressInstrumentation', () => {

describe('Disabling plugin', () => {
it('should not create new spans', async () => {
instrumentation.disable();
const rootSpan = tracer.startSpan('rootSpan');
const app = express();
app.use(express.json());
app.use((req, res, next) => {
for (let i = 0; i < 1000; i++) {
continue;
const { server, port } = await serverWithMiddleware(
tracer,
rootSpan,
app => {
app.use(express.json());
const customMiddleware: express.RequestHandler = (req, res, next) => {
for (let i = 0; i < 1000; i++) {
continue;
}
return next();
};
app.use(customMiddleware);
Comment on lines +291 to +297
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe we could further simplify this test by removing this.

Copy link
Member

@rauno56 rauno56 Apr 13, 2022

Choose a reason for hiding this comment

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

I think it's here to ensure that the instrumentation is also disabled on the app level, not only router level.
Treating Express as a black box and keeping at least one of those middleware makes sense imho.

}
return next();
});
const { server, port } = await createServer(app);
);
assert.strictEqual(memoryExporter.getFinishedSpans().length, 0);
await context.with(
trace.setSpan(context.active(), rootSpan),
async () => {
await httpRequest.get(`http://localhost:${port}/toto/tata`);
rootSpan.end();
// There should be exactly one span, and it should be the root span.
// There should not be any spans from the Express instrumentation.
assert.deepEqual(memoryExporter.getFinishedSpans().length, 1);
assert.notStrictEqual(
memoryExporter.getFinishedSpans()[0],
Expand Down