Skip to content

Commit

Permalink
Inject trace information into sql server queries
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeshay committed Nov 14, 2024
1 parent 59e9a2a commit 73f5bf0
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
6 changes: 3 additions & 3 deletions packages/datadog-instrumentations/src/tedious.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const {
const shimmer = require('../../datadog-shimmer')

addHook({ name: 'tedious', versions: ['>=1.0.0'] }, tedious => {
const startCh = channel('apm:tedious:request:start')
const finishCh = channel('apm:tedious:request:finish')
const errorCh = channel('apm:tedious:request:error')
const startCh = channel('apm:tedious:query:start')
const finishCh = channel('apm:tedious:query:finish')
const errorCh = channel('apm:tedious:query:error')
shimmer.wrap(tedious.Connection.prototype, 'makeRequest', makeRequest => function (request) {
if (!startCh.hasSubscribers) {
return makeRequest.apply(this, arguments)
Expand Down
24 changes: 14 additions & 10 deletions packages/datadog-plugin-tedious/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@ const DatabasePlugin = require('../../dd-trace/src/plugins/database')

class TediousPlugin extends DatabasePlugin {
static get id () { return 'tedious' }
static get operation () { return 'request' } // TODO: change to match other database plugins
static get system () { return 'mssql' }

start ({ queryOrProcedure, connectionConfig }) {
this.startSpan(this.operationName(), {
service: this.serviceName({ pluginConfig: this.config, system: this.system }),
resource: queryOrProcedure,
start (payload) {
const service =
this.serviceName({ pluginConfig: this.config, dbConfig: payload.connectionConfig, system: this.system })

const span = this.startSpan(this.operationName(), {
service,
resource: payload.queryOrProcedure,
type: 'sql',
kind: 'client',
meta: {
'db.type': 'mssql',
component: 'tedious',
'out.host': connectionConfig.server,
[CLIENT_PORT_KEY]: connectionConfig.options.port,
'db.user': connectionConfig.userName || connectionConfig.authentication.options.userName,
'db.name': connectionConfig.options.database,
'db.instance': connectionConfig.options.instanceName
'out.host': 'DEV8B',
[CLIENT_PORT_KEY]: payload.connectionConfig.options.port,
'db.user': payload.connectionConfig.userName || payload.connectionConfig.authentication.options.userName,
'db.name': payload.connectionConfig.options.database,
'db.instance': payload.connectionConfig.options.instanceName
}
})

payload.queryOrProcedure = this.injectDbmQuery(span, payload.queryOrProcedure, service)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('esm', () => {
const res = agent.assertMessageReceived(({ headers, payload }) => {
assert.propertyVal(headers, 'host', `127.0.0.1:${agent.port}`)
assert.isArray(payload)
assert.strictEqual(checkSpansForServiceName(payload, 'tedious.request'), true)
assert.strictEqual(checkSpansForServiceName(payload, 'tedious.query'), true)
})

proc = await spawnPluginIntegrationTestProc(sandbox.folder, 'server.mjs', agent.port)
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-plugin-tedious/test/naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { resolveNaming } = require('../../dd-trace/test/plugins/helpers')
const rawExpectedSchema = {
outbound: {
v0: {
opName: 'tedious.request',
opName: 'tedious.query',
serviceName: 'test-mssql'
},
v1: {
Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/src/service-naming/schemas/v0/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const storage = {
},
redis: redisConfig,
tedious: {
opName: () => 'tedious.request',
opName: () => 'tedious.query',
serviceName: ({ tracerService, pluginConfig, system }) =>
pluginConfig.service || fromSystem(tracerService, system)
}
Expand Down

0 comments on commit 73f5bf0

Please sign in to comment.