Skip to content
This repository was archived by the owner on Jun 16, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 4 additions & 3 deletions packages/datadog-plugin-mongodb-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MongodbCorePlugin extends DatabasePlugin {
static get peerServicePrecursors () { return [] }
start ({ ns, ops, options = {}, name }) {
const query = getQuery(ops)
const resource = truncate(getResource(this, ns, query, name))
const resource = truncate(getResource(this, ns, query, name, ops.aggregate))
this.startSpan(this.operationName(), {
service: this.serviceName({ pluginConfig: this.config }),
resource,
Expand Down Expand Up @@ -47,8 +47,9 @@ function getQuery (cmd) {
if (cmd.pipeline) return sanitizeBigInt(limitDepth(cmd.pipeline))
}

function getResource (plugin, ns, query, operationName) {
const parts = [operationName, ns]
function getResource (plugin, ns, query, operationName, aggregateCollection) {
const appendedNs = aggregateCollection ? `${ns}.${aggregateCollection}` : ns
const parts = [operationName, appendedNs]

if (plugin.config.queryInResourceName && query) {
parts.push(query)
Expand Down
19 changes: 19 additions & 0 deletions packages/datadog-plugin-mongodb-core/test/mongodb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,25 @@ describe('Plugin', () => {
}).toArray()
})

it('should append the collection name to ressource for aggregate queries', done => {
agent
.use(traces => {
const span = traces[0][0]
const resource = `aggregate test.$cmd.${collectionName}`
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'

Expand Down