Capture the body of outgoing (client) http requests

Hi

referencing to APM Java agent - capture body backend request - Apache HttpClient

this has become a big deal for us if we can somehow capture body of outgoing http requests

we have so many applications which are depending on back-end systems and we would need this option to see what is going on

example of such back-end systems is Tibco/ESB systems which are now talking over Rest

so please if there is any way of including such support in some of next versions..

we would be able to do monitoring of all systems this way

Thanks in advance
tomislav

You could do this by adding a custom HttpRequestInterceptor:

HttpClients.custom()
    .addInterceptorFirst(new HttpRequestInterceptor() {
    @Override
    public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
        if (request instanceof HttpEntityEnclosingRequest) {
            Span span = ElasticApm.currentSpan();
            HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
            if (entity.isRepeatable()) {
                span.addLabel("body", new String(entity.getContent().readAllBytes()));
            }
        }
    }
}).build();

Note that tags are limited to 1024 characters and are indexed by default. The Transaction interface has a addCustom(String, String) method which has a higher limit. Adding custom data for spans sounds like a good addition though.

Wow

i am thankful to grave.. :slight_smile:

let me try to do something about this

tomislav

1 Like

Hi
we implemented this and we still dont see anything in APM

this is the code added (you will see comments in code)

import co.elastic.apm.api.ElasticApm;
import co.elastic.apm.api.Span;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.apache.http.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;


httpClientBuilder.addInterceptorFirst(new HttpRequestInterceptor() {
    @Override
    public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
        if (request instanceof HttpEntityEnclosingRequest) {
            Span span = ElasticApm.currentSpan();
            HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
            if (entity.isRepeatable()) {
                LOG.error("body_log: " + EntityUtils.toString(entity)); **//this line logs entity content correctly**
                span.addLabel("body", EntityUtils.toString(entity)); //new String(entity.getContent().readAllBytes()) **won't work, so we used EntityUtils.toString(entity));**
            }
        }
    }
}).build();

do we need to add something as java parameter to capture this, some label?

many thanks
tomislav

Sorry for the late reply. Could you send me a screenshot of such a span in the APM UI? By clicking on it, you should be able to see it's metadata, including labels.

Does adding other types of labels work for you?

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