Skip to content

Commit 76d1b12

Browse files
brad-gardnerQard
authored andcommitted
chore: make hapi instrumentation support @hapi/hapi rename
1 parent cc0188a commit 76d1b12

File tree

11 files changed

+732
-650
lines changed

11 files changed

+732
-650
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
@@ -100,7 +100,7 @@ jobs:
100100
-
101101
node_js: '12'
102102
if: type IN (cron, pull_request) AND NOT branch =~ ^greenkeeper/.*
103-
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,express,express-queue
103+
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,@hapi/hapi,express,express-queue
104104
script: tav --quiet
105105
-
106106
node_js: '12'
@@ -127,7 +127,7 @@ jobs:
127127
-
128128
node_js: '11'
129129
if: type IN (cron, pull_request) AND NOT branch =~ ^greenkeeper/.*
130-
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,express,express-queue
130+
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,@hapi/hapi,express,express-queue
131131
script: tav --quiet
132132
-
133133
node_js: '11'
@@ -154,7 +154,7 @@ jobs:
154154
-
155155
node_js: '10'
156156
if: type IN (cron, pull_request) AND NOT branch =~ ^greenkeeper/.*
157-
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,express,express-queue
157+
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,@hapi/hapi,express,express-queue
158158
script: tav --quiet
159159
-
160160
node_js: '10'
@@ -181,7 +181,7 @@ jobs:
181181
-
182182
node_js: '8'
183183
if: type IN (cron, pull_request) AND NOT branch =~ ^greenkeeper/.*
184-
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,express,express-queue
184+
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,@hapi/hapi,express,express-queue
185185
script: tav --quiet
186186
-
187187
node_js: '8'
@@ -208,7 +208,7 @@ jobs:
208208
-
209209
node_js: '6'
210210
if: type IN (cron, pull_request) AND NOT branch =~ ^greenkeeper/.*
211-
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,express,express-queue
211+
env: TAV=ws,graphql,express-graphql,elasticsearch,hapi,@hapi/hapi,express,express-queue
212212
script: tav --quiet
213213
-
214214
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 or list of strings}+
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/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: 2 additions & 1 deletion
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",
@@ -186,6 +187,6 @@
186187
},
187188
"coordinates": [
188189
55.778264,
189-
12.593202
190+
12.59313
190191
]
191192
}

test/config.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,9 @@ test('disableInstrumentations', function (t) {
640640
if (semver.lt(process.version, '8.9.0') && semver.gte(hapiVersion, '17.0.0')) {
641641
modules.delete('hapi')
642642
}
643+
if (semver.lt(process.version, '8.9.0')) {
644+
modules.delete('@hapi/hapi')
645+
}
643646
if (semver.lt(process.version, '6.0.0') && semver.gte(mysql2Version, '1.6.0')) {
644647
modules.delete('mysql2')
645648
}
@@ -671,7 +674,13 @@ test('disableInstrumentations', function (t) {
671674
}
672675

673676
for (const mod of modules) {
674-
require(mod)
677+
if (Array.isArray(mod)) {
678+
for (const subMod of mod) {
679+
require(subMod)
680+
}
681+
} else {
682+
require(mod)
683+
}
675684
}
676685

677686
t.deepEqual(selectionSet, found, 'disabled all selected modules')
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)