Skip to content

Commit

Permalink
MySQL databases - service naming (#3057)
Browse files Browse the repository at this point in the history
* add v0 naming to mysql integrations
* add v1 to mysql integrations
* add tests on v1 for mysql databases

---------

Co-authored-by: Thomas Hunter II <tlhunter@datadog.com>
  • Loading branch information
2 people authored and nsavoire committed Jun 20, 2023
1 parent 405d58f commit b06369f
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 21 deletions.
14 changes: 10 additions & 4 deletions packages/datadog-plugin-mariadb/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const agent = require('../../dd-trace/test/plugins/agent')
const proxyquire = require('proxyquire').noPreserveCache()
const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants')

const namingSchema = require('./naming')

// https://github.com/mariadb-corporation/mariadb-connector-nodejs/commit/0a90b71ab20ab4e8b6a86a77ba291bba8ba6a34e
const range = semver.gte(process.version, '15.0.0') ? '>=2.5.1' : '>=2'

Expand Down Expand Up @@ -86,7 +88,8 @@ describe('Plugin', () => {
it('should do automatic instrumentation', done => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('service', 'test-mariadb')
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', 'SELECT 1 + 1 AS solution')
expect(traces[0][0]).to.have.property('type', 'sql')
expect(traces[0][0].meta).to.have.property('span.kind', 'client')
Expand All @@ -108,7 +111,8 @@ describe('Plugin', () => {
it('should support prepared statement shorthand', done => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('service', 'test-mariadb')
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', 'SELECT ? + ? AS solution')
expect(traces[0][0]).to.have.property('type', 'sql')
expect(traces[0][0].meta).to.have.property('span.kind', 'client')
Expand All @@ -129,7 +133,8 @@ describe('Plugin', () => {
it('should support prepared statements', done => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('service', 'test-mariadb')
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', 'SELECT ? + ? AS solution')
expect(traces[0][0]).to.have.property('type', 'sql')
expect(traces[0][0].meta).to.have.property('span.kind', 'client')
Expand Down Expand Up @@ -293,7 +298,8 @@ describe('Plugin', () => {
it('should do automatic instrumentation', done => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('service', 'test-mariadb')
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', 'SELECT 1 + 1 AS solution')
expect(traces[0][0]).to.have.property('type', 'sql')
expect(traces[0][0].meta).to.have.property('span.kind', 'client')
Expand Down
14 changes: 14 additions & 0 deletions packages/datadog-plugin-mariadb/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: 'mariadb.query',
serviceName: 'test-mariadb'
},
v1: {
opName: 'mariadb.query',
serviceName: 'test'
}
}
})
13 changes: 2 additions & 11 deletions packages/datadog-plugin-mysql/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ class MySQLPlugin extends DatabasePlugin {
static get system () { return 'mysql' }

start (payload) {
const service = getServiceName(this.config, payload.conf)

this.startSpan(`${this.system}.query`, {
const service = this.serviceName(this.config, payload.conf, this.system)
this.startSpan(this.operationName(), {
service,
resource: payload.sql,
type: 'sql',
Expand All @@ -27,12 +26,4 @@ class MySQLPlugin extends DatabasePlugin {
}
}

function getServiceName (config, dbConfig) {
if (typeof config.service === 'function') {
return config.service(dbConfig)
}

return config.service
}

module.exports = MySQLPlugin
10 changes: 8 additions & 2 deletions packages/datadog-plugin-mysql/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const agent = require('../../dd-trace/test/plugins/agent')
const proxyquire = require('proxyquire').noPreserveCache()
const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants')

const namingSchema = require('./naming')

describe('Plugin', () => {
let mysql
let tracer
Expand Down Expand Up @@ -67,7 +69,8 @@ describe('Plugin', () => {

it('should do automatic instrumentation', done => {
agent.use(traces => {
expect(traces[0][0]).to.have.property('service', 'test-mysql')
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', 'SELECT 1 + 1 AS solution')
expect(traces[0][0]).to.have.property('type', 'sql')
expect(traces[0][0].meta).to.have.property('span.kind', 'client')
Expand Down Expand Up @@ -134,6 +137,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')
done()
})
Expand Down Expand Up @@ -167,6 +171,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')
sinon.assert.calledWith(serviceSpy, sinon.match({
host: 'localhost',
Expand Down Expand Up @@ -203,7 +208,8 @@ describe('Plugin', () => {

it('should do automatic instrumentation', done => {
agent.use(traces => {
expect(traces[0][0]).to.have.property('service', 'test-mysql')
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', 'SELECT 1 + 1 AS solution')
expect(traces[0][0]).to.have.property('type', 'sql')
expect(traces[0][0].meta).to.have.property('span.kind', 'client')
Expand Down
14 changes: 14 additions & 0 deletions packages/datadog-plugin-mysql/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: 'mysql.query',
serviceName: 'test-mysql'
},
v1: {
opName: 'mysql.query',
serviceName: 'test'
}
}
})
14 changes: 10 additions & 4 deletions packages/datadog-plugin-mysql2/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const agent = require('../../dd-trace/test/plugins/agent')
const proxyquire = require('proxyquire').noPreserveCache()
const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants')

const namingSchema = require('./naming')

describe('Plugin', () => {
let mysql2
let tracer
Expand Down Expand Up @@ -74,7 +76,8 @@ describe('Plugin', () => {
it('should do automatic instrumentation', done => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('service', 'test-mysql')
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', 'SELECT 1 + 1 AS solution')
expect(traces[0][0]).to.have.property('type', 'sql')
expect(traces[0][0].meta).to.have.property('span.kind', 'client')
Expand All @@ -94,7 +97,8 @@ describe('Plugin', () => {
it('should support prepared statement shorthand', done => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('service', 'test-mysql')
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', 'SELECT ? + ? AS solution')
expect(traces[0][0]).to.have.property('type', 'sql')
expect(traces[0][0].meta).to.have.property('span.kind', 'client')
Expand All @@ -116,7 +120,8 @@ describe('Plugin', () => {
it('should support prepared statements', done => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('service', 'test-mysql')
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', 'SELECT ? + ? AS solution')
expect(traces[0][0]).to.have.property('type', 'sql')
expect(traces[0][0].meta).to.have.property('span.kind', 'client')
Expand Down Expand Up @@ -262,7 +267,8 @@ describe('Plugin', () => {
it('should do automatic instrumentation', done => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('service', 'test-mysql')
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', 'SELECT 1 + 1 AS solution')
expect(traces[0][0]).to.have.property('type', 'sql')
expect(traces[0][0].meta).to.have.property('span.kind', 'client')
Expand Down
14 changes: 14 additions & 0 deletions packages/datadog-plugin-mysql2/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: 'mysql.query',
serviceName: 'test-mysql'
},
v1: {
opName: 'mysql.query',
serviceName: 'test'
}
}
})
19 changes: 19 additions & 0 deletions packages/dd-trace/src/service-naming/schemas/v0/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ function fromSystem (service, system) {
return system ? `${service}-${system}` : undefined
}

function mysqlServiceName (service, config, dbConfig, system) {
if (typeof config.service === 'function') {
return config.service(dbConfig)
}
return config.service ? config.service : fromSystem(service, system)
}

const redisConfig = {
opName: () => 'redis.command',
serviceName: (service, config, system, connectionName) => {
Expand All @@ -22,10 +29,22 @@ const redisConfig = {
const storage = {
client: {
ioredis: redisConfig,
mariadb: {
opName: () => 'mariadb.query',
serviceName: mysqlServiceName
},
memcached: {
opName: () => 'memcached.command',
serviceName: (service, config, system) => config.service || fromSystem(service, system)
},
mysql: {
opName: () => 'mysql.query',
serviceName: mysqlServiceName
},
mysql2: {
opName: () => 'mysql.query',
serviceName: mysqlServiceName
},
redis: redisConfig,
tedious: {
opName: () => 'tedious.request',
Expand Down
13 changes: 13 additions & 0 deletions packages/dd-trace/src/service-naming/schemas/v1/storage.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { identityService } = require('../util')

function configWithFallback (service, config) {
return config.service || service
}
Expand All @@ -7,13 +9,24 @@ const redisNaming = {
serviceName: configWithFallback
}

const mySQLNaming = {
opName: () => 'mysql.query',
serviceName: identityService
}

const storage = {
client: {
ioredis: redisNaming,
mariadb: {
opName: () => 'mariadb.query',
serviceName: identityService
},
memcached: {
opName: () => 'memcached.command',
serviceName: configWithFallback
},
mysql: mySQLNaming,
mysql2: mySQLNaming,
redis: redisNaming,
tedious: {
opName: () => 'mssql.query',
Expand Down

0 comments on commit b06369f

Please sign in to comment.