-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add tedious integration
- Loading branch information
Showing
9 changed files
with
560 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
'use strict' | ||
|
||
const Tags = require('../../../ext/tags') | ||
const Kinds = require('../../../ext/kinds') | ||
const analyticsSampler = require('../../dd-trace/src/analytics_sampler') | ||
const tx = require('../../dd-trace/src/plugins/util/tx') | ||
|
||
const procnameRegex = /^sp_[a-z]+$/ | ||
|
||
function createWrapRequestClass (tracer) { | ||
return function wrapRequestClass (Request) { | ||
class RequestWithTrace extends Request { | ||
constructor (sqlTextOrProcedure, callback) { | ||
super(sqlTextOrProcedure, callback) | ||
tracer.scope().bind(this) | ||
} | ||
} | ||
|
||
return RequestWithTrace | ||
} | ||
} | ||
|
||
function createWrapConnectionClass (tracer) { | ||
return function wrapConnectionClass (Connection) { | ||
class ConnectionWithTrace extends Connection { | ||
constructor (config) { | ||
super(config) | ||
tracer.scope().bind(this) | ||
} | ||
} | ||
|
||
return ConnectionWithTrace | ||
} | ||
} | ||
|
||
function createWrapMakeRequest (tracer, config) { | ||
return function wrapMakeRequest (makeRequest) { | ||
return function makeRequestWithTrace (request) { | ||
const connectionConfig = this.config | ||
const scope = tracer.scope() | ||
const childOf = scope.active() | ||
const query = getQuery(request) | ||
|
||
if (!query) { | ||
return makeRequest.apply(this, arguments) | ||
} | ||
|
||
const span = tracer.startSpan(`tedious.request`, { | ||
childOf, | ||
tags: { | ||
[Tags.SPAN_KIND]: Kinds.CLIENT, | ||
'db.type': 'mssql', | ||
'span.type': 'sql', | ||
'component': 'tedious', | ||
'service.name': config.service || `${tracer._service}-mssql`, | ||
'resource.name': query | ||
} | ||
}) | ||
|
||
addConnectionTags(span, connectionConfig) | ||
addDatabaseTags(span, connectionConfig) | ||
addProcIdTags(span, request) | ||
|
||
analyticsSampler.sample(span, config.analytics) | ||
request.callback = tx.wrap(span, request.callback) | ||
|
||
return scope.bind(makeRequest, span).apply(this, arguments) | ||
} | ||
} | ||
} | ||
|
||
function createWrapGetRowStream (tracer) { | ||
return function wrapGetRowStream (getRowStream) { | ||
return function getRowStreamWithTrace () { | ||
const scope = tracer.scope() | ||
|
||
const rowToPacketTransform = getRowStream.apply(this, arguments) | ||
return scope.bind(rowToPacketTransform) | ||
} | ||
} | ||
} | ||
|
||
function getQuery (request) { | ||
if (request.parameters) { | ||
if (request.parameters.length === 0) { | ||
return request.sqlTextOrProcedure | ||
} else { | ||
const statement = request.parametersByName.statement || request.parametersByName.stmt | ||
return statement.value | ||
} | ||
} | ||
} | ||
|
||
function addConnectionTags (span, connectionConfig) { | ||
span.setTag('out.host', connectionConfig.server) | ||
span.setTag('out.port', connectionConfig.options.port) | ||
} | ||
|
||
function addDatabaseTags (span, connectionConfig) { | ||
span.setTag('db.user', connectionConfig.userName || connectionConfig.authentication.options.userName) | ||
span.setTag('db.name', connectionConfig.options.database) | ||
span.setTag('db.instance', connectionConfig.options.instanceName) | ||
} | ||
|
||
function addProcIdTags (span, request) { | ||
if (!procnameRegex.test(request.sqlTextOrProcedure)) return | ||
span.setTag('tds.proc.name', request.sqlTextOrProcedure) | ||
} | ||
|
||
module.exports = [ | ||
{ | ||
name: 'tedious', | ||
versions: [ '>=1.0.0' ], | ||
patch (tedious, tracer, config) { | ||
this.wrap(tedious, 'Request', createWrapRequestClass(tracer)) | ||
this.wrap(tedious, 'Connection', createWrapConnectionClass(tracer)) | ||
this.wrap(tedious.Connection.prototype, 'makeRequest', createWrapMakeRequest(tracer, config)) | ||
|
||
if (tedious.BulkLoad && tedious.BulkLoad.prototype.getRowStream) { | ||
this.wrap(tedious.BulkLoad.prototype, 'getRowStream', createWrapGetRowStream(tracer)) | ||
} | ||
}, | ||
unpatch (tedious) { | ||
this.unwrap(tedious, 'Request') | ||
this.unwrap(tedious, 'Connection') | ||
this.unwrap(tedious.Connection.prototype, 'makeRequest') | ||
|
||
if (tedious.BulkLoad && tedious.BulkLoad.prototype.getRowStream) { | ||
this.unwrap(tedious.BulkLoad.prototype, 'getRowStream') | ||
} | ||
} | ||
} | ||
] |
Oops, something went wrong.