@instana/core
Advanced tools
Comparing version 1.134.0 to 1.135.0
{ | ||
"name": "@instana/core", | ||
"version": "1.134.0", | ||
"version": "1.135.0", | ||
"description": "Core library for Instana's Node.js packages", | ||
@@ -135,3 +135,3 @@ "main": "src/index.js", | ||
}, | ||
"gitHead": "222ed99d7ce0fa90529e071a9395d726eafa9fb6" | ||
"gitHead": "20309759c0759e51266e8d49126e8e21e3e9e5c1" | ||
} |
@@ -11,2 +11,5 @@ /* | ||
const cls = require('../../cls'); | ||
let logger = require('../../../logger').getLogger('tracing/fastify', newLogger => { | ||
logger = newLogger; | ||
}); | ||
@@ -27,11 +30,11 @@ let active = false; | ||
// Fastify exposes a function as its module's export. We are replacing | ||
// this exposed function so that we gain access to the created fastify | ||
// object. This is necessary so that we can overwrite the fastify.route | ||
// method. fastify.route is the central routing registration method to | ||
// which all other functions delegate. | ||
// | ||
// We overwrite fastify.route so that we can wrap users's request | ||
// handlers. During request handler execution time, we can identify the | ||
// full path template. | ||
/** | ||
* Fastify is auto instrumend by our http server instrumention. | ||
* | ||
* In this instrumentation, we want to capture extra data on top. | ||
* We register a custom hook via the framework API and add this data to the | ||
* target http entry span. | ||
* | ||
* See https://www.fastify.io/docs/latest/Hooks | ||
*/ | ||
function instrument(build) { | ||
@@ -52,42 +55,23 @@ if (typeof build !== 'function') { | ||
if (app.route) { | ||
const originalRoute = app.route; | ||
app.route = function shimmedRoute(opts) { | ||
if (opts.handler) { | ||
const originalHandler = opts.handler; | ||
opts.handler = function shimmedHandler() { | ||
annotateHttpEntrySpanWithPathTemplate(app, opts); | ||
return originalHandler.apply(this, arguments); | ||
}; | ||
} | ||
// NOTE: all major versions support `addHook` - this is just a safe protection | ||
if (!app.addHook) { | ||
logger.warn('Instana was not able to instrument Fastify. The instrumention of http requests is still working.'); | ||
return app; | ||
} | ||
let preHandler; | ||
let preHandlerKey; | ||
if (opts.preHandler) { | ||
// In Fastify 2.x, the attribute is called preHandler. | ||
preHandler = opts.preHandler; | ||
preHandlerKey = 'preHandler'; | ||
} else if (opts.beforeHandler) { | ||
// In Fastify 1.x, the attribute is called beforeHandler. | ||
preHandler = opts.beforeHandler; | ||
preHandlerKey = 'beforeHandler'; | ||
} | ||
app.addHook('onRequest', function onRequest(request, reply, done) { | ||
try { | ||
// NOTE: v1 uses _context https://github.com/fastify/fastify/blob/1.x/fastify.js#L276 | ||
// v2/v3 uses context https://github.com/fastify/fastify/blob/2.x/test/handler-context.test.js#L41 | ||
const url = reply._context ? reply._context.config.url : reply.context.config.url; | ||
if (preHandler) { | ||
if (typeof preHandler === 'function') { | ||
opts[preHandlerKey] = function shimmedPreHandler() { | ||
annotateHttpEntrySpanWithPathTemplate(app, opts); | ||
return preHandler.apply(this, arguments); | ||
}; | ||
} else if (Array.isArray(preHandler)) { | ||
opts[preHandlerKey].unshift(function prependedBeforeHandler(request, reply, done) { | ||
annotateHttpEntrySpanWithPathTemplate(app, opts); | ||
done(); | ||
}); | ||
} | ||
} | ||
annotateHttpEntrySpanWithPathTemplate(app, url); | ||
} catch (err) { | ||
logger.warn( | ||
'Instana was not able to retrieve the path template. The instrumention of http requests is still working.' | ||
); | ||
} | ||
return originalRoute.apply(this, arguments); | ||
}; | ||
} | ||
done(); | ||
}); | ||
@@ -98,3 +82,7 @@ return app; | ||
function annotateHttpEntrySpanWithPathTemplate(app, opts) { | ||
/** | ||
* A request comes in GET /foo/22 | ||
* We want to trace GET /foo/:id | ||
*/ | ||
function annotateHttpEntrySpanWithPathTemplate(app, url) { | ||
if (!active) { | ||
@@ -111,3 +99,3 @@ return; | ||
const basePathOrPrefix = basePathDescriptor && basePathDescriptor.get ? app.prefix : app.basePath; | ||
span.data.http.path_tpl = (basePathOrPrefix || '') + (opts.url || opts.path || '/'); | ||
span.data.http.path_tpl = (basePathOrPrefix || '') + (url || '/'); | ||
} |
547000
15466