-
-
Notifications
You must be signed in to change notification settings - Fork 444
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
HTTP request body not being reported for application/json encodings #331
Comments
We discussed this previously here: #223 (comment) The fact that |
This also comes up some here: #273 |
Yes, there are tons of stackoverflow's asking for workarounds to the fact that an HttpServletRequest's input can be read only once. Someone over at sun once long ago probably had a good reason for it, but today as soon as you add a bunch of logging modules upstream from something like jackson that converts your json into java, it becomes a major pia. My solution has been to write a request filter that replaces the HttpServletRequest with a custom HttpServletRequestWrapper which caches the body and lets With that caching filter in place, workaround is I just dump the body into the MDC where sentry auto picks it up. An API like in #273 would let me directly inject it. I think sentry automatically replacing the HttpServletRequest with a request cache wrapper is kinda dangerous (lots of concerns around out-of-scope of sentry, eg request size limits, what if someone is already doing their own cache, etc.), but given these limitation of j2ee, I think it would be awesome if you guys at least provided a default implementation that acts as an out-of-the-box opt-in-if-you-accept-the-risks. #273's API would help a lot, as well as updating the docs to make it clear that only form-encoded request payloads are included by default (to include application/json, opt-in to this this silly request-caching filter kludge). |
A |
If this were to be done automagically by Sentry, that would be great. |
@bretthoerner Can you please provide some example how to set request body manual? Can I define it in ServletContextInitializer as a Spring bean? Or I have to set body every time error happen? |
Is it fixed completely? i can't see body data on sentry error event now with |
Is this fixed or is there any workaround if I am using spring-sentry? |
I hava fixed this issue using spring-boot
step2:
|
For reference what the option looks like in other SDKs: |
btw logging the whole response body might drop the events by the server - size limits (aka 413), so I'd not recommend that, unless the SDK supports this config |
I started using sentry recently and I'm very interested in this feature. Here are my two cents: As for the "The fact that getReader() can only be called once seemingly kills any chance for Sentry to do anything automatically", what about caching the body request with a simple cache filter as explained here https://www.baeldung.com/spring-reading-httpservletrequest-multiple-times ? Since I think caching has already been mentioned before I'm not sure what are exactly the concerns that are preventing this feature to be implemented, so could you please elaborate on that? For now I'll try to address some of the concerns I've read of. I'm thinking about having two options:
What do you think? In the meantime, if I already have a cache filter, Is there anything I can do, as a workaround, to send body to sentry? |
ASP.NET Core has the same issue where the body is a stream and it's read once and data is gone. So in order to get this feature to work, if opt-in, we do wrap the stream with a memory stream so the payload stays around in case of an error. This obviously can affect the overall application memory footprint since now data can hang around for longer even if not needed (i.e Used to stream into a deserializer and create objects, now both objects plus original binary blob are alive for the duration of the request).
This feature exists in other SDKs and is configured like this: An option named The values are:
If opting in means the request stream need to be buffered, that should be transparent. Docs can mention this caveat but it's fair to assume that if you're looking to get the request body included in errors, you're not running a server that's about to tip over and you can afford having the payload buffered.
I believe others such as @maciejwalkowiak can help better here. This talk is high in priority though so hopefully we can see this happening in the upcoming weeks. |
That's great news @bruno-garcia, can't wait for that!! Just a few more suggestions, hopefully they'll be useful:
Also, don't know if that's possible, but it would also be great if through some configuration, or maybe annotations, we could enable/disable body logging on specific endpoints, therefore having more control on where it makes sense to log or not body requests. |
using Spring capabilities is fine and makes total sense, but let's also be sure that the core impl. is also expandable for mobile usage (eg Android uses the |
This is historically a server side feature. For example, the core dotnet sdk doesn't have this feature. Only aspnet and aspnet core. How do you see us using this from android? |
I thought we could use this feature to populate the request's payload or response's body when doing HTTP calls and raising events as a client |
This was added on Spring MVC through #1595 |
If your HTTP request contains a json-encoded body (eg POST requests on modern API's), the body doesn't show up in sentry.
The lib extracts the body using ServletRequest.getParameterMap(): https://github.com/getsentry/raven-java/blob/v7.8.3/raven/src/main/java/com/getsentry/raven/event/interfaces/HttpInterface.java#L55
This works fine for form url encodings but returns nothing for JSON encodings. eg http://stackoverflow.com/questions/3831680/httpservletrequest-get-json-post-data
The simplest way to get the raw body text is something like
servletRequest.getReader().lines().collect(Collectors.joining())
(but getReader() has limitations that it can only be called once, so sentry will have to be clever not to hurt any downstream code). Ideally this could be sent up to sentry.The text was updated successfully, but these errors were encountered: