-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: instrumentation for apache http client 4.x (#1330)
@PrajeenRG LGTM! Sorry about the delay getting back to you there. Merging 🚀
- Loading branch information
Showing
15 changed files
with
1,050 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...ache-httpclient/src/main/java/kamon/instrumentation/apache/httpclient/RequestAdvisor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package kamon.instrumentation.apache.httpclient; | ||
|
||
import org.apache.http.HttpHost; | ||
import org.apache.http.HttpRequest; | ||
import org.apache.http.HttpResponse; | ||
|
||
import kamon.instrumentation.http.HttpClientInstrumentation.RequestHandler; | ||
import kamon.instrumentation.http.HttpMessage.RequestBuilder; | ||
import kamon.trace.Span; | ||
import kamon.Kamon; | ||
import kamon.context.Context; | ||
import kamon.context.Storage.Scope; | ||
import kamon.instrumentation.context.HasContext; | ||
|
||
import kanela.agent.libs.net.bytebuddy.asm.Advice; | ||
|
||
public class RequestAdvisor { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter(@Advice.Argument(0) HttpHost host, | ||
@Advice.Argument(value = 1, readOnly = false) HttpRequest request, | ||
@Advice.Local("handler") RequestHandler<HttpRequest> handler, | ||
@Advice.Local("scope") Scope scope) { | ||
if (((HasContext) request).context().nonEmpty()) { | ||
// Request has been instrumented already | ||
return; | ||
} | ||
final Context parentContext = Kamon.currentContext(); | ||
final RequestBuilder<HttpRequest> builder = ApacheHttpClientHelper.toRequestBuilder(host, request); | ||
handler = ApacheHttpClientInstrumentation.httpClientInstrumentation().createHandler(builder, parentContext); | ||
final Context ctx = parentContext.withEntry(Span.Key(), handler.span()); | ||
scope = Kamon.storeContext(ctx); | ||
request = handler.request(); | ||
((HasContext) request).setContext(ctx); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit(@Advice.Return HttpResponse response, | ||
@Advice.Thrown Throwable t, | ||
@Advice.Local("handler") RequestHandler<HttpRequest> handler, | ||
@Advice.Local("scope") Scope scope) { | ||
if (scope == null) { | ||
return; | ||
} | ||
ApacheHttpClientInstrumentation.processResponse(handler, response, t); | ||
scope.close(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...ient/src/main/java/kamon/instrumentation/apache/httpclient/RequestWithHandlerAdvisor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package kamon.instrumentation.apache.httpclient; | ||
|
||
import org.apache.http.HttpHost; | ||
import org.apache.http.HttpRequest; | ||
import org.apache.http.client.ResponseHandler; | ||
|
||
import kamon.Kamon; | ||
import kamon.context.Context; | ||
import kamon.context.Storage.Scope; | ||
import kamon.instrumentation.context.HasContext; | ||
import kamon.instrumentation.http.HttpClientInstrumentation.RequestHandler; | ||
import kamon.instrumentation.http.HttpMessage.RequestBuilder; | ||
import kamon.trace.Span; | ||
import kanela.agent.libs.net.bytebuddy.asm.Advice; | ||
|
||
public class RequestWithHandlerAdvisor { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter(@Advice.Argument(0) HttpHost host, | ||
@Advice.Argument(value = 1, readOnly = false) HttpRequest request, | ||
@Advice.Argument(value = 2, readOnly = false) ResponseHandler<?> resHandler, | ||
@Advice.Local("handler") RequestHandler<HttpRequest> reqHandler, | ||
@Advice.Local("scope") Scope scope) { | ||
if (((HasContext) request).context().nonEmpty()) { | ||
// Request has been instrumented already | ||
return; | ||
} | ||
final Context parentContext = Kamon.currentContext(); | ||
final RequestBuilder<HttpRequest> builder = ApacheHttpClientHelper.toRequestBuilder(host, request); | ||
reqHandler = ApacheHttpClientInstrumentation.httpClientInstrumentation().createHandler(builder, parentContext); | ||
resHandler = new ResponseHandlerProxy<>(reqHandler, resHandler, parentContext); | ||
final Context ctx = parentContext.withEntry(Span.Key(), reqHandler.span()); | ||
scope = Kamon.storeContext(ctx); | ||
request = reqHandler.request(); | ||
((HasContext) request).setContext(ctx); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit(@Advice.Local("scope") Scope scope) { | ||
if (scope != null) { | ||
scope.close(); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...ttpclient/src/main/java/kamon/instrumentation/apache/httpclient/ResponseHandlerProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package kamon.instrumentation.apache.httpclient; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.ClientProtocolException; | ||
import org.apache.http.client.ResponseHandler; | ||
|
||
import kamon.Kamon; | ||
import kamon.context.Context; | ||
import kamon.context.Storage.Scope; | ||
import kamon.instrumentation.http.HttpClientInstrumentation.RequestHandler; | ||
|
||
public class ResponseHandlerProxy<T, U> implements ResponseHandler<T> { | ||
|
||
private final ResponseHandler<T> delegate; | ||
private final RequestHandler<U> handler; | ||
private Context parentContext; | ||
|
||
public ResponseHandlerProxy(RequestHandler<U> handler, ResponseHandler<T> delegate, Context parentContext) { | ||
this.handler = handler; | ||
this.delegate = delegate; | ||
this.parentContext = parentContext; | ||
} | ||
|
||
@Override | ||
public T handleResponse(HttpResponse response) throws ClientProtocolException, IOException { | ||
ApacheHttpClientInstrumentation.processResponse(handler, response, null); | ||
// run original handler in parent context to avoid nesting of spans | ||
try (Scope ignored = Kamon.storeContext(parentContext)) { | ||
return delegate.handleResponse(response); | ||
} | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...e-httpclient/src/main/java/kamon/instrumentation/apache/httpclient/UriRequestAdvisor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package kamon.instrumentation.apache.httpclient; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.methods.HttpUriRequest; | ||
|
||
import kamon.Kamon; | ||
import kamon.context.Context; | ||
import kamon.context.Storage.Scope; | ||
import kamon.instrumentation.context.HasContext; | ||
import kamon.instrumentation.http.HttpClientInstrumentation.RequestHandler; | ||
import kamon.instrumentation.http.HttpMessage.RequestBuilder; | ||
import kamon.trace.Span; | ||
import kanela.agent.libs.net.bytebuddy.asm.Advice; | ||
|
||
public class UriRequestAdvisor { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter(@Advice.Argument(value = 0, readOnly = false) HttpUriRequest request, | ||
@Advice.Local("handler") RequestHandler<HttpUriRequest> handler, | ||
@Advice.Local("scope") Scope scope) { | ||
if (((HasContext) request).context().nonEmpty()) { | ||
// Request has been instrumented already | ||
return; | ||
} | ||
final Context parentContext = Kamon.currentContext(); | ||
final RequestBuilder<HttpUriRequest> builder = ApacheHttpClientHelper.toRequestBuilder(request); | ||
handler = ApacheHttpClientInstrumentation.httpClientInstrumentation().createHandler(builder, parentContext); | ||
final Context ctx = parentContext.withEntry(Span.Key(), handler.span()); | ||
scope = Kamon.storeContext(ctx); | ||
request = handler.request(); | ||
((HasContext) request).setContext(ctx); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit(@Advice.Return HttpResponse response, | ||
@Advice.Thrown Throwable t, | ||
@Advice.Local("handler") RequestHandler<HttpUriRequest> handler, | ||
@Advice.Local("scope") Scope scope) { | ||
if (scope == null) { | ||
return; | ||
} | ||
ApacheHttpClientInstrumentation.processResponse(handler, response, t); | ||
scope.close(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...t/src/main/java/kamon/instrumentation/apache/httpclient/UriRequestWithHandlerAdvisor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package kamon.instrumentation.apache.httpclient; | ||
|
||
import org.apache.http.client.ResponseHandler; | ||
import org.apache.http.client.methods.HttpUriRequest; | ||
|
||
import kamon.Kamon; | ||
import kamon.context.Context; | ||
import kamon.context.Storage.Scope; | ||
import kamon.instrumentation.context.HasContext; | ||
import kamon.instrumentation.http.HttpClientInstrumentation.RequestHandler; | ||
import kamon.instrumentation.http.HttpMessage.RequestBuilder; | ||
import kamon.trace.Span; | ||
import kanela.agent.libs.net.bytebuddy.asm.Advice; | ||
|
||
public class UriRequestWithHandlerAdvisor { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.Argument(value = 0, readOnly = false) HttpUriRequest request, | ||
@Advice.Argument(value = 1, readOnly = false) ResponseHandler<?> resHandler, | ||
@Advice.Local("handler") RequestHandler<HttpUriRequest> reqHandler, | ||
@Advice.Local("scope") Scope scope) { | ||
if (((HasContext) request).context().nonEmpty()) { | ||
// Request has been instrumented already | ||
return; | ||
} | ||
final Context parentContext = Kamon.currentContext(); | ||
final RequestBuilder<HttpUriRequest> builder = ApacheHttpClientHelper.toRequestBuilder(request); | ||
reqHandler = ApacheHttpClientInstrumentation.httpClientInstrumentation().createHandler(builder, parentContext); | ||
resHandler = new ResponseHandlerProxy<>(reqHandler, resHandler, parentContext); | ||
final Context ctx = parentContext.withEntry(Span.Key(), reqHandler.span()); | ||
scope = Kamon.storeContext(ctx); | ||
request = reqHandler.request(); | ||
((HasContext) request).setContext(ctx); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit(@Advice.Local("scope") Scope scope) { | ||
if (scope != null) { | ||
scope.close(); | ||
} | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
instrumentation/kamon-apache-httpclient/src/main/resources/reference.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# ================================================== # | ||
# kamon Apache HttpClient 2.0 client reference configuration # | ||
# ================================================== # | ||
|
||
# Settings to control the HTTP Client instrumentation | ||
# | ||
# IMPORTANT: The entire configuration of the HTTP Client Instrumentation is based on the constructs provided by the | ||
# Kamon Instrumentation Common library which will always fallback to the settings found under the | ||
# "kamon.instrumentation.http-client.default" path. The default settings have been included here to make them easy to | ||
# find and understand in the context of this project and commented out so that any changes to the default settings | ||
# will actually have effect. | ||
# | ||
kamon.instrumentation.apache.httpclient { | ||
|
||
# | ||
# Configuration for HTTP context propagation. | ||
# | ||
propagation { | ||
|
||
# Enables or disables HTTP context propagation on this HTTP client instrumentation. Please note that if | ||
# propagation is disabled then some distributed tracing features will not be work as expected (e.g. Spans can | ||
# be created and reported but will not be linked across boundaries nor take trace identifiers from tags). | ||
#enabled = yes | ||
|
||
# HTTP propagation channel to b used by this instrumentation. Take a look at the kamon.propagation.http.default | ||
# configuration for more details on how to configure the detault HTTP context propagation. | ||
#channel = "default" | ||
} | ||
|
||
tracing { | ||
|
||
# Enables HTTP request tracing. When enabled the instrumentation will create Spans for outgoing requests | ||
# and finish them when the response is received from the server. | ||
#enabled = yes | ||
|
||
# Enables collection of span metrics using the `span.processing-time` metric. | ||
#span-metrics = on | ||
|
||
# Select which tags should be included as span and span metric tags. The possible options are: | ||
# - span: the tag is added as a Span tag (i.e. using span.tag(...)) | ||
# - metric: the tag is added a a Span metric tag (i.e. using span.tagMetric(...)) | ||
# - off: the tag is not used. | ||
# | ||
tags { | ||
|
||
# Use the http.url tag. | ||
#url = span | ||
|
||
# Use the http.method tag. | ||
#method = metric | ||
|
||
# Use the http.status_code tag. | ||
#status-code = metric | ||
|
||
# Copy tags from the context into the Spans with the specified purpouse. For example, to copy a customer_type | ||
# tag from the context into the HTTP Client Span created by the instrumentation, the following configuration | ||
# should be added: | ||
# | ||
# from-context { | ||
# customer_type = span | ||
# } | ||
# | ||
from-context { | ||
|
||
} | ||
} | ||
|
||
operations { | ||
|
||
# The default operation name to be used when creating Spans to handle the HTTP client requests. The HTTP | ||
# Client instrumentation will always try to use the HTTP Operation Name Generator configured below to get | ||
# a name, but if it fails to generate it then this name will be used. | ||
#default = "http.client.request" | ||
|
||
# FQCN for a HttpOperationNameGenerator implementation, or ony of the following shorthand forms: | ||
# - hostname: Uses the request Host as the operation name. | ||
# - method: Uses the request HTTP method as the operation name. | ||
# | ||
#name-generator = "method" | ||
} | ||
} | ||
|
||
} | ||
|
||
kanela { | ||
modules { | ||
apache-httpclient { | ||
name = "Apache Http Client" | ||
description = "Provides tracing of client calls made with the official Apache HttpClient library." | ||
instrumentations = [ | ||
"kamon.instrumentation.apache.httpclient.ApacheHttpClientInstrumentation" | ||
] | ||
|
||
within = [ | ||
"org.apache.http..*" | ||
] | ||
} | ||
} | ||
} |
Oops, something went wrong.