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

MongoDB instrumentation not working with MongoClient.connect static method #2467

Closed
1 of 3 tasks
abreu-gyant opened this issue Nov 24, 2021 · 1 comment · Fixed by #2512
Closed
1 of 3 tasks

MongoDB instrumentation not working with MongoClient.connect static method #2467

abreu-gyant opened this issue Nov 24, 2021 · 1 comment · Fixed by #2512
Assignees
Labels
agent-nodejs Make available for APM Agents project planning. community

Comments

@abreu-gyant
Copy link

Describe the bug

MongoDB instrumentation is not working when the MongoClient is created through the static method. Also, when creating the MongoClient through the constructor it only works if monitorCommands: true is set in the client options.

To Reproduce

Steps to reproduce the behavior:

import * as apm from 'elastic-apm-node';

apm.start({
  serviceName: 'MongoDB Test',
  serverUrl: 'http://localhost:8200/',
  logLevel: 'debug',
});

async function test() {
  const client = await MongoClient.connect('mongodb://localhost:27017/apm-test');
  const db = client.db();
  apm.startTransaction('test');
  const result = await db.listCollections().toArray();
  console.log(result);
  apm.endTransaction('end');
}

void test();

Expected behavior

APM spans of MongoDB should be logged.

Environment (please complete the following information)

  • OS: macOS (11.3.1)
  • Node.js version: v14.17.6
  • APM Server version: 7.15.0
  • Agent version: 3.24.0
  • MongoDB: 4.2.0

How are you starting the agent? (please tick one of the boxes)

  • Calling agent.start() directly (e.g. require('elastic-apm-node').start(...))
  • Requiring elastic-apm-node/start from within the source code
  • Starting node with -r elastic-apm-node/start
@github-actions github-actions bot added agent-nodejs Make available for APM Agents project planning. community triage labels Nov 24, 2021
trentm added a commit that referenced this issue Dec 10, 2021
- ensure a mongodb span is not accidentally a child of an inflight
  mongodb span
- Fix an issue where instrumentation of `new MongoClient(url)` would
  fail because the internal handling of `arguments` would add
  `arguments[1]` but arguments.length stayed at 1. Fix by first
  changing to an Array.
- Fix instrument of a connection made using the MongoClient.connect
  static method. #2467
@trentm
Copy link
Member

trentm commented Dec 10, 2021

@abreu-gyant Thanks very much for the issue! I believe I will have a fix for both issues in #2512

@trentm trentm self-assigned this Dec 10, 2021
@trentm trentm removed the triage label Dec 10, 2021
trentm added a commit that referenced this issue Jan 6, 2022
- Add 'mongodb' to test-all-versions (TAV) testing.
- Ensure a mongodb span is not accidentally a child of an inflight
  mongodb span
- Fix an issue where instrumentation of `new MongoClient(url)` would
  fail because the internal handling of `arguments` would add
  `arguments[1]` but arguments.length stayed at 1. Fix by first
  changing to an Array.
- Fix instrument of a connection made using the MongoClient.connect
  static method. #2467

Refs: #2430
Fixes: #2467
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent-nodejs Make available for APM Agents project planning. community
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants