Skip to content

Commit

Permalink
[pinpoint-apm#86] handle wrap refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
feelform committed Sep 12, 2023
1 parent 9cb96ee commit a4bc6bb
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions lib/instrumentation/module/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const { makeMethodDescriptorBuilder } = require('../../context/make-method-descr
const ExpressMethodDescriptorBuilder = require('../../context/express-method-descriptor-builder')
const semver = require('semver')

const parameterizedLayerPathSymbol = Symbol('PinpointParameterizedLayerPathSymbol')
const layerPatchedSymbol = Symbol('layer-patched')

// https://github.com/elastic/apm-agent-nodejs/blob/master/lib/instrumentation/modules/express.js
module.exports = function (agent, version, express) {
try {
Expand All @@ -28,8 +31,6 @@ module.exports = function (agent, version, express) {
return express
}

const layerPatchedSymbol = Symbol('layer-patched')

const MODULE_NAME = 'express'
const methodNames = ['GET', 'POST', 'PUT', 'HEAD', 'DELETE', 'OPTIONS', 'PATCH', 'ALL']

Expand Down Expand Up @@ -74,29 +75,38 @@ module.exports = function (agent, version, express) {
})

function patchLayer(layer, objectName, layerName, layerPath) {
/*
if (!layer[layerPatchedSymbol] && false) {
layer[layerPatchedSymbol] = true
shimmer.wrap(layer, 'handle', function (origin) {
let handle
if (origin.length !== 4) {
// https://github.com/elastic/apm-agent-nodejs/issues/443#issuecomment-455352070
if (!layer.route && layerPath && typeof next === 'function') {

}
handle = function (req, res, next) {

// https://github.com/elastic/apm-agent-nodejs/issues/443#issuecomment-455352070
if (!layer.route && layerPath && typeof next === 'function') {
safePush(req, parameterizedLayerPathSymbol, layerPath)
arguments[2] = function () {
if (!(req.route && arguments[0] instanceof Error)) {
req[parameterizedLayerPathSymbol].pop()
}
return next.apply(this, arguments)
}
}
return origin.apply(this, arguments)
}
} else {
handle = function (err, req, res, next) {

return origin.apply(this, arguments)
}
}
return handle
})
}
*/
let appAllMethodDescriptor
let handlerMethodDescriptor
if (objectName == 'route') {
Expand All @@ -112,6 +122,11 @@ module.exports = function (agent, version, express) {
})
}

function safePush(obj, prop, value) {
if (!obj[prop]) obj[prop] = []
obj[prop].push(value)
}

function recordHandle(original, objectName, layerName, appAllMethodDescriptor, handlerMethodDescriptor, layerPath) {
return function (req) {
const trace = agent.traceContext.currentTraceObject()
Expand Down

0 comments on commit a4bc6bb

Please sign in to comment.