signalfx-tracing
Version:
Provides auto-instrumentation for JavaScript libraries and frameworks
47 lines (38 loc) • 1.17 kB
JavaScript
const METHODS = require('methods').concat('use', 'route', 'param', 'all')
const web = require('./util/web')
const routerPlugin = require('./router')
function createWrapMethod (tracer, config) {
config = web.normalizeConfig(config)
function expressTrace (req, res, next) {
web.instrument(tracer, config, req, res, 'express.request')
const span = web.active(req)
if (span) {
span.setTag('component', 'express')
}
next()
}
return function wrapMethod (original) {
return function methodWithTrace () {
if (!this._datadog_trace_patched && !this._router) {
this._datadog_trace_patched = true
this.use(expressTrace)
}
return original.apply(this, arguments)
}
}
}
function patch (express, tracer, config) {
this.wrap(express.application, METHODS, createWrapMethod(tracer, config))
routerPlugin.patch.call(this, { prototype: express.Router }, tracer, config)
}
function unpatch (express) {
this.unwrap(express.application, METHODS)
routerPlugin.unpatch.call(this, { prototype: express.Router })
}
module.exports = {
name: 'express',
versions: ['>=4'],
patch,
unpatch
}