Skip to content

Commit

Permalink
Add options arg to view/server plugins.
Browse files Browse the repository at this point in the history
Additional logging of patch errors.
  • Loading branch information
bmacnaughton committed Aug 27, 2018
1 parent 7732192 commit b2f830e
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions lib/probes/hapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ const log = ao.loggers
const Span = ao.Span
const conf = ao.probes.hapi

const notFunction = 'hapi - %s is not a function'
const protoNotFound = 'hapi - %s.prototype not found'

const trackedRenders = new WeakMap()
function wrapPrepare (request) {
if (typeof request.prepare !== 'function') return
if (typeof request.prepare !== 'function') {
log.patching(notFunction, 'request.prepare')
return
}
shimmer.wrap(request, 'prepare', fn => function (response, callback) {
const last = Span.last
if (!last || !conf.enabled) {
Expand All @@ -31,14 +37,19 @@ function wrapPrepare (request) {
trackedRenders.set(response, span)
span.async = true
span.enter()
} catch (e) {}
} catch (e) {
log.error('hapi - failed to enter hapi-render span')
}

return fn.call(this, response, callback)
})
}

function wrapMarshal (request) {
if (typeof request.marshal !== 'function') return
if (typeof request.marshal !== 'function') {
log.patching(notFunction, 'request.marshal')
return
}
shimmer.wrap(request, 'marshal', fn => function (response, callback) {
const span = trackedRenders.get(response)
if (!span || !conf.enabled) {
Expand All @@ -51,7 +62,9 @@ function wrapMarshal (request) {
const topSpan = ao.requestStore.get('topSpan')
rum.inject(response.source.context, ao.rumId, topSpan.events.exit)
}
} catch (e) {}
} catch (e) {
log.error('hapi - rum.inject failed')
}

return fn.call(this, response, utility.before(() => span.exit(), callback))
})
Expand All @@ -64,7 +77,7 @@ function wrapPrepareMarshal (request) {

function wrapManager (manager) {
if (typeof manager._response !== 'function') {
log.patching('hapi.manager._response not a function')
log.patching(notFunction, 'manager._response')
return
}
shimmer.wrap(manager, '_response', fn => function () {
Expand All @@ -76,7 +89,7 @@ function wrapManager (manager) {

function wrapRender (render, version) {
if (typeof render !== 'function') {
log.patching('hapi.render not a function')
log.patching(notFunction, 'render')
return render
}
return function (filename, context, options, callback) {
Expand Down Expand Up @@ -108,7 +121,7 @@ function wrapRender (render, version) {
const patchedViews = new WeakMap()
function patchView (view, version) {
if (typeof view.render !== 'function') {
log.patching('hapi.view.render not a function')
log.patching(notFunction, 'view.render')
return
}
if (patchedViews.get(view)) return
Expand All @@ -118,7 +131,7 @@ function patchView (view, version) {

function patchConnection (conn) {
if (typeof conn.views !== 'function') {
log.patching('hapi.connection.views not a function')
log.patching(notFunction, 'connection.views')
return
}

Expand All @@ -137,15 +150,15 @@ function patchConnection (conn) {
if (proto) {
patchView(proto)
} else {
log.patching('hapi.views.constructor.prototype not found')
log.patching(protoNotFound, 'views.constructor')
}
return ret
})
}

function patchGenerator (generator) {
if (typeof generator.request !== 'function') {
log.patching('hapi.generator.request not a function')
log.patching(notFunction, 'generator.request')
return
}
shimmer.wrap(generator, 'request', fn => function () {
Expand All @@ -157,7 +170,7 @@ function patchGenerator (generator) {

function patchRequest (request) {
if (typeof request._execute !== 'function') {
log.patching('hap.request._execute not a function')
log.patching(notFunction, 'request._execute')
return
}

Expand All @@ -184,13 +197,13 @@ function patchRequest (request) {

function patchDecorator (plugin, version) {
if (typeof plugin.decorate !== 'function') {
log.patching('hapi.plugin.decorator not a function')
log.patching(notFunction, 'plugin.decorator')
return
}

function wrapViews (views) {
if (typeof views !== 'function') {
log.patching('hapi.plugin.decorator.views not a function')
log.patching(notFunction, 'plugin.decorator.views')
return views
}
return function () {
Expand All @@ -210,11 +223,11 @@ function patchDecorator (plugin, version) {
}
}

shimmer.wrap(plugin, 'decorate', fn => function (name, method, handler) {
shimmer.wrap(plugin, 'decorate', fn => function (name, method, handler, options) {
if (name === 'server' && method === 'views') {
handler = wrapViews(handler)
}
return fn.call(this, name, method, handler)
return fn.call(this, name, method, handler, options)
})
}

Expand All @@ -235,7 +248,7 @@ module.exports = function (hapi) {
patchRequest(Request.prototype)
}
} else {
log.patching('hapi/Request.prototype not found')
log.patching(protoNotFound, 'Request')
}

// After 8.0.0, the Plugin system was introduced
Expand All @@ -247,7 +260,7 @@ module.exports = function (hapi) {
if (Plugin && Plugin.prototype) {
patchDecorator(Plugin.prototype, version)
} else {
log.patching('hapi/Plugin.prototype not found')
log.patching(protoNotFound, 'Plugin')
}

// After 7.2.0, Server became Connection
Expand All @@ -259,7 +272,7 @@ module.exports = function (hapi) {
if (Connection && Connection.prototype) {
patchConnection(Connection.prototype)
} else {
log.patching('hapi/Connection.prototype not found')
log.patching(protoNotFound, 'Connection')
}

// After 2.0.0, View was not patchable directly
Expand All @@ -271,7 +284,7 @@ module.exports = function (hapi) {
if (Server && Server.prototype) {
patchConnection(Server.prototype)
} else {
log.patching('hapi/Server.prototype not found')
log.patching(protoNotFound, 'Server')
}

// Beyond that, we can patch View directly
Expand All @@ -293,7 +306,7 @@ module.exports = function (hapi) {
}
}
if (!patched) {
log.patching('hapi/views not patched')
log.patching('hapi - views not patched')
}
}

Expand Down

0 comments on commit b2f830e

Please sign in to comment.