Apm .net capturing outgoing http request body


I'm using the 1.25.0 version of .net apm agent, and I wonder how I can capture the body of the outgoing requests (Post, Patch ...) that are performed within an Apm transaction?

What are the guidelines to do it properly?
Thanks for your help

Hi @Nicolas_Rey

Perhaps take a look at

Be careful there could be sensitive data

Hello @stephenb
I use this parameter for incoming http request, it works but when I use a http client to consume any API, the body is not captured, so for outgoing request I don't know

@Nicolas_Rey Indeed, the CaptureBody configuration applies only to incoming HTTP requests to ASP.NET (Core). The APM Agent does not collect bodies or headers for outgoing HTTP requests, for example, those sent via HttpClient.

One solution would be to log these from your application, attaching the current trace ID as structured data so that you can correlate the logs to the traces.

You can fetch the current trace ID using:

var traceId = Agent.Tracer.CurrentTransaction?.TraceId;

Another solution might be to use the public Tracer API (see docs) to capture any application-specific content such as outgoing request bodies.

This feature has been discussed previously in an issue, and there are no plans to add it by default. The linked issue comment includes an example of a custom diagnostic listener that could be used to capture this.

The same warnings apply regarding the risk of capturing sensitive data and increasing the size of the APM documents stored in Elasticsearch.

Hello @stevejgordon
Thanks for your answer, indeed I plan to capture the body using a handler attached to the current transaction and logging what's I'm interested in within a correlated log.
Most of the time the transaction is already opened, the http call is a part of a bigger piece, so maybe I can span it.
Anyway I'm going to check the sample, thanks a ton for that :pray:


The sample sounds exactly what I need.

Just a question when Greg says in the CustomHttpListener

// Read the HTTP content here and set it accordingly

How can we set a property?
I know how to tag a label from the Api but is it the same thing?

Let's say I want to capture a property called "http.body" with the serialized content of the outgoing body, how am I supposed to set up this content after accessing my http object?

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