Home > front end >  Datadog RUM: Is it possible to include HTTP request body in resource logging?
Datadog RUM: Is it possible to include HTTP request body in resource logging?

Time:08-16

I am working on an Android app configured with Datadog RUM. When the device makes an HTTP request during an RUM session I can see it in the Resources section in Datadog. The log contains info about the request including the url, method and query parameters, but no request body. Is it possible to include the request body in the Datadog logging?

I imagine one would normally use APM traces for this, but I'd like to know if it's possible in RUM as well.

CodePudding user response:

Turns out it is possible to log custom resources. It can be done using RumResourceAttributesProvider:

OkHttpClient.Builder builder = new OkHttpClient.Builder()
            .addInterceptor(new DatadogInterceptor((request, span, response, throwable) -> {},
                    new RumResourceAttributesProvider() {
                        @NonNull
                        @Override
                        public Map<String, Object> onProvideAttributes(@NonNull Request request, @Nullable Response response, @Nullable Throwable throwable) {
                            HashMap<String, Object> map = new HashMap<>();
                            String body = null;
                            if(request.body() != null) {
                                try {
                                    final Buffer buffer = new Buffer();
                                    request.body().writeTo(buffer);
                                    body = buffer.readUtf8();
                                }
                                catch (final IOException e) {
                                    body = null;
                                }
                            }
                            if(body != null) {
                                map.put("request_body", body);
                            }
                            return map;
                        }
                    }
            ));
  • Related