Skip to content

Commit

Permalink
Apply service naming workflow to databases (#3256)
Browse files Browse the repository at this point in the history
* add cassandra-driver versions
* add elasticsearch/opensearch versions
* add mongodb versions
* add pg versions
* add oracledb versions
  • Loading branch information
jbertran authored Jun 23, 2023
1 parent 6770761 commit 38d5699
Show file tree
Hide file tree
Showing 20 changed files with 367 additions and 72 deletions.
4 changes: 2 additions & 2 deletions packages/datadog-plugin-cassandra-driver/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class CassandraDriverPlugin extends DatabasePlugin {
query = combine(query)
}

this.startSpan('cassandra.query', {
service: this.config.service,
this.startSpan(this.operationName(), {
service: this.serviceName(this.config, this.system),
resource: trim(query, 5000),
type: 'cassandra',
kind: 'client',
Expand Down
19 changes: 17 additions & 2 deletions packages/datadog-plugin-cassandra-driver/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const semver = require('semver')
const agent = require('../../dd-trace/test/plugins/agent')
const { ERROR_TYPE, ERROR_MESSAGE, ERROR_STACK } = require('../../dd-trace/src/constants')
const namingSchema = require('./naming')

describe('Plugin', () => {
let cassandra
Expand Down Expand Up @@ -46,7 +47,8 @@ describe('Plugin', () => {
const query = 'SELECT now() FROM local;'
agent
.use(traces => {
expect(traces[0][0]).to.have.property('service', 'test-cassandra')
expect(traces[0][0]).to.have.property('name', namingSchema.outbound.opName)
expect(traces[0][0]).to.have.property('service', namingSchema.outbound.serviceName)
expect(traces[0][0]).to.have.property('resource', query)
expect(traces[0][0]).to.have.property('type', 'cassandra')
expect(traces[0][0].meta).to.have.property('db.type', 'cassandra')
Expand Down Expand Up @@ -142,6 +144,12 @@ describe('Plugin', () => {
})
})
})

withNamingSchema(
done => client.execute('SELECT now() FROM local;', err => err && done(err)),
() => namingSchema.outbound.opName,
() => namingSchema.outbound.serviceName
)
})

describe('with configuration', () => {
Expand Down Expand Up @@ -181,6 +189,12 @@ describe('Plugin', () => {

client.execute('SELECT now() FROM local;', err => err && done(err))
})

withNamingSchema(
done => client.execute('SELECT now() FROM local;', err => err && done(err)),
() => namingSchema.outbound.opName,
() => 'custom'
)
})

// Promise support added in 3.2.0
Expand Down Expand Up @@ -219,7 +233,8 @@ describe('Plugin', () => {

agent
.use(traces => {
expect(traces[0][0]).to.have.property('service', 'test-cassandra')
expect(traces[0][0]).to.have.property('name', namingSchema.outbound.opName)
expect(traces[0][0]).to.have.property('service', namingSchema.outbound.serviceName)
expect(traces[0][0]).to.have.property('resource', query)
expect(traces[0][0]).to.have.property('type', 'cassandra')
expect(traces[0][0].meta).to.have.property('db.type', 'cassandra')
Expand Down
14 changes: 14 additions & 0 deletions packages/datadog-plugin-cassandra-driver/test/naming.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { resolveNaming } = require('../../dd-trace/test/plugins/helpers')

module.exports = resolveNaming({
outbound: {
v0: {
opName: 'cassandra.query',
serviceName: 'test-cassandra'
},
v1: {
opName: 'cassandra.query',
serviceName: 'test'
}
}
})
4 changes: 2 additions & 2 deletions packages/datadog-plugin-elasticsearch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class ElasticsearchPlugin extends DatabasePlugin {
start ({ params }) {
const body = getBody(params.body || params.bulkBody)

this.startSpan(`${this.system}.query`, {
service: this.config.service,
this.startSpan(this.operationName(), {
service: this.serviceName(this.config),
resource: `${params.method} ${quantizePath(params.path)}`,
type: 'elasticsearch',
kind: 'client',
Expand Down
34 changes: 30 additions & 4 deletions packages/datadog-plugin-elasticsearch/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { ERROR_MESSAGE, ERROR_STACK, ERROR_TYPE } = require('../../dd-trace/src/constants')
const agent = require('../../dd-trace/test/plugins/agent')
const { breakThen, unbreakThen } = require('../../dd-trace/test/plugins/helpers')
const namingSchema = require('./naming')

describe('Plugin', () => {
let elasticsearch
Expand Down Expand Up @@ -57,6 +58,8 @@ describe('Plugin', () => {
it('should set the correct tags', done => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('name', namingSchema.outbound.opName)
expect(traces[0][0]).to.have.property('service', namingSchema.outbound.serviceName)
expect(traces[0][0].meta).to.have.property('component', 'elasticsearch')
expect(traces[0][0].meta).to.have.property('db.type', 'elasticsearch')
expect(traces[0][0].meta).to.have.property('span.kind', 'client')
Expand Down Expand Up @@ -90,6 +93,8 @@ describe('Plugin', () => {
it('should set the correct tags on msearch', done => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('name', namingSchema.outbound.opName)
expect(traces[0][0]).to.have.property('service', namingSchema.outbound.serviceName)
expect(traces[0][0].meta).to.have.property('component', 'elasticsearch')
expect(traces[0][0].meta).to.have.property('db.type', 'elasticsearch')
expect(traces[0][0].meta).to.have.property('span.kind', 'client')
Expand Down Expand Up @@ -143,7 +148,8 @@ describe('Plugin', () => {
it('should do automatic instrumentation', done => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('service', 'test-elasticsearch')
expect(traces[0][0]).to.have.property('name', namingSchema.outbound.opName)
expect(traces[0][0]).to.have.property('service', namingSchema.outbound.serviceName)
expect(traces[0][0]).to.have.property('resource', 'HEAD /')
expect(traces[0][0]).to.have.property('type', 'elasticsearch')
})
Expand Down Expand Up @@ -206,7 +212,8 @@ describe('Plugin', () => {
it('should do automatic instrumentation', done => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('service', 'test-elasticsearch')
expect(traces[0][0]).to.have.property('name', namingSchema.outbound.opName)
expect(traces[0][0]).to.have.property('service', namingSchema.outbound.serviceName)
expect(traces[0][0]).to.have.property('resource', 'HEAD /')
expect(traces[0][0]).to.have.property('type', 'elasticsearch')
})
Expand Down Expand Up @@ -276,6 +283,15 @@ describe('Plugin', () => {

client.ping().catch(done)
})

withNamingSchema(
() => client.search(
{ index: 'logstash-2000.01.01', body: {} },
hasCallbackSupport ? () => {} : undefined
),
() => namingSchema.outbound.opName,
() => namingSchema.outbound.serviceName
)
})
})

Expand All @@ -284,7 +300,7 @@ describe('Plugin', () => {

before(() => {
return agent.load('elasticsearch', {
service: 'test',
service: 'custom',
hooks: { query: (span, params) => {
span.addTags({ 'elasticsearch.params': 'foo', 'elasticsearch.method': params.method })
} }
Expand Down Expand Up @@ -316,7 +332,8 @@ describe('Plugin', () => {

agent
.use(traces => {
expect(traces[0][0]).to.have.property('service', 'test')
expect(traces[0][0]).to.have.property('name', namingSchema.outbound.opName)
expect(traces[0][0]).to.have.property('service', 'custom')
expect(traces[0][0].meta).to.have.property('component', 'elasticsearch')
expect(traces[0][0].meta).to.have.property('elasticsearch.params', 'foo')
expect(traces[0][0].meta).to.have.property('elasticsearch.method', 'POST')
Expand All @@ -330,6 +347,15 @@ describe('Plugin', () => {
client.ping().catch(done)
}
})

withNamingSchema(
() => client.search(
{ index: 'logstash-2000.01.01', body: {} },
hasCallbackSupport ? () => {} : undefined
),
() => namingSchema.outbound.opName,
() => 'custom'
)
})
})
})
Expand Down
14 changes: 14 additions & 0 deletions packages/datadog-plugin-elasticsearch/test/naming.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { resolveNaming } = require('../../dd-trace/test/plugins/helpers')

module.exports = resolveNaming({
outbound: {
v0: {
opName: 'elasticsearch.query',
serviceName: 'test-elasticsearch'
},
v1: {
opName: 'elasticsearch.query',
serviceName: 'test'
}
}
})
4 changes: 2 additions & 2 deletions packages/datadog-plugin-mongodb-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class MongodbCorePlugin extends DatabasePlugin {
const query = getQuery(ops)
const resource = truncate(getResource(this, ns, query, name))

this.startSpan('mongodb.query', {
service: this.config.service,
this.startSpan(this.operationName(), {
service: this.serviceName(this.config),
resource,
type: 'mongodb',
kind: 'client',
Expand Down
18 changes: 16 additions & 2 deletions packages/datadog-plugin-mongodb-core/test/core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const semver = require('semver')
const agent = require('../../dd-trace/test/plugins/agent')
const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants')
const namingSchema = require('./naming')

const withTopologies = fn => {
withVersions('mongodb-core', ['mongodb-core', 'mongodb'], '<4', (version, moduleName) => {
Expand Down Expand Up @@ -79,8 +80,8 @@ describe('Plugin', () => {
const span = traces[0][0]
const resource = `insert test.${collection}`

expect(span).to.have.property('name', 'mongodb.query')
expect(span).to.have.property('service', 'test-mongodb')
expect(span).to.have.property('name', namingSchema.outbound.opName)
expect(span).to.have.property('service', namingSchema.outbound.serviceName)
expect(span).to.have.property('resource', resource)
expect(span).to.have.property('type', 'mongodb')
expect(span.meta).to.have.property('span.kind', 'client')
Expand Down Expand Up @@ -305,6 +306,12 @@ describe('Plugin', () => {
error = err
})
})

withNamingSchema(
() => server.insert(`test.${collection}`, [{ a: 1 }], () => {}),
() => namingSchema.outbound.opName,
() => namingSchema.outbound.serviceName
)
})
})

Expand Down Expand Up @@ -335,13 +342,20 @@ describe('Plugin', () => {
it('should be configured with the correct values', done => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('name', namingSchema.outbound.opName)
expect(traces[0][0]).to.have.property('service', 'custom')
})
.then(done)
.catch(done)

server.insert(`test.${collection}`, [{ a: 1 }], () => {})
})

withNamingSchema(
() => server.insert(`test.${collection}`, [{ a: 1 }], () => {}),
() => namingSchema.outbound.opName,
() => 'custom'
)
})
})
})
Expand Down
18 changes: 16 additions & 2 deletions packages/datadog-plugin-mongodb-core/test/mongodb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const semver = require('semver')
const agent = require('../../dd-trace/test/plugins/agent')
const namingSchema = require('./naming')

const withTopologies = fn => {
const isOldNode = semver.satisfies(process.version, '<=12')
Expand Down Expand Up @@ -81,8 +82,8 @@ describe('Plugin', () => {
const span = traces[0][0]
const resource = `insert test.${collectionName}`

expect(span).to.have.property('name', 'mongodb.query')
expect(span).to.have.property('service', 'test-mongodb')
expect(span).to.have.property('name', namingSchema.outbound.opName)
expect(span).to.have.property('service', namingSchema.outbound.serviceName)
expect(span).to.have.property('resource', resource)
expect(span).to.have.property('type', 'mongodb')
expect(span.meta).to.have.property('span.kind', 'client')
Expand Down Expand Up @@ -238,6 +239,12 @@ describe('Plugin', () => {
})
}
})

withNamingSchema(
() => collection.insertOne({ a: 1 }, {}, () => {}),
() => namingSchema.outbound.opName,
() => namingSchema.outbound.serviceName
)
})
})

Expand All @@ -262,6 +269,7 @@ describe('Plugin', () => {
it('should be configured with the correct values', done => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('name', namingSchema.outbound.opName)
expect(traces[0][0]).to.have.property('service', 'custom')
})
.then(done)
Expand All @@ -285,6 +293,12 @@ describe('Plugin', () => {
_bin: new BSON.Binary()
}).toArray()
})

withNamingSchema(
() => collection.insertOne({ a: 1 }, () => {}),
() => namingSchema.outbound.opName,
() => 'custom'
)
})
})
})
Expand Down
14 changes: 14 additions & 0 deletions packages/datadog-plugin-mongodb-core/test/naming.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { resolveNaming } = require('../../dd-trace/test/plugins/helpers')

module.exports = resolveNaming({
outbound: {
v0: {
opName: 'mongodb.query',
serviceName: 'test-mongodb'
},
v1: {
opName: 'mongodb.query',
serviceName: 'test'
}
}
})
Loading

0 comments on commit 38d5699

Please sign in to comment.