diff --git a/packages/datadog-plugin-mongodb-core/src/index.js b/packages/datadog-plugin-mongodb-core/src/index.js index e1ae187e8d5..80d7a58bb56 100644 --- a/packages/datadog-plugin-mongodb-core/src/index.js +++ b/packages/datadog-plugin-mongodb-core/src/index.js @@ -36,10 +36,14 @@ class MongodbCorePlugin extends DatabasePlugin { } } +function sanitizeBigInt (data) { + return JSON.stringify(data, (_key, value) => typeof value === 'bigint' ? value.toString() : value) +} + function getQuery (cmd) { if (!cmd || typeof cmd !== 'object' || Array.isArray(cmd)) return - if (cmd.query) return JSON.stringify(limitDepth(cmd.query)) - if (cmd.filter) return JSON.stringify(limitDepth(cmd.filter)) + if (cmd.query) return sanitizeBigInt(limitDepth(cmd.query)) + if (cmd.filter) return sanitizeBigInt(limitDepth(cmd.filter)) } function getResource (plugin, ns, query, operationName) { diff --git a/packages/datadog-plugin-mongodb-core/test/core.spec.js b/packages/datadog-plugin-mongodb-core/test/core.spec.js index 8cdabf4f275..31f1c0e6d7e 100644 --- a/packages/datadog-plugin-mongodb-core/test/core.spec.js +++ b/packages/datadog-plugin-mongodb-core/test/core.spec.js @@ -133,6 +133,39 @@ describe('Plugin', () => { }, () => {}) }) + it('should serialize BigInt without erroring', done => { + agent + .use(traces => { + const span = traces[0][0] + const resource = `find test.${collection}` + const query = `{"_id":"9999999999999999999999"}` + + expect(span).to.have.property('resource', resource) + expect(span.meta).to.have.property('mongodb.query', query) + }) + .then(done) + .catch(done) + + try { + server.command(`test.${collection}`, { + find: `test.${collection}`, + query: { + _id: 9999999999999999999999n + } + }, () => {}) + } catch (err) { + // It appears that most versions of MongodDB are happy to use a BigInt instance. + // For example, 2.0.0, 3.2.0, 3.1.10, etc. + // However, version 3.1.9 throws a synchronous error that it wants a Decimal128 instead. + if (err.message.includes('Decimal128')) { + // eslint-disable-next-line no-console + console.log('This version of mongodb-core does not accept BigInt instances') + return done() + } + done(err) + } + }) + it('should stringify BSON objects', done => { const BSON = require(`../../../versions/bson@4.0.0`).get() const id = '123456781234567812345678'