Elastic APM Java agent capture response body for Spring Boot application

I was wondering how I can capture a response body for a Spring Boot application using Elastic APM java agent? Is there any way that I can configure the agent to capture all responses or possibly the response object for a particular endpoint? I can confirm that request body has been captured by using the "transaction" option for APM config, but I cannot find any property for response body.

Not natively supported by a setup, check some links here.
You can attach response body to the transaction manually by calling apm.setCustomContext()
do it before the request have ended.

apm.setCustomContext({
  response: response_body
})
1 Like

Thank you for your help. I'm not sure if I understand what you meant by using setCustomContext(), do you happen to have any examples for Java agent where I can have a look?

Hi @Ali_Nazemian ,
you can capture REQUESTs body only. How to enable this, look into capture_body and capture_body_content_types.
For example, add vm argument: -Delastic.apm.capture_body=ALL when running application
and in http.request.body.original you can view your request body:

.

I think that for performance reasons, the RESPONSE body capturing functionality has not been added.

For capture response body you need add public-api dependency:
And in the required place of your application add custom context to transaction:

ElasticApm.currentTransaction().addCustomContext("custom.context.transaction.response_body", "string representation");
        

And in discovery tab, you can view your RESPONSE body data:
image

1 Like

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