Skip to content

Commit

Permalink
reuse code in util
Browse files Browse the repository at this point in the history
  • Loading branch information
XiXiaPdx committed Sep 2, 2021
1 parent d26aa48 commit 1f83b8f
Showing 1 changed file with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static Segment startSegment(URI uri) {
String scheme = uri.getScheme().toLowerCase();
Transaction txn = NewRelic.getAgent().getTransaction();
if (("http".equals(scheme) || "https".equals(scheme)) && txn != null) {
return txn.startSegment("JavaHttpClient");
return txn.startSegment("JavaHttpClient.send");
}
}
return null;
Expand All @@ -38,18 +38,9 @@ public static Segment startSegment(URI uri) {
try {
if (segment != null && uri != null) {
if (httpResponse != null) {
segment.reportAsExternal(HttpParameters
.library(LIBRARY)
.uri(uri)
.procedure(PROCEDURE)
.inboundHeaders(new InboundWrapper(httpResponse))
.build());
reportExternal(uri, segment, httpResponse);
} else if (throwableIsConnectException(throwable)) {
segment.reportAsExternal(GenericParameters
.library(LIBRARY)
.uri(UNKNOWN_HOST)
.procedure("failed")
.build());
reportUnknownHostExternal(segment);
} else if (throwable != null) {
NewRelic.noticeError(throwable);
}
Expand All @@ -67,31 +58,39 @@ public static Segment startSegment(URI uri) {

public static <T> void processResponse(HttpResponse<T> response, Segment segment){
if (response.uri() != null) {
segment.reportAsExternal(HttpParameters
.library(LIBRARY)
.uri(response.uri())
.procedure(PROCEDURE)
.inboundHeaders(new InboundWrapper(response))
.build());
segment.end();
reportExternal(response.uri(), segment, response);
}
}

public static void handleConnectException(Exception e, HttpRequest request, Segment segment){
if (request.uri() != null) {
if (throwableIsConnectException(e)) {
segment.reportAsExternal(GenericParameters
.library(LIBRARY)
.uri(UNKNOWN_HOST)
.procedure("failed")
.build());
segment.end();
reportUnknownHostExternal(segment);
} else {
NewRelic.noticeError(e);
}
}
}

private static <T> void reportExternal(URI uri, Segment segment, HttpResponse<T> httpResponse) {
segment.reportAsExternal(HttpParameters
.library(LIBRARY)
.uri(uri)
.procedure(PROCEDURE)
.inboundHeaders(new InboundWrapper(httpResponse))
.build());
segment.end();
}

private static void reportUnknownHostExternal(Segment segment) {
segment.reportAsExternal(GenericParameters
.library(LIBRARY)
.uri(UNKNOWN_HOST)
.procedure("failed")
.build());
segment.end();
}

private static boolean throwableIsConnectException(Throwable throwable) {
if (throwable instanceof ConnectException) {
return true;
Expand Down

0 comments on commit 1f83b8f

Please sign in to comment.