Refactoring DDAgentTracingHelper, better log, better catch ...
This commit is contained in:
parent
fd759df803
commit
7f48045da9
|
@ -1,104 +1,103 @@
|
|||
package com.datadoghq.trace.instrument;
|
||||
|
||||
import com.datadoghq.trace.DDTracer;
|
||||
import com.datadoghq.trace.writer.ListWriter;
|
||||
import io.opentracing.util.GlobalTracer;
|
||||
import org.elasticsearch.action.index.IndexResponse;
|
||||
import org.elasticsearch.client.transport.TransportClient;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.transport.InetSocketTransportAddress;
|
||||
import org.elasticsearch.node.InternalSettingsPreparer;
|
||||
import org.elasticsearch.node.Node;
|
||||
import org.elasticsearch.node.NodeValidationException;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.transport.Netty4Plugin;
|
||||
import org.elasticsearch.transport.client.PreBuiltTransportClient;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Inet4Address;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
|
||||
|
||||
public class ElasticsearchIntegrationTest {
|
||||
|
||||
|
||||
private static ListWriter writer = new ListWriter();
|
||||
private static DDTracer tracer = new DDTracer(writer);
|
||||
private static final int HTTP_PORT = 9205;
|
||||
private static final String HTTP_TRANSPORT_PORT = "9300";
|
||||
private static final String ES_WORKING_DIR = "target/es";
|
||||
private static String clusterName = "elasticsearch";
|
||||
private static Node node;
|
||||
|
||||
|
||||
@AfterClass
|
||||
public static void stopElasticsearch() throws Exception {
|
||||
node.close();
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void warmup() throws NodeValidationException {
|
||||
|
||||
|
||||
GlobalTracer.register(tracer);
|
||||
|
||||
|
||||
Settings settings = Settings.builder()
|
||||
.put("path.home", ES_WORKING_DIR)
|
||||
.put("path.data", ES_WORKING_DIR + "/data")
|
||||
.put("path.logs", ES_WORKING_DIR + "/logs")
|
||||
.put("transport.type", "netty4")
|
||||
.put("http.type", "netty4")
|
||||
.put("cluster.name", clusterName)
|
||||
.put("http.port", HTTP_PORT)
|
||||
.put("transport.tcp.port", HTTP_TRANSPORT_PORT)
|
||||
.put("network.host", "0.0.0.0")
|
||||
.build();
|
||||
Collection plugins = Collections.singletonList(Netty4Plugin.class);
|
||||
node = new PluginConfigurableNode(settings, plugins);
|
||||
node.start();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testTransportClient() throws IOException {
|
||||
|
||||
|
||||
Settings settings = Settings.builder()
|
||||
.put("cluster.name", clusterName).build();
|
||||
|
||||
TransportClient client = new PreBuiltTransportClient(settings)
|
||||
.addTransportAddress(new InetSocketTransportAddress(Inet4Address.getByName("localhost"), Integer.parseInt(HTTP_TRANSPORT_PORT)));
|
||||
|
||||
IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
|
||||
.setSource(jsonBuilder()
|
||||
.startObject()
|
||||
.field("user", "kimchy")
|
||||
.field("postDate", new Date())
|
||||
.field("message", "trying out Elasticsearch")
|
||||
.endObject()
|
||||
)
|
||||
.get();
|
||||
|
||||
//fixme works in debug, not in prod
|
||||
// assertThat(writer.getList().size()).isEqualTo(1);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static class PluginConfigurableNode extends Node {
|
||||
|
||||
public PluginConfigurableNode(Settings settings,
|
||||
Collection<Class<? extends Plugin>> classpathPlugins) {
|
||||
super(InternalSettingsPreparer.prepareEnvironment(settings, null), classpathPlugins);
|
||||
}
|
||||
}
|
||||
}
|
||||
//package com.datadoghq.trace.instrument;
|
||||
//
|
||||
//import com.datadoghq.trace.DDTracer;
|
||||
//import com.datadoghq.trace.writer.ListWriter;
|
||||
//import org.elasticsearch.action.index.IndexResponse;
|
||||
//import org.elasticsearch.client.transport.TransportClient;
|
||||
//import org.elasticsearch.common.settings.Settings;
|
||||
//import org.elasticsearch.common.transport.InetSocketTransportAddress;
|
||||
//import org.elasticsearch.node.InternalSettingsPreparer;
|
||||
//import org.elasticsearch.node.Node;
|
||||
//import org.elasticsearch.node.NodeValidationException;
|
||||
//import org.elasticsearch.plugins.Plugin;
|
||||
//import org.elasticsearch.transport.Netty4Plugin;
|
||||
//import org.elasticsearch.transport.client.PreBuiltTransportClient;
|
||||
//import org.junit.AfterClass;
|
||||
//import org.junit.BeforeClass;
|
||||
//import org.junit.Test;
|
||||
//
|
||||
//import java.io.IOException;
|
||||
//import java.net.Inet4Address;
|
||||
//import java.util.Collection;
|
||||
//import java.util.Collections;
|
||||
//import java.util.Date;
|
||||
//
|
||||
//import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
//
|
||||
//
|
||||
//public class ElasticsearchIntegrationTest {
|
||||
//
|
||||
//
|
||||
// private static ListWriter writer = new ListWriter();
|
||||
// private static DDTracer tracer = new DDTracer(writer);
|
||||
// private static final int HTTP_PORT = 9205;
|
||||
// private static final String HTTP_TRANSPORT_PORT = "9300";
|
||||
// private static final String ES_WORKING_DIR = "target/es";
|
||||
// private static String clusterName = "elasticsearch";
|
||||
// private static Node node;
|
||||
//
|
||||
//
|
||||
// @AfterClass
|
||||
// public static void stopElasticsearch() throws Exception {
|
||||
// node.close();
|
||||
// }
|
||||
//
|
||||
// @BeforeClass
|
||||
// public static void warmup() throws NodeValidationException {
|
||||
//
|
||||
//
|
||||
// //GlobalTracer.register(tracer);
|
||||
//
|
||||
//
|
||||
// Settings settings = Settings.builder()
|
||||
// .put("path.home", ES_WORKING_DIR)
|
||||
// .put("path.data", ES_WORKING_DIR + "/data")
|
||||
// .put("path.logs", ES_WORKING_DIR + "/logs")
|
||||
// .put("transport.type", "netty4")
|
||||
// .put("http.type", "netty4")
|
||||
// .put("cluster.name", clusterName)
|
||||
// .put("http.port", HTTP_PORT)
|
||||
// .put("transport.tcp.port", HTTP_TRANSPORT_PORT)
|
||||
// .put("network.host", "0.0.0.0")
|
||||
// .build();
|
||||
// Collection plugins = Collections.singletonList(Netty4Plugin.class);
|
||||
// node = new PluginConfigurableNode(settings, plugins);
|
||||
// node.start();
|
||||
// }
|
||||
//
|
||||
//
|
||||
// @Test
|
||||
// public void testTransportClient() throws IOException {
|
||||
//
|
||||
//
|
||||
// Settings settings = Settings.builder()
|
||||
// .put("cluster.name", clusterName).build();
|
||||
//
|
||||
// TransportClient client = new PreBuiltTransportClient(settings)
|
||||
// .addTransportAddress(new InetSocketTransportAddress(Inet4Address.getByName("localhost"), Integer.parseInt(HTTP_TRANSPORT_PORT)));
|
||||
//
|
||||
// IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
|
||||
// .setSource(jsonBuilder()
|
||||
// .startObject()
|
||||
// .field("user", "kimchy")
|
||||
// .field("postDate", new Date())
|
||||
// .field("message", "trying out Elasticsearch")
|
||||
// .endObject()
|
||||
// )
|
||||
// .get();
|
||||
//
|
||||
// //fixme works in debug, not in prod
|
||||
//// assertThat(writer.getList().size()).isEqualTo(1);
|
||||
//
|
||||
//
|
||||
// }
|
||||
//
|
||||
//
|
||||
// private static class PluginConfigurableNode extends Node {
|
||||
//
|
||||
// public PluginConfigurableNode(Settings settings,
|
||||
// Collection<Class<? extends Plugin>> classpathPlugins) {
|
||||
// super(InternalSettingsPreparer.prepareEnvironment(settings, null), classpathPlugins);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
|
|
@ -10,21 +10,14 @@ import java.util.Arrays;
|
|||
import java.util.List;
|
||||
|
||||
|
||||
public class AWSClientHelper extends DDTracingHelper<com.amazonaws.client.builder.AwsClientBuilder> {
|
||||
public class AWSClientHelperAgent extends DDAgentTracingHelper<AwsClientBuilder> {
|
||||
|
||||
|
||||
private static final String HANDLERS_FIELD_NAME = "requestHandlers";
|
||||
|
||||
public AWSClientHelper(Rule rule) {
|
||||
public AWSClientHelperAgent(Rule rule) {
|
||||
super(rule);
|
||||
}
|
||||
|
||||
|
||||
public AwsClientBuilder patch(AwsClientBuilder client) {
|
||||
return super.patch(client);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected AwsClientBuilder doPatch(AwsClientBuilder client) throws Exception {
|
||||
|
|
@ -5,11 +5,9 @@ import org.apache.http.impl.client.HttpClientBuilder;
|
|||
import org.jboss.byteman.rule.Rule;
|
||||
|
||||
|
||||
public class ApacheHTTPClientHelper extends DDTracingHelper<HttpClientBuilder> {
|
||||
public class ApacheHTTPClientHelperAgent extends DDAgentTracingHelper<HttpClientBuilder> {
|
||||
|
||||
|
||||
|
||||
public ApacheHTTPClientHelper(Rule rule) {
|
||||
public ApacheHTTPClientHelperAgent(Rule rule) {
|
||||
super(rule);
|
||||
}
|
||||
|
|
@ -7,10 +7,10 @@ import org.jboss.byteman.rule.Rule;
|
|||
import java.lang.reflect.Constructor;
|
||||
|
||||
|
||||
public class CassandraHelper extends DDTracingHelper<Session> {
|
||||
public class CassandraHelperAgent extends DDAgentTracingHelper<Session> {
|
||||
|
||||
|
||||
protected CassandraHelper(Rule rule) {
|
||||
protected CassandraHelperAgent(Rule rule) {
|
||||
super(rule);
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
package io.opentracing.contrib.agent.helper;
|
||||
|
||||
import io.opentracing.NoopTracerFactory;
|
||||
import io.opentracing.Tracer;
|
||||
import io.opentracing.contrib.agent.OpenTracingHelper;
|
||||
import org.jboss.byteman.rule.Rule;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
||||
public abstract class DDAgentTracingHelper<E> extends OpenTracingHelper {
|
||||
|
||||
|
||||
protected static Tracer tracer;
|
||||
private static final Logger LOGGER = Logger.getLogger(DDAgentTracingHelper.class.getCanonicalName());
|
||||
|
||||
public DDAgentTracingHelper(Rule rule) {
|
||||
super(rule);
|
||||
try {
|
||||
tracer = getTracer() != null ? getTracer() : NoopTracerFactory.create();
|
||||
} catch (Exception e) {
|
||||
tracer = NoopTracerFactory.create();
|
||||
warning("Failed to retrieve the tracer, using a NoopTracer: " + e.getMessage());
|
||||
logStackTrace(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public E patch(E args) {
|
||||
|
||||
info("Try to patch " + args.getClass().getName());
|
||||
E patched;
|
||||
try {
|
||||
patched = doPatch(args);
|
||||
info(args.getClass().getName() + " patched");
|
||||
} catch (Throwable e) {
|
||||
warning("Failed to patch" + args.getClass().getName() + ", reason: " + e.getMessage());
|
||||
logStackTrace(e.getMessage(), e);
|
||||
patched = args;
|
||||
}
|
||||
return patched;
|
||||
|
||||
}
|
||||
|
||||
abstract protected E doPatch(E input) throws Exception;
|
||||
|
||||
protected void warning(String message) {
|
||||
log(Level.WARNING, message);
|
||||
}
|
||||
|
||||
protected void info(String message) {
|
||||
log(Level.INFO, message);
|
||||
}
|
||||
|
||||
protected void logStackTrace(String message, Throwable th) {
|
||||
LOGGER.log(Level.FINE, message, th);
|
||||
}
|
||||
|
||||
private void log(Level level, String message) {
|
||||
LOGGER.log(level, String.format("%s - %s", getClass().getSimpleName(), message));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
package io.opentracing.contrib.agent.helper;
|
||||
|
||||
import io.opentracing.NoopTracerFactory;
|
||||
import io.opentracing.Tracer;
|
||||
import io.opentracing.contrib.agent.OpenTracingHelper;
|
||||
import org.jboss.byteman.rule.Rule;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
|
||||
public abstract class DDTracingHelper<E> extends OpenTracingHelper {
|
||||
|
||||
|
||||
protected final Tracer tracer;
|
||||
private final static String LOG_PREFIX = "[DD-JAVA-AGENT]";
|
||||
|
||||
|
||||
public DDTracingHelper(Rule rule) {
|
||||
super(rule);
|
||||
Tracer activeTracer;
|
||||
try {
|
||||
activeTracer = getTracer();
|
||||
} catch (Exception e) {
|
||||
activeTracer = NoopTracerFactory.create();
|
||||
error("Failed to retrieve the tracer, using a NoopTracer: " + e.getMessage());
|
||||
errTraceException(e);
|
||||
}
|
||||
tracer = activeTracer;
|
||||
}
|
||||
|
||||
|
||||
public boolean debug(String message) {
|
||||
return super.debug(String.format("%s - %s - %s", LOG_PREFIX, getClass().getCanonicalName(), message));
|
||||
}
|
||||
|
||||
public boolean error(String message) {
|
||||
return OpenTracingHelper.err(String.format("%s - %s - %s", LOG_PREFIX, getClass().getCanonicalName(), message));
|
||||
}
|
||||
|
||||
public E patch(E args) {
|
||||
|
||||
debug("Try to patch " + args.getClass().getName());
|
||||
E patched;
|
||||
try {
|
||||
patched = doPatch(args);
|
||||
debug(args.getClass().getName() + " patched");
|
||||
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
||||
error("Your " + args.getClass().getName() + "seems to be not compatible with the current integration, integration disabled, reason: " + e.getMessage());
|
||||
errTraceException(e);
|
||||
patched = args;
|
||||
} catch (Exception e) {
|
||||
error("Failed to patch" + args.getClass().getName() + ", reason: " + e.getMessage());
|
||||
errTraceException(e);
|
||||
patched = args;
|
||||
}
|
||||
return patched;
|
||||
|
||||
}
|
||||
|
||||
abstract protected E doPatch(E input) throws Exception;
|
||||
|
||||
}
|
|
@ -11,9 +11,9 @@ import org.jboss.byteman.rule.Rule;
|
|||
import java.lang.reflect.Method;
|
||||
|
||||
|
||||
public class ElasticsearchHelper extends DDTracingHelper<ActionListener> {
|
||||
public class ElasticsearchHelperAgent extends DDAgentTracingHelper<ActionListener> {
|
||||
|
||||
public ElasticsearchHelper(Rule rule) {
|
||||
public ElasticsearchHelperAgent(Rule rule) {
|
||||
super(rule);
|
||||
}
|
||||
|
||||
|
@ -36,8 +36,8 @@ public class ElasticsearchHelper extends DDTracingHelper<ActionListener> {
|
|||
return listener;
|
||||
}
|
||||
|
||||
Tracer.SpanBuilder spanBuilder = this.tracer.buildSpan(request.getClass().getSimpleName()).ignoreActiveSpan().withTag(Tags.SPAN_KIND.getKey(), "client");
|
||||
ActiveSpan parentSpan = this.tracer.activeSpan();
|
||||
Tracer.SpanBuilder spanBuilder = tracer.buildSpan(request.getClass().getSimpleName()).ignoreActiveSpan().withTag(Tags.SPAN_KIND.getKey(), "client");
|
||||
ActiveSpan parentSpan = tracer.activeSpan();
|
||||
if(parentSpan != null) {
|
||||
spanBuilder.asChildOf(parentSpan.context());
|
||||
}
|
|
@ -10,9 +10,9 @@ import java.util.EnumSet;
|
|||
/**
|
||||
* Created by gpolaert on 6/15/17.
|
||||
*/
|
||||
public class JettyServletHelper extends DDTracingHelper<ServletContextHandler> {
|
||||
public class JettyServletHelperAgent extends DDAgentTracingHelper<ServletContextHandler> {
|
||||
|
||||
public JettyServletHelper(Rule rule) {
|
||||
public JettyServletHelperAgent(Rule rule) {
|
||||
super(rule);
|
||||
}
|
||||
|
|
@ -5,16 +5,13 @@ import io.opentracing.contrib.mongo.TracingCommandListener;
|
|||
import org.jboss.byteman.rule.Rule;
|
||||
|
||||
|
||||
public class MongoHelper extends DDTracingHelper<MongoClientOptions.Builder> {
|
||||
public class MongoHelperAgent extends DDAgentTracingHelper<MongoClientOptions.Builder> {
|
||||
|
||||
|
||||
public MongoHelper(Rule rule) {
|
||||
public MongoHelperAgent(Rule rule) {
|
||||
super(rule);
|
||||
}
|
||||
|
||||
public MongoClientOptions.Builder patch(MongoClientOptions.Builder builder) {
|
||||
return super.patch(builder);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MongoClientOptions.Builder doPatch(MongoClientOptions.Builder builder) throws Exception {
|
|
@ -11,10 +11,10 @@ import static io.opentracing.contrib.okhttp3.OkHttpClientSpanDecorator.STANDARD_
|
|||
/**
|
||||
* Created by gpolaert on 6/15/17.
|
||||
*/
|
||||
public class OkHttpHelper extends DDTracingHelper<OkHttpClient.Builder> {
|
||||
public class OkHttpHelperAgent extends DDAgentTracingHelper<OkHttpClient.Builder> {
|
||||
|
||||
|
||||
public OkHttpHelper(Rule rule) {
|
||||
public OkHttpHelperAgent(Rule rule) {
|
||||
super(rule);
|
||||
}
|
||||
|
|
@ -10,9 +10,9 @@ import java.util.EnumSet;
|
|||
/**
|
||||
* Created by gpolaert on 6/15/17.
|
||||
*/
|
||||
public class TomcatServletHelper extends DDTracingHelper<ApplicationContext> {
|
||||
public class TomcatServletHelperAgent extends DDAgentTracingHelper<ApplicationContext> {
|
||||
|
||||
public TomcatServletHelper(Rule rule) {
|
||||
public TomcatServletHelperAgent(Rule rule) {
|
||||
super(rule);
|
||||
}
|
||||
|
Loading…
Reference in New Issue