Skip to content

Commit 76a34a6

Browse files
Qardwatson
authored andcommitted
feat(hapi): support new @hapi/hapi module (#1246)
1 parent 2175914 commit 76a34a6

File tree

12 files changed

+727
-649
lines changed

12 files changed

+727
-649
lines changed

.tav.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,21 @@ hapi-no-async-await:
210210
name: hapi
211211
versions: '>=9.0.1 <17.0.0'
212212
commands:
213-
- node test/instrumentation/modules/hapi/basic.js
213+
- node test/instrumentation/modules/hapi/basic-legacy-path.js
214214
- node test/instrumentation/modules/hapi/set-framework.js
215215
hapi-async-await:
216216
name: hapi
217217
node: '>=8.2'
218218
versions: '>=17.0.0'
219219
commands:
220-
- node test/instrumentation/modules/hapi/basic.js
220+
- node test/instrumentation/modules/hapi/basic-legacy-path.js
221221
- node test/instrumentation/modules/hapi/set-framework.js
222-
222+
'@hapi/hapi':
223+
node: '>=8.2'
224+
versions: '>=17.0.0'
225+
commands:
226+
- node test/instrumentation/modules/hapi/basic.js
227+
- node test/instrumentation/modules/hapi/set-framework-2.js
223228
tedious:
224229
name: tedious
225230
versions: '>=1.9 <2.7 || 3.x || >4.0.0'

.travis.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ jobs:
115115
-
116116
node_js: '12'
117117
if: type IN (cron, pull_request) AND NOT branch =~ ^greenkeeper/.*
118-
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,express,express-queue
118+
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,@hapi/hapi,express,express-queue
119119
script: tav --quiet
120120
-
121121
node_js: '12'
@@ -142,7 +142,7 @@ jobs:
142142
-
143143
node_js: '11'
144144
if: type IN (cron, pull_request) AND NOT branch =~ ^greenkeeper/.*
145-
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,express,express-queue
145+
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,@hapi/hapi,express,express-queue
146146
script: tav --quiet
147147
-
148148
node_js: '11'
@@ -169,7 +169,7 @@ jobs:
169169
-
170170
node_js: '10'
171171
if: type IN (cron, pull_request) AND NOT branch =~ ^greenkeeper/.*
172-
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,express,express-queue
172+
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,@hapi/hapi,express,express-queue
173173
script: tav --quiet
174174
-
175175
node_js: '10'
@@ -196,7 +196,7 @@ jobs:
196196
-
197197
node_js: '8'
198198
if: type IN (cron, pull_request) AND NOT branch =~ ^greenkeeper/.*
199-
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,express,express-queue
199+
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,@hapi/hapi,express,express-queue
200200
script: tav --quiet
201201
-
202202
node_js: '8'
@@ -223,7 +223,7 @@ jobs:
223223
-
224224
node_js: '6'
225225
if: type IN (cron, pull_request) AND NOT branch =~ ^greenkeeper/.*
226-
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,express,express-queue
226+
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,@hapi/hapi,express,express-queue
227227
script: tav --quiet
228228
-
229229
node_js: '6'

docs/agent-api.asciidoc

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,12 +690,12 @@ Optionally, a type may also be provided to group lambdas together. By default,
690690
Read more lambda support in the <<lambda,Lambda>> article.
691691

692692
[[apm-add-patch]]
693-
==== `apm.addPatch(name, handler)`
693+
==== `apm.addPatch(modules, handler)`
694694

695695
[small]#Added in: v2.7.0#
696696

697-
* `name` +{type-string}+
698-
Name of module to apply the patch to, when required.
697+
* `modules` +{type-string}+ | +{type-string-array}+
698+
Name of module(s) to apply the patch to, when required.
699699
* `handler` +{type-function}+ | +{type-string}+
700700
Must be a patch function or a path to a module exporting a patch function
701701
** `exports` +{type-object}+ The original export object of the module
@@ -726,11 +726,26 @@ apm.addPatch('timers', (exports, agent, { version, enabled }) => {
726726
727727
// or ...
728728
729+
apm.addPatch(['hapi', '@hapi/hapi'], (exports, agent, { version, enabled }) => {
730+
const setTimeout = exports.setTimeout
731+
exports.setTimeout = (fn, ms) => {
732+
const span = agent.createSpan('set-timeout')
733+
return setTimeout(() => {
734+
span.end()
735+
fn()
736+
}, ms)
737+
}
738+
739+
return exports
740+
})
741+
742+
// or ...
743+
729744
apm.addPatch('timers', './timer-patch')
730745
----
731746

732747
[[apm-remove-patch]]
733-
==== `apm.removePatch(name, handler)`
748+
==== `apm.removePatch(modules, handler)`
734749

735750
[small]#Added in: v2.7.0#
736751

@@ -744,6 +759,10 @@ apm.removePatch('timers', './timers-patch')
744759
745760
// or ...
746761
762+
apm.removePatch(['timers'], './timers-patch')
763+
764+
// or ...
765+
747766
apm.removePatch('timers', timerPatchFunction)
748767
----
749768

docs/index.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
include::{asciidoc-dir}/../../shared/attributes.asciidoc[]
44

55
:type-string: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type[<string>]
6+
:type-string-array: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type[<string[]>]
67
:type-boolean: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type[<boolean>]
78
:type-object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object[<Object>]
89
:type-number: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type[<number>]

docs/supported-technologies.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ These are the frameworks that we officially support:
4646
|Framework |Version |Note
4747
|<<express,Express>> |^4.0.0 |
4848
|<<hapi,hapi>> |>=9.0.0 <19.0.0 |
49+
|<<hapi,@hapi/hapi>> |>=17.9.0 <19.0.0 |
4950
|<<koa,Koa>> via koa-router |>=5.2.0 <8.0.0 |Koa doesn't have a built in router,
5051
so we can't support Koa directly since we rely on router information for full support.
5152
We currently support the most popular Koa router called https://github.com/alexmingoia/koa-router[koa-router]

lib/instrumentation/index.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var MODULES = [
2323
'generic-pool',
2424
'graphql',
2525
'handlebars',
26-
'hapi',
26+
['hapi', '@hapi/hapi'],
2727
'http',
2828
'https',
2929
'http2',
@@ -70,30 +70,41 @@ function Instrumentation (agent) {
7070
this._patches = new NamedArray()
7171

7272
for (let mod of MODULES) {
73+
if (!Array.isArray(mod)) mod = [mod]
74+
const pathName = mod[0]
75+
7376
this.addPatch(mod, (exports, name, version, enabled) => {
7477
// Lazy require so that we don't have to use `require.resolve` which
7578
// would fail in combination with Webpack. For more info see:
7679
// https://github.com/elastic/apm-agent-nodejs/pull/957
77-
const patch = require(`./modules/${mod}`)
80+
const patch = require(`./modules/${pathName}`)
7881
return patch(exports, name, version, enabled)
7982
})
8083
}
8184
}
8285

83-
Instrumentation.prototype.addPatch = function (name, handler) {
84-
const type = typeof handler
85-
if (type !== 'function' && type !== 'string') {
86-
this._agent.logger.error('Invalid patch handler type:', type)
87-
return
88-
}
86+
Instrumentation.prototype.addPatch = function (modules, handler) {
87+
if (!Array.isArray(modules)) modules = [modules]
8988

90-
this._patches.add(name, handler)
91-
this._startHook()
89+
modules.forEach(mod => {
90+
const type = typeof handler
91+
if (type !== 'function' && type !== 'string') {
92+
this._agent.logger.error('Invalid patch handler type:', type)
93+
return
94+
}
95+
96+
this._patches.add(mod, handler)
97+
this._startHook()
98+
})
9299
}
93100

94-
Instrumentation.prototype.removePatch = function (name, handler) {
95-
this._patches.delete(name, handler)
96-
this._startHook()
101+
Instrumentation.prototype.removePatch = function (modules, handler) {
102+
if (!Array.isArray(modules)) modules = [modules]
103+
104+
modules.forEach(mod => {
105+
this._patches.delete(mod, handler)
106+
this._startHook()
107+
})
97108
}
98109

99110
Instrumentation.prototype.clearPatches = function (name) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
"graphql": "^14.3.1",
131131
"handlebars": "^4.0.12",
132132
"hapi": "^18.1.0",
133+
"@hapi/hapi": "^18.2.0",
133134
"https-pem": "^2.0.0",
134135
"inquirer": "^0.12.0",
135136
"ioredis": "^4.10.0",

test/config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,8 @@ test('disableInstrumentations', function (t) {
630630
var mysql2Version = require('mysql2/package.json').version
631631
var wsVersion = require('ws/package.json').version
632632

633-
var modules = new Set(Instrumentation.modules)
633+
var flattenedModules = Instrumentation.modules.reduce((acc, val) => acc.concat(val), [])
634+
var modules = new Set(flattenedModules)
634635
if (semver.lt(process.version, '8.6.0')) {
635636
modules.delete('restify')
636637
}
@@ -640,6 +641,9 @@ test('disableInstrumentations', function (t) {
640641
if (semver.lt(process.version, '8.9.0') && semver.gte(hapiVersion, '17.0.0')) {
641642
modules.delete('hapi')
642643
}
644+
if (semver.lt(process.version, '8.9.0')) {
645+
modules.delete('@hapi/hapi')
646+
}
643647
if (semver.lt(process.version, '6.0.0') && semver.gte(mysql2Version, '1.6.0')) {
644648
modules.delete('mysql2')
645649
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
require('./shared')('hapi')

0 commit comments

Comments
 (0)