From ccbffdb2afc67f8ba358d79346875705d44bc20e Mon Sep 17 00:00:00 2001 From: Sarah Chen Date: Wed, 18 Dec 2024 11:31:41 -0600 Subject: [PATCH] Update Apache getRequestHeader and getResponseHeader --- .../ApacheHttpAsyncClientDecorator.java | 26 ++++++++++++++----- .../ApacheHttpClientDecorator.java | 26 ++++++++++++++----- .../ApacheHttpClientDecorator.java | 26 ++++++++++++++----- 3 files changed, 60 insertions(+), 18 deletions(-) diff --git a/dd-java-agent/instrumentation/apache-httpasyncclient-4/src/main/java/datadog/trace/instrumentation/apachehttpasyncclient/ApacheHttpAsyncClientDecorator.java b/dd-java-agent/instrumentation/apache-httpasyncclient-4/src/main/java/datadog/trace/instrumentation/apachehttpasyncclient/ApacheHttpAsyncClientDecorator.java index e8faa71340d..d9fd30b6a42 100644 --- a/dd-java-agent/instrumentation/apache-httpasyncclient-4/src/main/java/datadog/trace/instrumentation/apachehttpasyncclient/ApacheHttpAsyncClientDecorator.java +++ b/dd-java-agent/instrumentation/apache-httpasyncclient-4/src/main/java/datadog/trace/instrumentation/apachehttpasyncclient/ApacheHttpAsyncClientDecorator.java @@ -60,9 +60,16 @@ protected int status(final HttpContext context) { @Override protected String getRequestHeader(HttpUriRequest request, String headerName) { - Header header = request.getFirstHeader(headerName); - if (header != null) { - return header.getValue(); + Header[] headers = request.getHeaders(headerName); + if (headers.length > 0) { + StringBuilder result = new StringBuilder(); + for (int i = 0; i < headers.length; i++) { + result.append(headers[i].getValue()); + if (i + 1 < headers.length) { + result.append(","); + } + } + return result.toString(); } return null; } @@ -71,9 +78,16 @@ protected String getRequestHeader(HttpUriRequest request, String headerName) { protected String getResponseHeader(HttpContext context, String headerName) { final Object responseObject = context.getAttribute(HttpCoreContext.HTTP_RESPONSE); if (responseObject instanceof HttpResponse) { - Header header = ((HttpResponse) responseObject).getFirstHeader(headerName); - if (header != null) { - return header.getValue(); + Header[] headers = ((HttpResponse) responseObject).getHeaders(headerName); + if (headers.length > 0) { + StringBuilder result = new StringBuilder(); + for (int i = 0; i < headers.length; i++) { + result.append(headers[i].getValue()); + if (i + 1 < headers.length) { + result.append(","); + } + } + return result.toString(); } } return null; diff --git a/dd-java-agent/instrumentation/apache-httpclient-4/src/main/java/datadog/trace/instrumentation/apachehttpclient/ApacheHttpClientDecorator.java b/dd-java-agent/instrumentation/apache-httpclient-4/src/main/java/datadog/trace/instrumentation/apachehttpclient/ApacheHttpClientDecorator.java index 91cee1f3f85..20bfcdfcddd 100644 --- a/dd-java-agent/instrumentation/apache-httpclient-4/src/main/java/datadog/trace/instrumentation/apachehttpclient/ApacheHttpClientDecorator.java +++ b/dd-java-agent/instrumentation/apache-httpclient-4/src/main/java/datadog/trace/instrumentation/apachehttpclient/ApacheHttpClientDecorator.java @@ -46,18 +46,32 @@ protected int status(final HttpResponse httpResponse) { @Override protected String getRequestHeader(HttpUriRequest request, String headerName) { - Header header = request.getFirstHeader(headerName); - if (null != header) { - return header.getValue(); + Header[] headers = request.getHeaders(headerName); + if (headers.length > 0) { + StringBuilder result = new StringBuilder(); + for (int i = 0; i < headers.length; i++) { + result.append(headers[i].getValue()); + if (i + 1 < headers.length) { + result.append(","); + } + } + return result.toString(); } return null; } @Override protected String getResponseHeader(HttpResponse response, String headerName) { - Header header = response.getFirstHeader(headerName); - if (null != header) { - return header.getValue(); + Header[] headers = response.getHeaders(headerName); + if (headers.length > 0) { + StringBuilder result = new StringBuilder(); + for (int i = 0; i < headers.length; i++) { + result.append(headers[i].getValue()); + if (i + 1 < headers.length) { + result.append(","); + } + } + return result.toString(); } return null; } diff --git a/dd-java-agent/instrumentation/apache-httpclient-5/src/main/java/datadog/trace/instrumentation/apachehttpclient5/ApacheHttpClientDecorator.java b/dd-java-agent/instrumentation/apache-httpclient-5/src/main/java/datadog/trace/instrumentation/apachehttpclient5/ApacheHttpClientDecorator.java index 78d7958d6f6..1fd29d79528 100644 --- a/dd-java-agent/instrumentation/apache-httpclient-5/src/main/java/datadog/trace/instrumentation/apachehttpclient5/ApacheHttpClientDecorator.java +++ b/dd-java-agent/instrumentation/apache-httpclient-5/src/main/java/datadog/trace/instrumentation/apachehttpclient5/ApacheHttpClientDecorator.java @@ -54,18 +54,32 @@ protected int status(final HttpResponse httpResponse) { @Override protected String getRequestHeader(HttpRequest request, String headerName) { - Header header = request.getFirstHeader(headerName); - if (null != header) { - return header.getValue(); + Header[] headers = request.getHeaders(headerName); + if (headers.length > 0) { + StringBuilder result = new StringBuilder(); + for (int i = 0; i < headers.length; i++) { + result.append(headers[i].getValue()); + if (i + 1 < headers.length) { + result.append(","); + } + } + return result.toString(); } return null; } @Override protected String getResponseHeader(HttpResponse response, String headerName) { - Header header = response.getFirstHeader(headerName); - if (null != header) { - return header.getValue(); + Header[] headers = response.getHeaders(headerName); + if (headers.length > 0) { + StringBuilder result = new StringBuilder(); + for (int i = 0; i < headers.length; i++) { + result.append(headers[i].getValue()); + if (i + 1 < headers.length) { + result.append(","); + } + } + return result.toString(); } return null; }