Skip to content

Commit a9623a6

Browse files
committed
Add comments and explanation on strategies used
1 parent 6107454 commit a9623a6

File tree

9 files changed

+156
-36
lines changed

9 files changed

+156
-36
lines changed

dd-java-agent/src/main/java/io/opentracing/contrib/agent/helper/AWSClientHelper.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,25 @@
99
import java.util.Arrays;
1010
import java.util.List;
1111

12-
12+
/**
13+
* Patch the AWS Client during the building steps.
14+
* This opentracing integration is compatible with the latest release of the AWS SDK
15+
*/
1316
public class AWSClientHelper extends DDAgentTracingHelper<AwsClientBuilder> {
1417

1518

1619
public AWSClientHelper(Rule rule) {
1720
super(rule);
1821
}
1922

20-
21-
@Override
23+
/**
24+
* Strategy: we add a tracing handler to the client when it has just been built. We intercept the return value of
25+
* the com.amazonaws.client.builder.AwsClientBuilder.build() method and add the handler.
26+
*
27+
* @param client The fresh AWS client instance
28+
* @return The same instance, but patched
29+
* @throws Exception
30+
*/
2231
protected AwsClientBuilder doPatch(AwsClientBuilder client) throws Exception {
2332

2433
RequestHandler2 handler = new TracingRequestHandler(tracer);

dd-java-agent/src/main/java/io/opentracing/contrib/agent/helper/ApacheHTTPClientHelper.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,32 @@
44
import org.apache.http.impl.client.HttpClientBuilder;
55
import org.jboss.byteman.rule.Rule;
66

7-
7+
/**
8+
* Patch the Apache HTTP Client during the building steps
9+
*/
810
public class ApacheHTTPClientHelper extends DDAgentTracingHelper<HttpClientBuilder> {
911

1012
public ApacheHTTPClientHelper(Rule rule) {
1113
super(rule);
1214
}
1315

1416

17+
@Override
1518
public HttpClientBuilder patch(HttpClientBuilder builder) {
1619
return super.patch(builder);
1720
}
1821

1922

20-
@Override
23+
/**
24+
* Strategy: We replace the legacy builder by a new instance providing by the opentracing contribution when
25+
* the builder is instantiate. @see org.apache.http.impl.client.HttpClientBuilder.create() method
26+
*
27+
* @param builder The legacy builder instance
28+
* @return A tracing builder instance (new reference)
29+
* @throws Exception
30+
*/
2131
protected HttpClientBuilder doPatch(HttpClientBuilder builder) throws Exception {
22-
2332
return new TracingHttpClientBuilder();
24-
2533
}
2634

2735
}

dd-java-agent/src/main/java/io/opentracing/contrib/agent/helper/CassandraHelper.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,44 @@
66

77
import java.lang.reflect.Constructor;
88

9-
9+
/**
10+
* Patch each new sessions created when trying to connect to a Cassandra cluster.
11+
*/
1012
public class CassandraHelper extends DDAgentTracingHelper<Session> {
1113

1214

1315
protected CassandraHelper(Rule rule) {
1416
super(rule);
1517
}
1618

17-
19+
@Override
1820
public Session patch(Session session) {
1921
return super.patch(session);
2022
}
2123

22-
@Override
24+
25+
/**
26+
* Strategy: each time we build a connection to a Cassandra cluster, the com.datastax.driver.core.Cluster$Manager.newSession()
27+
* method is called. The opentracing contribution is a simple wrapper, so we just have to wrap the new session.
28+
*
29+
* @param session The fresh session to patch
30+
* @return A new tracing session
31+
* @throws Exception
32+
*/
2333
protected Session doPatch(Session session) throws Exception {
2434

2535

36+
if ("io.opentracing.contrib.cassandra.TracingSession".equals(session.getClass().getCanonicalName())) {
37+
return session;
38+
}
39+
2640
Class<?> clazz = Class.forName("io.opentracing.contrib.cassandra.TracingSession");
2741
Constructor<?> constructor = clazz.getDeclaredConstructor(Session.class, Tracer.class);
2842
constructor.setAccessible(true);
2943
Session newSession = (Session) constructor.newInstance(session, tracer);
3044

3145
return newSession;
3246

33-
3447
}
3548

3649

dd-java-agent/src/main/java/io/opentracing/contrib/agent/helper/DDAgentTracingHelper.java

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,46 @@
88
import java.util.logging.Level;
99
import java.util.logging.Logger;
1010

11-
11+
/**
12+
* This class provides helpfull stuff in order to easy patch object using Byteman rules
13+
*
14+
* @param <ObjectType> The type of the object to patch
15+
*/
1216
public abstract class DDAgentTracingHelper<ObjectType> extends OpenTracingHelper {
1317

14-
protected static Tracer tracer;
1518
private static final Logger LOGGER = Logger.getLogger(DDAgentTracingHelper.class.getCanonicalName());
1619

17-
public DDAgentTracingHelper(Rule rule) {
20+
/**
21+
* The current instance of the tracer. If something goes wrong during the resolution,
22+
* we provides a NoopTracer.
23+
*/
24+
protected final Tracer tracer;
25+
26+
DDAgentTracingHelper(Rule rule) {
1827
super(rule);
28+
Tracer tracerResolved;
1929
try {
20-
tracer = getTracer() != null ? getTracer() : NoopTracerFactory.create();
30+
tracerResolved = getTracer();
31+
tracerResolved = tracerResolved == null ? NoopTracerFactory.create() : tracerResolved;
2132
} catch (Exception e) {
22-
tracer = NoopTracerFactory.create();
23-
warning("Failed to retrieve the tracer, using a NoopTracer: " + e.getMessage());
33+
tracerResolved = NoopTracerFactory.create();
34+
warning("Failed to retrieve the tracer, using a NoopTracer instead: " + e.getMessage());
2435
logStackTrace(e.getMessage(), e);
2536
}
37+
tracer = tracerResolved;
2638
}
2739

40+
41+
/**
42+
* This method takes an object and applies some mutation in order to add tracing capabilities.
43+
* This method should never return any Exception in order to not stop the app traced.
44+
* <p>
45+
* This method should be defined as final, but something Byteman need to define this one with the explicit
46+
* type (i.e. without using generic), so this is why we don't use final here.
47+
*
48+
* @param args The object to patch, the type is defined by the subclass instantiation
49+
* @return The object patched
50+
*/
2851
public ObjectType patch(ObjectType args) {
2952

3053
info("Try to patch " + args.getClass().getName());
@@ -38,19 +61,42 @@ public ObjectType patch(ObjectType args) {
3861
patched = args;
3962
}
4063
return patched;
41-
4264
}
4365

44-
abstract protected ObjectType doPatch(ObjectType input) throws Exception;
66+
/**
67+
* The current implementation of the patch
68+
*
69+
* @param obj the object to patch
70+
* @return the object patched
71+
* @throws Exception The exceptions are managed directly to the patch method
72+
*/
73+
abstract protected ObjectType doPatch(ObjectType obj) throws Exception;
74+
4575

76+
/**
77+
* Simple wrapper to emit a warning
78+
*
79+
* @param message the message to log as a warning
80+
*/
4681
protected void warning(String message) {
4782
log(Level.WARNING, message);
4883
}
4984

85+
/**
86+
* Simple wrapper to emit an info
87+
*
88+
* @param message the message to log as an info
89+
*/
5090
protected void info(String message) {
5191
log(Level.INFO, message);
5292
}
5393

94+
/**
95+
* Simple wrapper to emit the corresponding stacktrace. To not warn the user, we log the stack as a debug info.
96+
* By default, the stack traces are noit shown in the log.
97+
*
98+
* @param message the stacktrace to log as a debug
99+
*/
54100
protected void logStackTrace(String message, Throwable th) {
55101
LOGGER.log(Level.FINE, message, th);
56102
}

dd-java-agent/src/main/java/io/opentracing/contrib/agent/helper/ElasticsearchHelper.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010

1111
import java.lang.reflect.Method;
1212

13-
13+
/**
14+
* Instrument all Elasticsearch queries.
15+
* We have not found a way to inject the opentracing contribution, so this helper is the instrumentation.
16+
* FIXME find a better to way to inject the OT contrib
17+
*/
1418
public class ElasticsearchHelper extends DDAgentTracingHelper<ActionListener> {
1519

1620
public ElasticsearchHelper(Rule rule) {
@@ -19,26 +23,38 @@ public ElasticsearchHelper(Rule rule) {
1923

2024
private Object request;
2125

26+
/**
27+
* This method is used to register/save some object that will be used for the instrumentation.
28+
* Currently, we need to keep a reference of the request called
29+
*
30+
* @param request The request used for the query
31+
*/
2232
public void registerArgs(Object request) {
2333
this.request = request;
2434
}
2535

26-
36+
@Override
2737
public ActionListener patch(ActionListener listener) {
2838
return super.patch(listener);
2939
}
3040

31-
32-
@Override
33-
protected ActionListener doPatch(ActionListener listener) throws Exception {
34-
35-
if (listener instanceof TracingResponseListener) {
41+
/**
42+
* Strategy: When a query is executed, if start the instrumentation and a new Span.
43+
* We override the default FutureAction by using the one provided in the opentracing contribution.
44+
*
45+
* @param listener default listener
46+
* @return The tracing listener, the default listener is wrapped in this one.
47+
* @throws Exception
48+
*/
49+
protected ActionListener doPatch(ActionListener listener) throws Exception {
50+
51+
if (listener instanceof TracingResponseListener) {
3652
return listener;
3753
}
3854

3955
Tracer.SpanBuilder spanBuilder = tracer.buildSpan(request.getClass().getSimpleName()).ignoreActiveSpan().withTag(Tags.SPAN_KIND.getKey(), "client");
4056
ActiveSpan parentSpan = tracer.activeSpan();
41-
if(parentSpan != null) {
57+
if (parentSpan != null) {
4258
spanBuilder.asChildOf(parentSpan.context());
4359
}
4460

dd-java-agent/src/main/java/io/opentracing/contrib/agent/helper/JettyServletHelper.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@
88
import java.util.EnumSet;
99

1010
/**
11-
* Created by gpolaert on 6/15/17.
11+
* Patch the Jetty Servlet during the init steps
1212
*/
1313
public class JettyServletHelper extends DDAgentTracingHelper<ServletContextHandler> {
1414

1515
public JettyServletHelper(Rule rule) {
1616
super(rule);
1717
}
1818

19-
@Override
19+
/**
20+
* Strategy: Use the contextHandler provided to add a new Tracing filter
21+
*
22+
* @param contextHandler The current contextHandler
23+
* @return The same current contextHandler but patched
24+
* @throws Exception
25+
*/
2026
protected ServletContextHandler doPatch(ServletContextHandler contextHandler) throws Exception {
2127

2228
String[] patterns = {"/*"};

dd-java-agent/src/main/java/io/opentracing/contrib/agent/helper/MongoHelper.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,24 @@
44
import io.opentracing.contrib.mongo.TracingCommandListener;
55
import org.jboss.byteman.rule.Rule;
66

7-
7+
/**
8+
* Patch the Mongo builder before constructing the final client
9+
*/
810
public class MongoHelper extends DDAgentTracingHelper<MongoClientOptions.Builder> {
911

1012

1113
public MongoHelper(Rule rule) {
1214
super(rule);
1315
}
1416

15-
16-
@Override
17+
/**
18+
* Strategy: Just before com.mongodb.MongoClientOptions$Builder.build() method is called, we add a new command listener
19+
* in charge of the tracing.
20+
*
21+
* @param builder The builder instance
22+
* @return The same builder instance with a new tracing command listener that will be use for the client construction
23+
* @throws Exception
24+
*/
1725
protected MongoClientOptions.Builder doPatch(MongoClientOptions.Builder builder) throws Exception {
1826

1927

dd-java-agent/src/main/java/io/opentracing/contrib/agent/helper/OkHttpHelper.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
import static io.opentracing.contrib.okhttp3.OkHttpClientSpanDecorator.STANDARD_TAGS;
1010

11+
1112
/**
12-
* Created by gpolaert on 6/15/17.
13+
* Patch the OkHttp Client during the building steps.
1314
*/
1415
public class OkHttpHelper extends DDAgentTracingHelper<OkHttpClient.Builder> {
1516

@@ -18,7 +19,14 @@ public OkHttpHelper(Rule rule) {
1819
super(rule);
1920
}
2021

21-
@Override
22+
/**
23+
* Strategy: Just before the okhttp3.OkHttpClient$Builder.build() method called, we add a new interceptor for the tracing
24+
* part.
25+
*
26+
* @param builder The builder instance to patch
27+
* @return The same builder instance with a new tracing interceptor
28+
* @throws Exception
29+
*/
2230
protected OkHttpClient.Builder doPatch(OkHttpClient.Builder builder) throws Exception {
2331
TracingInterceptor interceptor = new TracingInterceptor(tracer, Collections.singletonList(STANDARD_TAGS));
2432
builder.addInterceptor(interceptor);

dd-java-agent/src/main/java/io/opentracing/contrib/agent/helper/TomcatServletHelper.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@
88
import java.util.EnumSet;
99

1010
/**
11-
* Created by gpolaert on 6/15/17.
11+
* Patch the Jetty Servlet during the init steps
1212
*/
1313
public class TomcatServletHelper extends DDAgentTracingHelper<ApplicationContext> {
1414

1515
public TomcatServletHelper(Rule rule) {
1616
super(rule);
1717
}
1818

19-
@Override
19+
/**
20+
* Strategy: Use the contextHandler provided to add a new Tracing filter
21+
*
22+
* @param contextHandler The current contextHandler
23+
* @return The same current contextHandler but patched
24+
* @throws Exception
25+
*/
2026
protected ApplicationContext doPatch(ApplicationContext contextHandler) throws Exception {
2127

2228
String[] patterns = {"/*"};

0 commit comments

Comments
 (0)