# Java apm agent doesn't capture text or json body

**URL:** <https://discuss.elastic.co/t/java-apm-agent-doesnt-capture-text-or-json-body/324064>\
**Category:** APM\
**Tags:** java\
**Created:** [January 26, 2023, 7:12pm UTC](https://discuss.elastic.co/t/java-apm-agent-doesnt-capture-text-or-json-body/324064 "2023-01-26T19:12:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrodal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mrodal/32/116446_2.png) [@mrodal](https://discuss.elastic.co/u/mrodal)\
**Post date:** [January 26, 2023, 7:12pm UTC](https://discuss.elastic.co/t/java-apm-agent-doesnt-capture-text-or-json-body/324064/1 "2023-01-26T19:12:02Z")

</div>

**Kibana version** : 8.6

**Elasticsearch version** :8.6

**APM Server version** : 8.6

**APM Agent language and version** : Java 1.35.0

**Browser version** : Chrome

**Original install method (e.g. download page, yum, deb, from source, etc.) and version**: Download page

**Fresh install or upgraded from other version?** : Fresh

**Is there anything special in your setup?** : Spring boot 2.7.4 application

**Description of the problem including expected versus actual behavior. Please include screenshots (if relevant)**:  
Apm agent doesnt register plain text request body. It does register application/x-www-form-urlencoded.  
Using trace logging I can see that the information that the agent sends doesnt include the body.  
Debugging with the agent I cant find where the bodyBuffer is assigned with the request data

**Steps to reproduce** :

1. Set java option elastic.apm.capture\_body=all
2. Make a POST request with content-type: text/plain

Could anyone refer me to where in the code is the apm request body copied from the servlet request body?

---

<div class="post-metadata">

**Author:** ![Jonas\_Kunz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonas_kunz/32/110740_2.png) [@Jonas\_Kunz](https://discuss.elastic.co/u/Jonas_Kunz)\
**Post date:** [January 27, 2023, 1:58pm UTC](https://discuss.elastic.co/t/java-apm-agent-doesnt-capture-text-or-json-body/324064/2 "2023-01-27T13:58:56Z")

</div>

Hi @mrodal ,

Could you please provide us [Debug Logs](https://www.elastic.co/guide/en/apm/agent/java/current/trouble-shooting.html#trouble-shooting-logging) (via [GH gists](https://gist.github.com/)) of a run from your application which includes both the startup of the application as well as a POST request with content-type text/plain. The debug logs should usually report why a body is not being captured.

---

<div class="post-metadata">

**Author:** ![mrodal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mrodal/32/116446_2.png) [@mrodal](https://discuss.elastic.co/u/mrodal)\
**Post date:** [January 27, 2023, 3:34pm UTC](https://discuss.elastic.co/t/java-apm-agent-doesnt-capture-text-or-json-body/324064/3 "2023-01-27T15:34:16Z")

</div>

Hi @Jonas_Kunz ,  
Heres a trace log so it captures the serialized information sent to the apm agent:

[https://gist.githubusercontent.com/mrodal/7acff287bc31bb592df437d513b388e2/raw/eb82498c17046452ff15ee92ed34f0ad615cdbe0](https://gist.githubusercontent.com/mrodal/7acff287bc31bb592df437d513b388e2/raw/eb82498c17046452ff15ee92ed34f0ad615cdbe0)

If you search for "123456" you can find where the transaction begins and searching for "text/plain" you can find the serialized data

Please let me know if I can provide any more useful information

---

<div class="post-metadata">

**Author:** ![Jonas\_Kunz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonas_kunz/32/110740_2.png) [@Jonas\_Kunz](https://discuss.elastic.co/u/Jonas_Kunz)\
**Post date:** [January 30, 2023, 11:12am UTC](https://discuss.elastic.co/t/java-apm-agent-doesnt-capture-text-or-json-body/324064/4 "2023-01-30T11:12:13Z")

</div>

Ok, the logs do not indicate any reason why the agent is actively not capturing the body.

Does your application consume the incoming request body? E.g. does your spring controller have something like a `@RequestBody String content` parameter?

The agent decides on whether or not to capture the body [here](https://github.com/elastic/apm-agent-java/blob/73b62624cfa4e095b1a6241ac11bac046dc096f1/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/ServletTransactionHelper.java#L126). Based on that decision the `ServletRequest`'s `getInputStream` is wrapped [here](https://github.com/elastic/apm-agent-java/blob/cb870b072523f83c56669ba8f4cabc3b48f79c44/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/JavaxRequestStreamRecordingInstrumentation.java#L60) with a stream which copies the data to the transaction. So if your application does not consume the input stream, no data will be recorded on the transaction.

If you are already debugging yourself with the agent source code, these two code locations might allow you to locate the issue.

---

<div class="post-metadata">

**Author:** ![mrodal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mrodal/32/116446_2.png) [@mrodal](https://discuss.elastic.co/u/mrodal)\
**Post date:** [January 30, 2023, 12:00pm UTC](https://discuss.elastic.co/t/java-apm-agent-doesnt-capture-text-or-json-body/324064/5 "2023-01-30T12:00:50Z")

</div>

To my knowledge, the application doesnt consume the incoming request body. Actually the controller method was a GET and I changed it to a POST to test this.

The first line you mention (`request.withBodyBuffer()`) gets hit. For the second one, I cant get the debugger to stop there. Where is `afterGetInputStream` supposed to get called from?

Following the code from `onEnterServletService` and `onExitServletService` [here](https://github.com/elastic/apm-agent-java/blob/main/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/JavaxServletApiAdvice.java) I cant find where afterGetInputStream gets called

Thanks

---

<div class="post-metadata">

**Author:** ![Jonas\_Kunz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonas_kunz/32/110740_2.png) [@Jonas\_Kunz](https://discuss.elastic.co/u/Jonas_Kunz)\
**Post date:** [January 30, 2023, 12:20pm UTC](https://discuss.elastic.co/t/java-apm-agent-doesnt-capture-text-or-json-body/324064/6 "2023-01-30T12:20:15Z")

</div>

`afterGetInputStream` is invoked whenever any implementation of [ServletRequest.getInputStream](https://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getInputStream()) is called. This very likely never happens because your application doesn't consume the request body. Making your application consume the body should hopefully fix the issue for you.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 20, 2023, 8:21am UTC](https://discuss.elastic.co/t/java-apm-agent-doesnt-capture-text-or-json-body/324064/7 "2023-02-20T08:21:06Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
