Add more logs to debug
This commit is contained in:
parent
797332caeb
commit
78c99f4574
|
@ -18,22 +18,24 @@ package com.datadoghq.trace.agent;
|
|||
|
||||
import io.opentracing.contrib.agent.OpenTracingAgent;
|
||||
import java.lang.instrument.Instrumentation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* This class provides a wrapper around the ByteMan agent, to establish required system properties
|
||||
* and the manager class.
|
||||
*/
|
||||
@Slf4j
|
||||
public class AnnotationsTracingAgent extends OpenTracingAgent {
|
||||
|
||||
public static void premain(String agentArgs, Instrumentation inst) throws Exception {
|
||||
public static void premain(String agentArgs, final Instrumentation inst) throws Exception {
|
||||
agentArgs = addManager(agentArgs);
|
||||
|
||||
log.debug("Using premain for loading {}", AnnotationsTracingAgent.class.getSimpleName());
|
||||
org.jboss.byteman.agent.Main.premain(agentArgs, inst);
|
||||
}
|
||||
|
||||
public static void agentmain(String agentArgs, Instrumentation inst) throws Exception {
|
||||
public static void agentmain(String agentArgs, final Instrumentation inst) throws Exception {
|
||||
agentArgs = addManager(agentArgs);
|
||||
|
||||
log.debug("Using agentmain for loading {}", AnnotationsTracingAgent.class.getSimpleName());
|
||||
org.jboss.byteman.agent.Main.agentmain(agentArgs, inst);
|
||||
}
|
||||
|
||||
|
@ -44,7 +46,7 @@ public class AnnotationsTracingAgent extends OpenTracingAgent {
|
|||
agentArgs += ",";
|
||||
}
|
||||
agentArgs += "manager:" + TraceAnnotationsManager.class.getName();
|
||||
|
||||
log.debug("Agent args=: {}", agentArgs);
|
||||
return agentArgs;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,9 @@ package com.datadoghq.trace.agent;
|
|||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class DDJavaAgentInfo {
|
||||
public static final String VERSION;
|
||||
|
||||
|
@ -22,5 +24,6 @@ public class DDJavaAgentInfo {
|
|||
v = "unknown";
|
||||
}
|
||||
VERSION = v;
|
||||
log.debug("dd-java-agent - version: {}", v);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,11 +18,10 @@ import lombok.extern.slf4j.Slf4j;
|
|||
public class InstrumentationChecker {
|
||||
|
||||
private static final String CONFIG_FILE = "dd-trace-supported-framework";
|
||||
private static InstrumentationChecker INSTANCE;
|
||||
private final Map<String, List<Map<String, String>>> rules;
|
||||
private final Map<String, String> frameworks;
|
||||
|
||||
private static InstrumentationChecker INSTANCE;
|
||||
|
||||
/* For testing purpose */
|
||||
InstrumentationChecker(
|
||||
final Map<String, List<Map<String, String>>> rules, final Map<String, String> frameworks) {
|
||||
|
@ -50,6 +49,65 @@ public class InstrumentationChecker {
|
|||
return INSTANCE.doGetUnsupportedRules();
|
||||
}
|
||||
|
||||
private static Map<String, String> scanLoadedLibraries() {
|
||||
|
||||
final Map<String, String> frameworks = new HashMap<>();
|
||||
|
||||
// Scan classpath provided jars
|
||||
final List<File> jars = getJarFiles(System.getProperty("java.class.path"));
|
||||
for (final File file : jars) {
|
||||
|
||||
final String jarName = file.getName();
|
||||
final String version = extractJarVersion(jarName);
|
||||
|
||||
if (version != null) {
|
||||
|
||||
// Extract artifactId
|
||||
final String artifactId = file.getName().substring(0, jarName.indexOf(version) - 1);
|
||||
|
||||
// Store it
|
||||
frameworks.put(artifactId, version);
|
||||
}
|
||||
}
|
||||
log.debug("{} libraries found in the class-path", frameworks.size());
|
||||
|
||||
return frameworks;
|
||||
}
|
||||
|
||||
private static List<File> getJarFiles(final String paths) {
|
||||
final List<File> filesList = new ArrayList<>();
|
||||
for (final String path : paths.split(File.pathSeparator)) {
|
||||
final File file = new File(path);
|
||||
if (file.isDirectory()) {
|
||||
recurse(filesList, file);
|
||||
} else {
|
||||
if (file.getName().endsWith(".jar")) {
|
||||
log.trace("{} found in the classpath", file.getName());
|
||||
filesList.add(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
return filesList;
|
||||
}
|
||||
|
||||
private static void recurse(final List<File> filesList, final File f) {
|
||||
final File[] list = f.listFiles();
|
||||
for (final File file : list) {
|
||||
getJarFiles(file.getPath());
|
||||
}
|
||||
}
|
||||
|
||||
private static String extractJarVersion(final String jarName) {
|
||||
|
||||
final Pattern versionPattern = Pattern.compile("-(\\d+\\..+)\\.jar");
|
||||
final Matcher matcher = versionPattern.matcher(jarName);
|
||||
if (matcher.find()) {
|
||||
return matcher.group(1);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> doGetUnsupportedRules() {
|
||||
|
||||
final List<String> unsupportedRules = new ArrayList<>();
|
||||
|
@ -83,61 +141,4 @@ public class InstrumentationChecker {
|
|||
|
||||
return unsupportedRules;
|
||||
}
|
||||
|
||||
private static Map<String, String> scanLoadedLibraries() {
|
||||
|
||||
final Map<String, String> frameworks = new HashMap<>();
|
||||
|
||||
// Scan classpath provided jars
|
||||
final List<File> jars = getJarFiles(System.getProperty("java.class.path"));
|
||||
for (final File file : jars) {
|
||||
|
||||
final String jarName = file.getName();
|
||||
final String version = extractJarVersion(jarName);
|
||||
|
||||
if (version != null) {
|
||||
|
||||
// Extract artifactId
|
||||
final String artifactId = file.getName().substring(0, jarName.indexOf(version) - 1);
|
||||
|
||||
// Store it
|
||||
frameworks.put(artifactId, version);
|
||||
}
|
||||
}
|
||||
|
||||
return frameworks;
|
||||
}
|
||||
|
||||
private static List<File> getJarFiles(final String paths) {
|
||||
final List<File> filesList = new ArrayList<>();
|
||||
for (final String path : paths.split(File.pathSeparator)) {
|
||||
final File file = new File(path);
|
||||
if (file.isDirectory()) {
|
||||
recurse(filesList, file);
|
||||
} else {
|
||||
if (file.getName().endsWith(".jar")) {
|
||||
filesList.add(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
return filesList;
|
||||
}
|
||||
|
||||
private static void recurse(final List<File> filesList, final File f) {
|
||||
final File[] list = f.listFiles();
|
||||
for (final File file : list) {
|
||||
getJarFiles(file.getPath());
|
||||
}
|
||||
}
|
||||
|
||||
private static String extractJarVersion(final String jarName) {
|
||||
|
||||
final Pattern versionPattern = Pattern.compile("-(\\d+\\..+)\\.jar");
|
||||
final Matcher matcher = versionPattern.matcher(jarName);
|
||||
if (matcher.find()) {
|
||||
return matcher.group(1);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,12 @@ import java.lang.reflect.Method;
|
|||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import javassist.ClassPool;
|
||||
|
@ -37,9 +42,20 @@ import org.reflections.util.FilterBuilder;
|
|||
@Slf4j
|
||||
public class TraceAnnotationsManager {
|
||||
|
||||
private static Retransformer transformer;
|
||||
|
||||
private static final String AGENT_RULES = "otarules.btm";
|
||||
private static final String CURRENT_SPAN_EXISTS = "IF TRUE\n";
|
||||
private static final String BUILD_SPAN = "DO\n" + "getTracer().buildSpan(\"";
|
||||
private static final String CLOSE_PARENTHESIS = "\")";
|
||||
private static final String START = ".startActive();";
|
||||
private static final String EXIT_RULE =
|
||||
"IF getTracer().activeSpan() != null\n" + "DO\n" + "getTracer().activeSpan().deactivate();\n";
|
||||
private static final String EXCEPTION_EXIT_RULE =
|
||||
"BIND span:io.opentracing.ActiveSpan = getTracer().activeSpan()\n"
|
||||
+ "IF span != null\n"
|
||||
+ "DO\n"
|
||||
+ "span.setTag(io.opentracing.tag.Tags.ERROR.getKey(),\"true\");\n"
|
||||
+ "span.deactivate();\n";
|
||||
private static Retransformer transformer;
|
||||
|
||||
/**
|
||||
* This method initializes the manager.
|
||||
|
@ -47,6 +63,7 @@ public class TraceAnnotationsManager {
|
|||
* @param trans The ByteMan retransformer
|
||||
*/
|
||||
public static void initialize(final Retransformer trans) throws Exception {
|
||||
log.debug("Initializing {}", TraceAnnotationsManager.class.getSimpleName());
|
||||
transformer = trans;
|
||||
//Load configuration
|
||||
final AgentTracerConfig agentTracerConfig =
|
||||
|
@ -55,6 +72,8 @@ public class TraceAnnotationsManager {
|
|||
DDTracerFactory.CONFIG_PATH,
|
||||
AgentTracerConfig.class);
|
||||
|
||||
log.debug("Configuration: {}", agentTracerConfig.toString());
|
||||
|
||||
final List<String> loadedScripts = loadRules(ClassLoader.getSystemClassLoader());
|
||||
|
||||
//Check if some rules have to be uninstalled
|
||||
|
@ -100,7 +119,7 @@ public class TraceAnnotationsManager {
|
|||
try (PrintWriter pr = new PrintWriter(sw)) {
|
||||
transformer.removeScripts(new ArrayList<>(rulesToRemove), pr);
|
||||
}
|
||||
log.info(sw.toString());
|
||||
log.info("Uninstall rule scripts: {}", rulesToRemove.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -140,6 +159,11 @@ public class TraceAnnotationsManager {
|
|||
}
|
||||
|
||||
log.debug("OpenTracing Agent rules loaded");
|
||||
if (log.isTraceEnabled()) {
|
||||
for (final String rule : scripts) {
|
||||
log.trace("Loading rule: {}", rule);
|
||||
}
|
||||
}
|
||||
return scripts;
|
||||
}
|
||||
|
||||
|
@ -202,6 +226,7 @@ public class TraceAnnotationsManager {
|
|||
log.warn(
|
||||
"Could not create rule for method " + method + ". Proceed to next annoted method.", e);
|
||||
}
|
||||
log.trace("Instrumenting annotated method: {}", method.getName());
|
||||
}
|
||||
try {
|
||||
final StringWriter sw = new StringWriter();
|
||||
|
@ -209,7 +234,7 @@ public class TraceAnnotationsManager {
|
|||
transformer.installScript(
|
||||
Arrays.asList(generatedScripts.toString()), Arrays.asList("@Trace annotations"), pr);
|
||||
}
|
||||
log.debug(sw.toString());
|
||||
log.trace("Install new rules: \n{}", sw.toString());
|
||||
} catch (final Exception e) {
|
||||
log.warn("Could not install annotation scripts.", e);
|
||||
}
|
||||
|
@ -264,23 +289,6 @@ public class TraceAnnotationsManager {
|
|||
scriptNames.add(uri.toString());
|
||||
}
|
||||
|
||||
private static final String CURRENT_SPAN_EXISTS = "IF TRUE\n";
|
||||
|
||||
private static final String BUILD_SPAN = "DO\n" + "getTracer().buildSpan(\"";
|
||||
private static final String CLOSE_PARENTHESIS = "\")";
|
||||
|
||||
private static final String START = ".startActive();";
|
||||
|
||||
private static final String EXIT_RULE =
|
||||
"IF getTracer().activeSpan() != null\n" + "DO\n" + "getTracer().activeSpan().deactivate();\n";
|
||||
|
||||
private static final String EXCEPTION_EXIT_RULE =
|
||||
"BIND span:io.opentracing.ActiveSpan = getTracer().activeSpan()\n"
|
||||
+ "IF span != null\n"
|
||||
+ "DO\n"
|
||||
+ "span.setTag(io.opentracing.tag.Tags.ERROR.getKey(),\"true\");\n"
|
||||
+ "span.deactivate();\n";
|
||||
|
||||
private static String buildSpan(final CtMethod javassistMethod) {
|
||||
try {
|
||||
final Trace trace = (Trace) javassistMethod.getAnnotation(Trace.class);
|
||||
|
|
|
@ -4,5 +4,6 @@ apply from: "${rootDir}/gradle/jacoco.gradle"
|
|||
|
||||
description = 'dd-trace-annotations'
|
||||
dependencies {
|
||||
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
|
||||
testCompile group: 'junit', name: 'junit', version: '3.8.1'
|
||||
}
|
||||
|
|
|
@ -2,7 +2,9 @@ package com.datadoghq.trace;
|
|||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class DDTraceAnnotationsInfo {
|
||||
public static final String VERSION;
|
||||
|
||||
|
@ -23,5 +25,6 @@ public class DDTraceAnnotationsInfo {
|
|||
v = "unknown";
|
||||
}
|
||||
VERSION = v;
|
||||
log.debug("dd-trace-annotations - version: {}", v);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,9 @@ package com.datadoghq.trace;
|
|||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class DDTraceInfo {
|
||||
|
||||
public static final String JAVA_VERSION = System.getProperty("java.version", "unknown");
|
||||
|
@ -26,5 +28,6 @@ public class DDTraceInfo {
|
|||
v = "unknown";
|
||||
}
|
||||
VERSION = v;
|
||||
log.debug("dd-trace - version: {}", v);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,6 +65,11 @@ public class DDTracer extends ThreadLocalActiveSpanSource implements io.opentrac
|
|||
this.sampler = sampler;
|
||||
registry = new CodecRegistry();
|
||||
registry.register(Format.Builtin.HTTP_HEADERS, new HTTPCodec());
|
||||
log.debug(
|
||||
"New tracer instance, default-service={}, writer={}, sampler={}",
|
||||
defaultServiceName,
|
||||
writer.getClass().getSimpleName(),
|
||||
sampler.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -94,16 +94,20 @@ public class DDTracerFactory {
|
|||
SYSTEM_PROPERTY_CONFIG_PATH, CONFIG_PATH, TracerConfig.class);
|
||||
|
||||
DDTracer tracer = null;
|
||||
log.trace("Tracer configuration: \n{}", tracerConfig);
|
||||
if (tracerConfig == null) {
|
||||
log.info("No valid configuration file {} found. Loading default tracer.", CONFIG_PATH);
|
||||
tracer = new DDTracer();
|
||||
} else {
|
||||
log.debug("Create a tracer instance from the configuration");
|
||||
|
||||
tracer = DDTracerFactory.create(tracerConfig);
|
||||
}
|
||||
|
||||
//Create decorators from resource files
|
||||
final List<AbstractDecorator> decorators = DDDecoratorsFactory.createFromResources();
|
||||
for (final AbstractDecorator decorator : decorators) {
|
||||
log.debug("Loading decorator: {}", decorator.getClass().getSimpleName());
|
||||
tracer.addDecorator(decorator);
|
||||
}
|
||||
|
||||
|
|
|
@ -12,9 +12,22 @@ import lombok.extern.slf4j.Slf4j;
|
|||
@AutoService(TracerResolver.class)
|
||||
public class DDTracerResolver extends TracerResolver {
|
||||
|
||||
public static Tracer registerTracer() {
|
||||
final Tracer tracer = TracerResolver.resolveTracer();
|
||||
|
||||
if (tracer == null) {
|
||||
log.warn("Cannot resolved the tracer, use NoopTracer");
|
||||
return NoopTracerFactory.create();
|
||||
}
|
||||
|
||||
log.info("Register the tracer via GlobalTracer");
|
||||
GlobalTracer.register(tracer);
|
||||
return tracer;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Tracer resolve() {
|
||||
log.info("Creating the Datadog tracer");
|
||||
log.info("Creating the Datadog Tracer from the resolver");
|
||||
|
||||
//Find a resource file named dd-trace.yml
|
||||
DDTracer tracer = null;
|
||||
|
@ -23,15 +36,4 @@ public class DDTracerResolver extends TracerResolver {
|
|||
|
||||
return tracer;
|
||||
}
|
||||
|
||||
public static Tracer registerTracer() {
|
||||
final Tracer tracer = TracerResolver.resolveTracer();
|
||||
|
||||
if (tracer == null) {
|
||||
return NoopTracerFactory.create();
|
||||
}
|
||||
|
||||
GlobalTracer.register(tracer);
|
||||
return tracer;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue