Skip to content

MongoDB Integration: find method not measured correctly.  #4495

@timofei-iatsenko

Description

@timofei-iatsenko

Package + Version

"@sentry/node": "6.17.3",

Description

Mongo's methods which return a Cursor doesn't measured correctly.
Let's go thru an example:

const span = Sentry.getCurrentHub().getScope().getTransaction().startChild({
  op: 'fetching',
  description: 'Scenario Events'
});
const response = await this.collection.find({ scenario_id: tripId }).sort({ st: 1 }).toArray();
span.finish();

This will reported to the Sentry like this (this is example from our real app, so the op and description may vary):
image

First row in a screenshot is an outer span, and second row is timing from integration. The actual time for query execution is showed in the first line.

Root Cause

The mongo's integration doesn't know anything about cursors. It's just trying to decorate all top level client async methods with instreumentation code. So in case of find method which is synchronously returning FindCursor instead of promise it's just measure time of this sync operation instead of query.

// getsentry/sentry-javascript/packages/tracing/src/integrations/node/mongo.ts
 const maybePromise = orig.call(this, ...args) as Promise<unknown>;
          if (isThenable(maybePromise)) {
            return maybePromise.then((res: unknown) => {
              span?.finish();
              return res;
            });
          } else {
            span?.finish();
            return maybePromise;
          }
        }

Proposed Solution

Decorate Async Cursor methods, or return wrapped Cursor with instrumentation.

Motivation

In our application we use find/findOne = 90/10 so built-in integration for mongo just don't work for a most of the cases

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions