Skip to content

Commit 0d9da33

Browse files
xxzefghtrentm
andcommitted
feat(fastify): add captureBody support (#2681)
Closes: #1906 Closes: #3217 Co-authored-by: Trent Mick <[email protected]>
1 parent 19f7df9 commit 0d9da33

File tree

5 files changed

+90
-5
lines changed

5 files changed

+90
-5
lines changed

.tav.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ fastify-v1:
476476
- node test/instrumentation/modules/fastify/fastify.test.js
477477
- node test/instrumentation/modules/fastify/async-await.test.js
478478
- node test/instrumentation/modules/fastify/set-framework.test.js
479+
- node test/sanitize-field-names/fastify.test.js
479480
fastify-v2:
480481
name: fastify
481482
versions: '>=2.0.0 <2.4.0 || >2.4.0 <3'
@@ -484,6 +485,7 @@ fastify-v2:
484485
- node test/instrumentation/modules/fastify/fastify.test.js
485486
- node test/instrumentation/modules/fastify/async-await.test.js
486487
- node test/instrumentation/modules/fastify/set-framework.test.js
488+
- node test/sanitize-field-names/fastify.test.js
487489
fastify-v3:
488490
name: fastify
489491
versions: '>=3.0.0 <4'
@@ -492,6 +494,7 @@ fastify-v3:
492494
- node test/instrumentation/modules/fastify/fastify.test.js
493495
- node test/instrumentation/modules/fastify/async-await.test.js
494496
- node test/instrumentation/modules/fastify/set-framework.test.js
497+
- node test/sanitize-field-names/fastify.test.js
495498
fastify:
496499
name: fastify
497500
versions: '>=4 <4.0.1 || >4.0.1 <4.16.0 || >4.16.2'
@@ -500,6 +503,7 @@ fastify:
500503
- node test/instrumentation/modules/fastify/fastify.test.js
501504
- node test/instrumentation/modules/fastify/async-await.test.js
502505
- node test/instrumentation/modules/fastify/set-framework.test.js
506+
- node test/sanitize-field-names/fastify.test.js
503507

504508
finalhandler:
505509
versions: '*'

CHANGELOG.asciidoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Notes:
4040
[float]
4141
===== Features
4242
43+
* Add <<capture-body>> support for Fastify instrumentation.
44+
Contributed by @xxzefgh. ({pull}2681[#2681])
45+
4346
* Add support for mysql2@3. Contributed by @firecow. ({pull}3301[#3301])
4447
4548
* Improve error handling with AWS Lambda. When used together with the

lib/instrumentation/modules/fastify.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ module.exports = function (fastify, agent, { version, enabled }) {
4040
next()
4141
})
4242

43+
agent.logger.debug('adding preHandler hook to fastify')
44+
_fastify.addHook('preHandler', (req, reply, next) => {
45+
// Save the parsed req body to be picked up by getContextFromRequest().
46+
req.raw.body = req.body
47+
next()
48+
})
49+
4350
agent.logger.debug('adding onError hook to fastify')
4451
_fastify.addHook('onError', (req, reply, err, next) => {
4552
agent.captureError(err, { request: req.raw })
@@ -60,6 +67,13 @@ module.exports = function (fastify, agent, { version, enabled }) {
6067
next()
6168
})
6269

70+
agent.logger.debug('adding preHandler hook to fastify')
71+
_fastify.addHook('preHandler', (req, reply, next) => {
72+
// Save the parsed req body to be picked up by getContextFromRequest().
73+
req.raw.body = req.body
74+
next()
75+
})
76+
6377
agent.logger.debug('adding onError hook to fastify')
6478
_fastify.addHook('onError', (req, reply, err, next) => {
6579
agent.captureError(err, { request: req.raw })
@@ -80,6 +94,13 @@ module.exports = function (fastify, agent, { version, enabled }) {
8094
next()
8195
})
8296

97+
agent.logger.debug('adding preHandler hook to fastify')
98+
_fastify.addHook('preHandler', (req, reply, next) => {
99+
// Save the parsed req body to be picked up by getContextFromRequest().
100+
req.raw.body = req.body
101+
next()
102+
})
103+
83104
agent.logger.warn('Elastic APM cannot automaticaly capture errors on this verison of Fastify. Upgrade to version 2.0.0 or later.')
84105

85106
return _fastify

test/instrumentation/modules/fastify/fastify.test.js

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ if (process.env.GITHUB_ACTIONS === 'true' && process.platform === 'win32') {
1212
}
1313

1414
const agent = require('../../../..').start({
15+
serviceName: 'test-fastify',
1516
captureExceptions: false,
16-
metricsInterval: 0,
17-
centralConfig: false
17+
metricsInterval: '0s',
18+
centralConfig: false,
19+
apmServerVersion: '8.7.0',
20+
captureBody: 'all'
1821
})
1922

2023
const isFastifyIncompat = require('../../../_is_fastify_incompat')()
@@ -69,6 +72,61 @@ test('transaction name', function (t) {
6972
})
7073
})
7174

75+
test('captureBody', function (t) {
76+
t.plan(9)
77+
78+
const postData = JSON.stringify({ foo: 'bar' })
79+
80+
resetAgent(data => {
81+
t.strictEqual(data.transactions.length, 1)
82+
var trans = data.transactions[0]
83+
t.strictEqual(trans.name, 'POST /postSomeData', 'transaction.name')
84+
t.strictEqual(trans.type, 'request', 'transaction.type')
85+
t.strictEqual(trans.result, 'HTTP 2xx', 'transaction.result')
86+
t.strictEqual(trans.context.request.method, 'POST', 'transaction.context.request.method')
87+
t.equal(trans.context.request.body, postData, 'transaction.context.request.body')
88+
fastify.close()
89+
})
90+
91+
var fastify = Fastify()
92+
93+
fastify.post('/postSomeData', (request, reply) => {
94+
reply.send('your data has been posted')
95+
})
96+
97+
fastify.listen({ port: 0 }, function (err) {
98+
t.error(err)
99+
100+
// build the URL manually as older versions of fastify doesn't supply it as
101+
// an argument to the callback
102+
const port = fastify.server.address().port
103+
const cReq = http.request(
104+
'http://localhost:' + port + '/postSomeData',
105+
{
106+
method: 'POST',
107+
hostname: 'localhost',
108+
port,
109+
headers: {
110+
'Content-Type': 'application/json',
111+
'Content-Length': Buffer.byteLength(postData)
112+
}
113+
},
114+
function (res) {
115+
t.strictEqual(res.statusCode, 200)
116+
res.on('data', function (chunk) {
117+
t.strictEqual(chunk.toString(), 'your data has been posted')
118+
})
119+
res.on('end', function () {
120+
agent.flush()
121+
t.end()
122+
})
123+
}
124+
)
125+
cReq.write(postData)
126+
cReq.end()
127+
})
128+
})
129+
72130
function resetAgent (cb) {
73131
agent._instrumentation.testReset()
74132
agent._transport = mockClient(1, cb)

test/sanitize-field-names/fastify.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ if (isFastifyIncompat) {
1616

1717
const {
1818
resetAgent,
19+
assertFormsWithFixture,
1920
assertRequestHeadersWithFixture,
2021
assertResponseHeadersWithFixture
2122
} = require('./_shared')
@@ -40,9 +41,7 @@ function runTest (
4041
const transaction = data.transactions.pop()
4142
assertRequestHeadersWithFixture(transaction, expected, t)
4243
assertResponseHeadersWithFixture(transaction, expected, t)
43-
// TODO: uncomment once we fix
44-
// https://github.com/elastic/apm-agent-nodejs/issues/1906
45-
// assertFormsWithFixture(transaction, expected, t)
44+
assertFormsWithFixture(transaction, expected, t)
4645
})
4746

4847
// register request handler

0 commit comments

Comments
 (0)