-
Notifications
You must be signed in to change notification settings - Fork 229
feat(fastify): add captureBody support #2681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
💚 CLA has been signed |
have not tested this yet, will reopen later |
Tested with |
💔 Build Failed
Expand to view the summary
Build stats
Test stats 🧪
Steps errors
Expand to view the steps failures
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xxzefgh This looks great! Thanks for starting this.
-
Could you add a test for this to "test/instrumentation/modules/fastify/fastify.test.js"?
captureBody
support was recently added for hapi instrumentation. Perhaps you could mostly copy how the equivalent test was added for that PR: https://github.com/elastic/apm-agent-nodejs/pull/2618/files#diff-ce2dc92ded3295986f31e8f4178788ae10a651ec2badfda8b8d92fc1b2d2cd7bR3-R118 If you need help understanding how the test code works there, please ask. I'm happy to help. -
There is also a commented out test in "test/sanitize-field-names/fastify.test.js" that could be added now that you've implemented this. You could apply this diff to enable it:
--- a/test/sanitize-field-names/fastify.test.js
+++ b/test/sanitize-field-names/fastify.test.js
@@ -3,6 +3,7 @@ const { createAgentConfig } = require('./_shared')
const agent = require('../..').start(createAgentConfig())
const {
resetAgent,
+ assertFormsWithFixture,
assertRequestHeadersWithFixture,
assertResponseHeadersWithFixture
} = require('./_shared')
@@ -27,9 +28,7 @@ function runTest (
const transaction = data.transactions.pop()
assertRequestHeadersWithFixture(transaction, expected, t)
assertResponseHeadersWithFixture(transaction, expected, t)
- // TODO: uncomment once we fix
- // https://github.com/elastic/apm-agent-nodejs/issues/1906
- // assertFormsWithFixture(transaction, expected, t)
+ assertFormsWithFixture(transaction, expected, t)
})
// register request handler
- You could add
Fixes: #1906
to your PR description to mark that this issue fixes the already open issue we had to add captureBody support for fastify.
run module tests for fastify |
@trentm thanks for the input, it will take me probably 1 or 2 weeks to make this a proper PR. |
@xxzefgh Okay. If you need help, let me know. You should be able to run that one test file directly to test your changes:
|
Hi @trentm, I've added tests as you requested. One issue is that tests are run against v2 (required in devDependencies), which was already supported. I did locally install v3 and it passed, but I'm not sure how tests can be run against multiple versions. |
@xxzefgh Thanks!
We just recently updated devDeps to fastify@3 in #2738 However, we also use test-all-versions (TAV for short) to test all/most supported versions of modules that we instrument. Lines 475 to 500 in 7e9b9e3
|
Actually we could stand to add Would you be willing to add this patch to your PR? diff --git a/.tav.yml b/.tav.yml
index d24137a2..dde25443 100644
--- a/.tav.yml
+++ b/.tav.yml
@@ -484,6 +484,7 @@ fastify-v1:
- node test/instrumentation/modules/fastify/fastify.test.js
- node test/instrumentation/modules/fastify/async-await.test.js
- node test/instrumentation/modules/fastify/set-framework.test.js
+ - node test/sanitize-field-names/fastify.test.js
fastify-v2:
name: fastify
versions: '>=2.0.0 <2.4.0 || >2.4.0 <3'
@@ -492,6 +493,7 @@ fastify-v2:
- node test/instrumentation/modules/fastify/fastify.test.js
- node test/instrumentation/modules/fastify/async-await.test.js
- node test/instrumentation/modules/fastify/set-framework.test.js
+ - node test/sanitize-field-names/fastify.test.js
fastify:
name: fastify
versions: '>=3.0.0'
@@ -500,6 +502,7 @@ fastify:
- node test/instrumentation/modules/fastify/fastify.test.js
- node test/instrumentation/modules/fastify/async-await.test.js
- node test/instrumentation/modules/fastify/set-framework.test.js
+ - node test/sanitize-field-names/fastify.test.js
finalhandler:
versions: '*' |
Ok, I think I was wrong on v2 comment, as I forgot to comment out my patches for v2 before testing 😐 Rebased and added changes to |
I'm not yet sure what is going on with the |
Ah, yes, I can reproduce locally: this happens when using fastify@3 (now in our package-lock.json file) and node 8.6. Fastify@3 only supports node v10 and later. So, we need to add a guard on top of the relevant fastify test files to just skip the test if the node version and fastify version aren't supported. @xxzefgh Can you please add this patch to your PR: diff --git a/test/instrumentation/modules/fastify/fastify.test.js b/test/instrumentation/modules/fastify/fastify.test.js
index f9f294ab..ae343585 100644
--- a/test/instrumentation/modules/fastify/fastify.test.js
+++ b/test/instrumentation/modules/fastify/fastify.test.js
@@ -8,6 +8,17 @@ const agent = require('../../../..').start({
captureBody: 'all'
})
+const fastifyVer = require('../../../../node_modules/fastify/package.json').version
+const semver = require('semver')
+if (
+ (semver.satisfies(fastifyVer, '1.x') && !semver.satisfies(process.version, '>=6 <12')) ||
+ (semver.satisfies(fastifyVer, '2.x') && !semver.satisfies(process.version, '>=6 <15')) ||
+ (semver.satisfies(fastifyVer, '3.x') && !semver.satisfies(process.version, '>=10'))
+) {
+ console.log(`# SKIP fastify@${fastifyVer} does not support node ${process.version}`)
+ process.exit()
+}
+
const http = require('http')
const Fastify = require('fastify') This reproduces the same version ranges as in the .tav.yml section for fastify. |
It is possible we'll need to add the same guard to the other test files that test fastify, but let's see. We already have a form of that guard in one of those test files: apm-agent-nodejs/test/sanitize-field-names/fastify.test.js Lines 5 to 11 in 7e9b9e3
|
run module tests for fastify |
@elasticmachine, run elasticsearch-ci/docs |
The "test all versions" tests failed on the diff --git a/docs/supported-technologies.asciidoc b/docs/supported-technologies.asciidoc
index b961ccfc..6b0b684c 100644
--- a/docs/supported-technologies.asciidoc
+++ b/docs/supported-technologies.asciidoc
@@ -46,7 +46,7 @@ These are the frameworks that we officially support:
router information for full support. We currently support the most popular Koa router called
https://github.com/alexmingoia/koa-router[koa-router].
|<<restify,Restify>> |>=5.2.0
-|<<fastify,Fastify>> |>=1.0.0; see also https://www.fastify.io/docs/latest/LTS/[Fastify's own LTS documentation]
+|<<fastify,Fastify>> |>=1.0.0; see also https://www.fastify.io/docs/latest/Reference/LTS/[Fastify's own LTS documentation]
|<<lambda,AWS Lambda>> |N/A
|=======================================================================
diff --git a/lib/instrumentation/modules/fastify.js b/lib/instrumentation/modules/fastify.js
index 7646822e..cd757ca7 100644
--- a/lib/instrumentation/modules/fastify.js
+++ b/lib/instrumentation/modules/fastify.js
@@ -61,6 +61,7 @@ module.exports = function (fastify, agent, { version, enabled }) {
})
// Save the parsed req body to be picked up by getContextFromRequest().
+ // (This doesn't work for fastify@1, but that is now EOL.)
_fastify.addHook('preHandler', (req, reply, next) => {
req.raw.body = req.body
next()
diff --git a/test/instrumentation/modules/fastify/fastify.test.js b/test/instrumentation/modules/fastify/fastify.test.js
index ae343585..df05c5bb 100644
--- a/test/instrumentation/modules/fastify/fastify.test.js
+++ b/test/instrumentation/modules/fastify/fastify.test.js
@@ -65,55 +65,58 @@ test('transaction name', function (t) {
})
})
-test('captureBody', function (t) {
- t.plan(9)
-
- const postData = JSON.stringify({ foo: 'bar' })
-
- resetAgent(data => {
- assert(t, data, { name: 'POST /postSomeData', method: 'POST' })
- t.equal(data.transactions[0].context.request.body, postData,
- 'body was captured to trans.context.request.body')
- fastify.close()
- })
-
- var fastify = Fastify()
+// Fastify `captureBody` support is only supported for [email protected] and later.
+if (semver.gte(fastifyVer, '2.0.0')) {
+ test('captureBody', function (t) {
+ t.plan(9)
+
+ const postData = JSON.stringify({ foo: 'bar' })
+
+ resetAgent(data => {
+ assert(t, data, { name: 'POST /postSomeData', method: 'POST' })
+ t.equal(data.transactions[0].context.request.body, postData,
+ 'body was captured to trans.context.request.body')
+ fastify.close()
+ })
- fastify.post('/postSomeData', (request, reply) => {
- reply.send('your data has been posted')
- })
+ var fastify = Fastify()
- fastify.listen(0, function (err) {
- t.error(err)
+ fastify.post('/postSomeData', (request, reply) => {
+ reply.send('your data has been posted')
+ })
- // build the URL manually as older versions of fastify doesn't supply it as
- // an argument to the callback
- const port = fastify.server.address().port
- const cReq = http.request(
- 'http://localhost:' + port + '/postSomeData',
- {
- method: 'POST',
- hostname: 'localhost',
- port,
- headers: {
- 'Content-Type': 'application/json',
- 'Content-Length': Buffer.byteLength(postData)
+ fastify.listen(0, function (err) {
+ t.error(err)
+
+ // build the URL manually as older versions of fastify doesn't supply it as
+ // an argument to the callback
+ const port = fastify.server.address().port
+ const cReq = http.request(
+ 'http://localhost:' + port + '/postSomeData',
+ {
+ method: 'POST',
+ hostname: 'localhost',
+ port,
+ headers: {
+ 'Content-Type': 'application/json',
+ 'Content-Length': Buffer.byteLength(postData)
+ }
+ },
+ function (res) {
+ t.strictEqual(res.statusCode, 200)
+ res.on('data', function (chunk) {
+ t.strictEqual(chunk.toString(), 'your data has been posted')
+ })
+ res.on('end', function () {
+ agent.flush()
+ })
}
- },
- function (res) {
- t.strictEqual(res.statusCode, 200)
- res.on('data', function (chunk) {
- t.strictEqual(chunk.toString(), 'your data has been posted')
- })
- res.on('end', function () {
- agent.flush()
- })
- }
- )
- cReq.write(postData)
- cReq.end()
+ )
+ cReq.write(postData)
+ cReq.end()
+ })
})
-})
+}
function resetAgent (cb) {
agent._instrumentation.testReset() @xxzefgh If you are good adding that to your PR, I can re-run the tests. Thanks for persisting through the slog of testing. |
@elasticmachine, run elasticsearch-ci/docs |
run module tests for fastify |
…aks; avoid APM server error warning by using apmServerVersion
This has languished. I'm adding some small commits to get this over the line. |
@elasticmachine, run elasticsearch-ci/docs |
@elasticmachine, run elasticsearch-ci/docs |
…e will effectively do this
@elasticmachine, run elasticsearch-ci/docs |
…re/support-specific-modules * 'main' of github.com:elastic/apm-agent-nodejs: (54 commits) chore: fix dev-utils/ci-tav-slow-jobs.sh (elastic#3319) test: reduce TAV test matrix for slowest jobs (elastic#3321) chore: sync package-lock so 'npm ci' can work (elastic#3318) docs: document `useElasticTraceparentHeader` config var (elastic#3316) chore, test: test driver improvements (elastic#3293) test: drop node 14 from RC tests now that it is EOL (elastic#3315) test: fix running fastify.test.js with node v8 (elastic#3317) feat: add @apollo/server@4 support (elastic#3203) chore: update nvm (elastic#3309) tests: stop testing 'express-graphql' instrumentation (elastic#3304) chore: fix bitrot.js dev util for recent changes (elastic#3308) test: restore testing of Azure Functions on node >=18.x (elastic#3307) fix: support Lambda instrumentation for `contextManager: 'patch'`; refactor Lambda tests (elastic#3305) test: fix fastify TAV test failures (elastic#3314) test: fix @aws-sdk/client-s3 TAV test failures (elastic#3312) feat: add instrumentation for aws-sdk S3 client (elastic#3287) feat(fastify): add captureBody support (elastic#2681) feat: mysql2@3 support (elastic#3301) chore(deps): bump @opentelemetry/exporter-prometheus from 0.37.0 to 0.38.0 in /test/opentelemetry-metrics/fixtures (elastic#3295) chore(deps-dev): bump fastify from 4.16.3 to 4.17.0 (elastic#3296) ...
Closes: #1906 Closes: #3217 Co-authored-by: Trent Mick <[email protected]>
Closes: elastic#1906 Closes: elastic#3217 Co-authored-by: Trent Mick <[email protected]>
Closes: #1906
Closes: #3217