From b27a955b27089be75a53310f40f58e3cfabb2901 Mon Sep 17 00:00:00 2001 From: Thomas Hunter II Date: Wed, 19 Feb 2025 13:05:33 -0800 Subject: [PATCH] Add pipeline to Mongodb query (#5299) Co-authored-by: Julien Vandeputte --- .../datadog-plugin-mongodb-core/src/index.js | 1 + .../test/mongodb.spec.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/packages/datadog-plugin-mongodb-core/src/index.js b/packages/datadog-plugin-mongodb-core/src/index.js index a60182458e1..52cab5e8944 100644 --- a/packages/datadog-plugin-mongodb-core/src/index.js +++ b/packages/datadog-plugin-mongodb-core/src/index.js @@ -70,6 +70,7 @@ function getQuery (cmd) { if (!cmd || typeof cmd !== 'object' || Array.isArray(cmd)) return if (cmd.query) return sanitizeBigInt(limitDepth(cmd.query)) if (cmd.filter) return sanitizeBigInt(limitDepth(cmd.filter)) + if (cmd.pipeline) return sanitizeBigInt(limitDepth(cmd.pipeline)) } function getResource (plugin, ns, query, operationName) { diff --git a/packages/datadog-plugin-mongodb-core/test/mongodb.spec.js b/packages/datadog-plugin-mongodb-core/test/mongodb.spec.js index db6ee8ffeec..cca4ea9a2c4 100644 --- a/packages/datadog-plugin-mongodb-core/test/mongodb.spec.js +++ b/packages/datadog-plugin-mongodb-core/test/mongodb.spec.js @@ -239,6 +239,25 @@ describe('Plugin', () => { }).toArray() }) + it('should log the aggregate pipeline in mongodb.query', done => { + agent + .use(traces => { + const span = traces[0][0] + const resource = 'aggregate test.$cmd' + const query = '[{"$match":{"_id":"1234"}},{"$project":{"_id":1}}]' + + expect(span).to.have.property('resource', resource) + expect(span.meta).to.have.property('mongodb.query', query) + }) + .then(done) + .catch(done) + + collection.aggregate([ + { $match: { _id: '1234' } }, + { $project: { _id: 1 } } + ]).toArray() + }) + it('should use the toJSON method of objects if it exists', done => { const id = '123456781234567812345678'