Closed cuiweiqiang closed 6 years ago
Oh, I can see our docs are not clear about how this works at all. Sorry about that.
We currently don't capture the body unless it's available on the http.IncomingMessage object. This is normally made available by frameworks like Express, but there's no official standard for this and Node.js doesn't do this natively.
Currently we look for the body on either req.json
, req.body
or req.payload
.
If you put your body onto one of these 3 properties, the Node.js agent will pick it up. Would that be an acceptable solution for you?
I've opened an issue to make sure we update the docs to be more clear about this: #408
Hmmmm, I use ugly code inject to the http-shared.js
. The package raw-body
provide a method which to parse raw body from IncomingMessage.
Because of my project provide restful api, so it pass params by json, the hacked code work well.
But this is very ungraced solution. Is there any way to delay execute hook function or hook ctx in koa ?
If you use raw-body
as a middleware in Koa, do you store the body on a property on the request object and in that case which one? Is it req.text
as shown in their example code?
I'm not sure what you mean by "Is there any way to delay execute hook function or hook ctx in koa ?" - which hook are you referring to?
Thanks for reply.
I hacked this file : /node_modules/elastic-apm-node/lib/instrumentation/http-shared.js
, and add this code in line 22:
When apm agent start, the instrumentation will start too, I find a piece of code in Instrumentation.prototype.start
method. In this method, the _patchModule
method is executed.
My project use koa-bodyparser
to parse the request body, then ctx object is be injectd like ctx.request.body
. Can we read body from here?
Thanks for sharing the details 😃
Yes, I think it would make sense for us to look into koa-bodyparser and see if we can add support for ti directly.
That being said, I think there's a non-hacky way for you to expose the body to the Elastic APM agent. How about adding this middleware after the bodyParser:
app.use(async ctx => {
ctx.req.body = ctx.request.body
})
If I'm not mistaken you'll always have access to the raw request via ctx.req
, and the koa-bodyparser
middelware will store the parsed body on ctx.request.body
, so if you just copy it over like this, I think it should have the same effect.
This is still not ideal, but better than manually patching http-shared.js
I think.
FYI: I've created a new issue to look into native support of koa-bodyparser
: #409
@watson Good Job 👍
I'll close this issue for now as the docs have been updated. Feel free to subscribe to #409 if you want to stay up to date on the specific support for koa-bodyparser
😃
This is my package.json dependencies:
and this is my start params:
But when do a request there is no body information in APM UI:
even spans: