Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1648 http response fixes #1775

Merged
merged 25 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f9412b3
Updates to the http-async-client-4 instrumentation to only hold the i…
jbedell-newrelic Feb 14, 2024
e1005f2
Unnecessary code removal
jbedell-newrelic Feb 15, 2024
5f6f6a1
Changes for the httpclient50 instrumentation module
jbedell-newrelic Feb 15, 2024
3310801
Changes for the httpclient-4.0 instrumentation module.
jbedell-newrelic Feb 15, 2024
2f00b7b
Changes for the httpclient31 module
jbedell-newrelic Feb 16, 2024
9dfd934
Changes for the httpclient-jdk11 instrumentation module.
jbedell-newrelic Feb 16, 2024
998fbf1
Changes for the Http4s ember/blaze clients
jbedell-newrelic Feb 20, 2024
66ab337
Changes for HttpURLConnection module
jbedell-newrelic Feb 20, 2024
d1ebcfc
Undo changes to HttpURLConnection
jbedell-newrelic Feb 21, 2024
ed7c6d2
Changes for the Jersey-client-3 module
jbedell-newrelic Feb 21, 2024
0d976cf
Changes for the jms-3 module
jbedell-newrelic Feb 21, 2024
6795856
Changes for the OkHttp44 module
jbedell-newrelic Feb 21, 2024
7b3feff
Change for the spray-can-http-client-1.3.1 module
jbedell-newrelic Feb 21, 2024
15e4dcd
Missed a change in the spray can module
jbedell-newrelic Feb 21, 2024
65ed66c
Changes for the spray-client-1.3.1 module
jbedell-newrelic Feb 21, 2024
853b9c0
Changes for the spring-jms-3 module (identical to the changes for jms…
jbedell-newrelic Feb 22, 2024
f98c43b
Changes for the spring-webclient-6.0 module
jbedell-newrelic Feb 22, 2024
2453dc0
Changes for the sttp-2.13_3.0.0 module
jbedell-newrelic Feb 22, 2024
3ad4b10
Changes for the sttp-2.13_3.0.0 module
jbedell-newrelic Feb 22, 2024
5de8e09
Changes for the sttp-akka-2.13_3.0.0 module
jbedell-newrelic Feb 22, 2024
64e9a59
Changes for the sttp-http4s-2.13_3.0.0 module
jbedell-newrelic Feb 22, 2024
e49eaaa
Changes for the vertx-core-4.5.1 module.
jbedell-newrelic Feb 23, 2024
cbb57d6
Changes for the httpurlconnection module
jbedell-newrelic Feb 27, 2024
55866a7
Merge branch 'main' into 1648_HttpResponseFixes
jbedell-newrelic Feb 29, 2024
54cb21d
Merge branch 'main' into 1648_HttpResponseFixes
jbedell-newrelic Feb 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
import org.apache.http.Header;
import org.apache.http.HttpResponse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* Wraps Apache http async client's inbound response headers for CAT.
*/
public class InboundWrapper extends ExtendedInboundHeaders {
private final HttpResponse response;
private final Header[] headers;

public InboundWrapper(HttpResponse response) {
this.response = response;
this.headers = response == null ? null : response.getAllHeaders();
}

@Override
Expand All @@ -32,25 +33,25 @@ public HeaderType getHeaderType() {

@Override
public String getHeader(String name) {
// getHeaders always returns an array of length >= 0
Header[] headers = response.getHeaders(name);
if (headers.length > 0) {
return headers[0].getValue();
}
return null;
if (headers == null || name == null) return null;

return Arrays.stream(headers)
.filter(h -> name.equals(h.getName()))
.map(h -> h.getValue())
.findFirst()
.orElse(null);
}

@Override
public List<String> getHeaders(String name) {
// getHeaders always returns an array of length >= 0
Header[] headers = response.getHeaders(name);
if (headers.length > 0) {
List<String> result = new ArrayList<>(headers.length);
for (Header header : headers) {
result.add(header.getValue());
}
return result;
}
return null;
if (headers == null || name == null) return null;

List<String> result = Arrays.stream(headers)
.filter(h -> name.equals(h.getName()))
.map(h -> h.getValue())
.collect(Collectors.toList());

return result.isEmpty() ? null : result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,21 @@ public class HttpAsyncResponseConsumer_Instrumentation<T> {
@NewField
private InboundWrapper inboundHeaders;
@NewField
private HttpResponse httpResponse;
private Integer statusCode;
@NewField
private String reasonMessage;

/**
* Invoked when a HTTP response message is received.
*/
public void responseReceived(HttpResponse response) throws IOException, HttpException {
inboundHeaders = new InboundWrapper(response);
httpResponse = response;

if (response != null && response.getStatusLine() != null) {
statusCode = response.getStatusLine().getStatusCode();
reasonMessage = response.getStatusLine().getReasonPhrase();
}

Weaver.callOriginal();
}

Expand All @@ -64,6 +71,8 @@ public void responseCompleted(HttpContext context) {
segment = null;
uri = null;
inboundHeaders = null;
statusCode = null;
reasonMessage = null;
Weaver.callOriginal();
}

Expand All @@ -90,16 +99,10 @@ public void failed(Exception ex) {
}

private Integer getStatusCode() {
if (httpResponse != null && httpResponse.getStatusLine() != null) {
return httpResponse.getStatusLine().getStatusCode();
}
return null;
return statusCode;
}

private String getReasonMessage() {
if (httpResponse != null && httpResponse.getStatusLine() != null) {
return httpResponse.getStatusLine().getReasonPhrase();
}
return null;
return reasonMessage;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.nr.instrumentation.http4s

import com.newrelic.api.agent.{ExtendedInboundHeaders, HeaderType}
import org.http4s.Response
import org.http4s.Headers
import org.typelevel.ci.CIString

import scala.jdk.CollectionConverters._
import java.util

class InboundResponseWrapper[F[_]](response: Response[F]) extends ExtendedInboundHeaders {
class InboundResponseWrapper[F[_]](headers: Headers) extends ExtendedInboundHeaders {
/**
* Return the type of header key syntax used for this.
*
Expand All @@ -25,8 +25,8 @@ class InboundResponseWrapper[F[_]](response: Response[F]) extends ExtendedInboun
* @since 3.5.0
*/
override def getHeader(name: String): String =
response.headers.headers.find(_.name == CIString(name)).map(_.value).orNull
headers.headers.find(_.name == CIString(name)).map(_.value).orNull

override def getHeaders(name: String): util.List[String] =
response.headers.headers.map(_.name.toString).asJava
headers.headers.map(_.name.toString).asJava
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object NewrelicClientMiddleware {
.library("HTTP4S")
.uri(new URI(req.uri.toString()))
.procedure(req.method.toString())
.inboundHeaders(new InboundResponseWrapper(response))
.inboundHeaders(new InboundResponseWrapper(response.headers))
.build())
seg.end()
response
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.nr.instrumentation.http4s

import com.newrelic.api.agent.{ExtendedInboundHeaders, HeaderType}
import org.http4s.Response
import org.http4s.Headers
import org.typelevel.ci.CIString

import scala.jdk.CollectionConverters._
import java.util

class InboundResponseWrapper[F[_]](response: Response[F]) extends ExtendedInboundHeaders {
class InboundResponseWrapper[F[_]](headers: Headers) extends ExtendedInboundHeaders {
/**
* Return the type of header key syntax used for this.
*
Expand All @@ -25,8 +25,8 @@ class InboundResponseWrapper[F[_]](response: Response[F]) extends ExtendedInboun
* @since 3.5.0
*/
override def getHeader(name: String): String =
response.headers.headers.find(_.name == CIString(name)).map(_.value).orNull
headers.headers.find(_.name == CIString(name)).map(_.value).orNull

override def getHeaders(name: String): util.List[String] =
response.headers.headers.map(_.name.toString).asJava
headers.headers.map(_.name.toString).asJava
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object NewrelicClientMiddleware {
.library("HTTP4S")
.uri(new URI(req.uri.toString()))
.procedure(req.method.toString())
.inboundHeaders(new InboundResponseWrapper(response))
.inboundHeaders(new InboundResponseWrapper(response.headers))
.build())
seg.end()
response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,38 @@
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethod;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class InboundWrapper extends ExtendedInboundHeaders {
private final HttpMethod delegate;
private final Header[] headers;

public InboundWrapper(HttpMethod response) {
this.delegate = response;
this.headers = response == null ? null : response.getResponseHeaders();
}

@Override
public String getHeader(String name) {
Header[] headers = delegate.getResponseHeaders(name);
if (headers.length > 0) {
return headers[0].getValue();
}
return null;
if (headers == null || name == null) return null;

return Arrays.stream(headers)
.filter(h -> name.equals(h.getName()))
.map(h -> h.getValue())
.findFirst()
.orElse(null);
}

@Override
public List<String> getHeaders(String name) {
Header[] headers = delegate.getResponseHeaders(name);
if (headers.length > 0) {
List<String> result = new ArrayList<>(headers.length);
for (Header header : headers) {
result.add(header.getValue());
}
return result;
}
return null;
if (headers == null || name == null) return null;

List<String> result = Arrays.stream(headers)
.filter(h -> name.equals(h.getName()))
.map(h -> h.getValue())
.collect(Collectors.toList());

return result.isEmpty() ? null : result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,38 @@
import org.apache.http.Header;
import org.apache.http.HttpResponse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class InboundWrapper extends ExtendedInboundHeaders {
private final HttpResponse delegate;
private final Header[] headers;

public InboundWrapper(HttpResponse response) {
this.delegate = response;
this.headers = response == null ? null : response.getAllHeaders();
}

@Override
public String getHeader(String name) {
Header[] headers = delegate.getHeaders(name);
if (headers.length > 0) {
return headers[0].getValue();
}
return null;
if (headers == null || name == null) return null;

return Arrays.stream(headers)
.filter(h -> name.equals(h.getName()))
.map(h -> h.getValue())
.findFirst()
.orElse(null);
}

@Override
public List<String> getHeaders(String name) {
Header[] headers = delegate.getHeaders(name);
if (headers.length > 0) {
List<String> result = new ArrayList<>(headers.length);
for (Header header : headers) {
result.add(header.getValue());
}
return result;
}
return null;
if (headers == null || name == null) return null;

List<String> result = Arrays.stream(headers)
.filter(h -> name.equals(h.getName()))
.map(h -> h.getValue())
.collect(Collectors.toList());

return result.isEmpty() ? null : result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,38 @@
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpResponse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class InboundWrapper extends ExtendedInboundHeaders {
private final HttpResponse delegate;
private final Header[] headers;

public InboundWrapper(HttpResponse response) {
this.delegate = response;
this.headers = response == null ? null : response.getHeaders();
}

@Override
public String getHeader(String name) {
Header[] headers = delegate.getHeaders(name);
if (headers.length > 0) {
return headers[0].getValue();
}
return null;
if (headers == null || name == null) return null;

return Arrays.stream(headers)
.filter(h -> name.equals(h.getName()))
.map(h -> h.getValue())
.findFirst()
.orElse(null);
}

@Override
public List<String> getHeaders(String name) {
Header[] headers = delegate.getHeaders(name);
if (headers.length > 0) {
List<String> result = new ArrayList<>(headers.length);
for (Header header : headers) {
result.add(header.getValue());
}
return result;
}
return null;
if (headers == null || name == null) return null;

List<String> result = Arrays.stream(headers)
.filter(h -> name.equals(h.getName()))
.map(h -> h.getValue())
.collect(Collectors.toList());

return result.isEmpty() ? null : result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import com.newrelic.api.agent.HeaderType;
import com.newrelic.api.agent.InboundHeaders;

import java.net.http.HttpHeaders;
import java.net.http.HttpResponse;
import java.util.function.Function;

public class InboundWrapper implements InboundHeaders {

private final HttpResponse delegate;
private final HttpHeaders headers;

public InboundWrapper(HttpResponse response) {
this.delegate = response;
this.headers = response == null ? null : response.headers();
}

@Override
Expand All @@ -21,6 +21,6 @@ public HeaderType getHeaderType() {

@Override
public String getHeader(String name) {
return delegate.headers().firstValue(name).map(String::toString).orElse(null);
return headers.firstValue(name).map(String::toString).orElse(null);
}
}
Loading
Loading