Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions agent/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ apply from: "$buildScriptsDir/common-java.gradle"
shadowJar {
classifier = ''
relocate 'org.objectweb.asm', 'com.microsoft.applicationinsights.agent.dependencies.asm'
relocate 'org.apache.commons', 'com.microsoft.applicationinsights.agent.dependencies.apachecommons'
}
archivesBaseName = 'applicationinsights-agent'

Expand All @@ -47,6 +48,7 @@ jar {
dependencies {
compile group: 'org.ow2.asm', name: 'asm-commons', version: '5.2'
compile group: 'org.ow2.asm', name: 'asm-all', version: '5.2'
compile group: 'org.apache.commons', name:'commons-lang3', version:'3.7'
testCompile group: 'commons-io', name: 'commons-io', version: '2.6'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.microsoft.applicationinsights.agent.internal.config.DataOfConfigurationForException;
import com.microsoft.applicationinsights.agent.internal.coresync.impl.ImplementationsCoordinator;
import com.microsoft.applicationinsights.agent.internal.logger.InternalAgentLogger;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -63,13 +64,15 @@ public static void premain(String args, Instrumentation inst) {
throw td;
} catch (Throwable throwable) {
try {
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, "Agent is NOT activated: failed to load to bootstrap class loader: %s", throwable.toString()); throwable.printStackTrace();
InternalAgentLogger.INSTANCE.error("Agent is NOT activated: failed to load to bootstrap class loader: %s",
ExceptionUtils.getStackTrace(throwable));
System.exit(-1);
} catch (ThreadDeath td) {
throw td;
} catch (Throwable t) {
// chomp
}

}
}

Expand All @@ -93,21 +96,21 @@ private static void initializeCodeInjector(Instrumentation inst) throws Throwabl
DataOfConfigurationForException exceptionData = agentConfiguration.getBuiltInConfiguration().getDataOfConfigurationForException();
if (inst.isRetransformClassesSupported()) {
if (exceptionData.isEnabled()) {
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.TRACE, "Instrumenting runtime exceptions.");
InternalAgentLogger.INSTANCE.trace("Instrumenting runtime exceptions.");

inst.addTransformer(codeInjector, true);
ImplementationsCoordinator.INSTANCE.setExceptionData(exceptionData);
inst.retransformClasses(RuntimeException.class);
inst.removeTransformer(codeInjector);
}
} else {
} else {
if (exceptionData.isEnabled()) {
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.TRACE, "The JVM does not support re-transformation of classes.");
InternalAgentLogger.INSTANCE.trace("The JVM does not support re-transformation of classes.");
}
}
}
inst.addTransformer(codeInjector);
} catch (Exception e) {
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, "Failed to load the code injector, exception: %s", e.toString());
InternalAgentLogger.INSTANCE.error("Failed to load the code injector, exception: %s", ExceptionUtils.getStackTrace(e));
throw e;
}
}
Expand All @@ -120,13 +123,13 @@ private static void appendJarsToBootstrapClassLoader(Instrumentation inst) throw
for (File file : agentFolder.listFiles()) {
if (file.getName().indexOf(AGENT_JAR_PREFIX) != -1) {
agentJarName = file.getName();
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.INFO,"Agent jar name is %s", agentJarName);
InternalAgentLogger.INSTANCE.info("Agent jar name is %s", agentJarName);
break;
}
}

if (agentJarName == null) {
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR,"Agent Jar Name is null....Throwing runtime exception");
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, "Agent Jar Name is null....Throwing runtime exception");
throw new RuntimeException("Could not find agent jar");
}

Expand All @@ -138,7 +141,7 @@ private static void appendJarsToBootstrapClassLoader(Instrumentation inst) throw

inst.appendToBootstrapClassLoaderSearch(agentJar);

InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.TRACE, "Successfully loaded Agent jar");
InternalAgentLogger.INSTANCE.trace("Successfully loaded Agent jar");
}

public static String getAgentJarLocation() throws UnsupportedEncodingException {
Expand All @@ -149,7 +152,7 @@ public static String getAgentJarLocation() throws UnsupportedEncodingException {
String urlPath = url.getPath();

if (urlPath.indexOf(AGENT_JAR_PREFIX) != -1) {
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.INFO,"Agent jar found at %s", urlPath);
InternalAgentLogger.INSTANCE.info("Agent jar found at %s", urlPath);
int index = urlPath.lastIndexOf('/');
urlPath = urlPath.substring(0, index + 1);
return urlPath;
Expand All @@ -160,7 +163,7 @@ public static String getAgentJarLocation() throws UnsupportedEncodingException {
} catch (ThreadDeath td) {
throw td;
} catch (Throwable throwable) {
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, "Error while trying to fetch Jar Location, Exception: %s", throwable.toString());
InternalAgentLogger.INSTANCE.error("Error while trying to fetch Jar Location, Exception: %s", ExceptionUtils.getStackTrace(throwable));
}

String stringPath = AgentImplementation.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Expand All @@ -175,76 +178,76 @@ private static void SetNonWebAppModeIfAskedByConf(String sdkPath) throws Throwab
File sdkFolder = new File(path);
if (!sdkFolder.exists()) {
String errorMessage = String.format("Path %s for core jar does not exist", path);
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, errorMessage);
InternalAgentLogger.INSTANCE.error(errorMessage);
throw new Exception(errorMessage);
}

if (!sdkFolder.isDirectory()) {
String errorMessage = String.format("Path %s for core jar must be a folder", path);
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, errorMessage);
InternalAgentLogger.INSTANCE.error(errorMessage);
throw new Exception(errorMessage);
}

if (!sdkFolder.canRead()) {
String errorMessage = String.format("Path %s for core jar must be a folder that can be read", path);
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, errorMessage);
InternalAgentLogger.INSTANCE.error(errorMessage);
throw new Exception(errorMessage);
}

InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.TRACE, "Found %s", path);
InternalAgentLogger.INSTANCE.trace("Found %s", path);
String coreJarName = null;
for (File file : sdkFolder.listFiles()) {
if (file.getName().indexOf(CORE_JAR_PREFIX) != -1 || file.getName().indexOf(DISTRIBUTION_JAR_PREFIX) != -1) {
coreJarName = file.getAbsolutePath();
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.TRACE, "Found core jar: %s", coreJarName);
InternalAgentLogger.INSTANCE.trace("Found core jar: %s", coreJarName);
break;
}
}

if (coreJarName == null) {
String errorMessage = String.format("Did not find core jar in path %s", path);
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, errorMessage);
InternalAgentLogger.INSTANCE.error(errorMessage);
throw new Exception(errorMessage);
}

InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.TRACE, "Found jar: %s", coreJarName);
InternalAgentLogger.INSTANCE.trace("Found jar: %s", coreJarName);

JarFile jarFile = null;
try {
jarFile = new JarFile(coreJarName);
} catch (IOException e) {
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, "Could not load jar: %s", coreJarName);
InternalAgentLogger.INSTANCE.error("Could not load jar: %s", coreJarName);
throw e;
}
Enumeration<JarEntry> e = jarFile.entries();

URL[] urls = { new URL("jar:file:" + coreJarName+"!/") };
URL[] urls = {new URL("jar:file:" + coreJarName + "!/")};
URLClassLoader cl = URLClassLoader.newInstance(urls);

while (e.hasMoreElements()) {
JarEntry je = e.nextElement();
if(je.isDirectory() || !je.getName().endsWith(".class")){
if (je.isDirectory() || !je.getName().endsWith(".class")) {
continue;
}
try {
Class clazz = cl.loadClass(CORE_SELF_REGISTRATOR_CLASS_NAME);
clazz.getDeclaredConstructor().newInstance();
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.TRACE, "Loaded core jar");
InternalAgentLogger.INSTANCE.trace("Loaded core jar");
break;
} catch (ClassNotFoundException e1) {
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, "Could not load class: %s, ClassNotFoundException", CORE_SELF_SHORT_REGISTRATOR_CLASS_NAME);
InternalAgentLogger.INSTANCE.error("Could not load class: %s, ClassNotFoundException", CORE_SELF_SHORT_REGISTRATOR_CLASS_NAME);
throw e1;
} catch (InvocationTargetException e1) {
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, "Could not load class: %s, InvocationTargetException", CORE_SELF_SHORT_REGISTRATOR_CLASS_NAME);
InternalAgentLogger.INSTANCE.error("Could not load class: %s, InvocationTargetException", CORE_SELF_SHORT_REGISTRATOR_CLASS_NAME);
throw e1;
} catch (NoSuchMethodException e1) {
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, "Could not load class: %s, NoSuchMethodException", CORE_SELF_SHORT_REGISTRATOR_CLASS_NAME);
InternalAgentLogger.INSTANCE.error("Could not load class: %s, NoSuchMethodException", CORE_SELF_SHORT_REGISTRATOR_CLASS_NAME);
throw e1;
} catch (InstantiationException e1) {
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, "Could not load class: %s, InstantiationException", CORE_SELF_SHORT_REGISTRATOR_CLASS_NAME);
InternalAgentLogger.INSTANCE.error("Could not load class: %s, InstantiationException", CORE_SELF_SHORT_REGISTRATOR_CLASS_NAME);
throw e1;
} catch (IllegalAccessException e1) {
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, "Could not load class: %s, IllegalAccessException", CORE_SELF_SHORT_REGISTRATOR_CLASS_NAME);
InternalAgentLogger.INSTANCE.error("Could not load class: %s, IllegalAccessException", CORE_SELF_SHORT_REGISTRATOR_CLASS_NAME);
throw e1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
import com.microsoft.applicationinsights.agent.internal.config.AgentConfigurationBuilderFactory;
import com.microsoft.applicationinsights.agent.internal.coresync.impl.ImplementationsCoordinator;
import com.microsoft.applicationinsights.agent.internal.logger.InternalAgentLogger;
import org.apache.commons.lang3.exception.ExceptionUtils;

/**
* The class is responsible for finding needed classes
*
* <p>
* Created by gupele on 5/11/2015.
*/
public final class CodeInjector implements ClassFileTransformer {
Expand All @@ -45,19 +46,19 @@ public final class CodeInjector implements ClassFileTransformer {
/**
* The constructor will set all the data needed for the transformation
*
* @param agentConfiguration The configuration
* @param agentConfiguration The configuration
*/
public CodeInjector(AgentConfiguration agentConfiguration) {
try {
loadConfiguration(agentConfiguration);
InternalAgentLogger.INSTANCE.info("Agent is up");

InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.INFO, "Agent is up");
} catch (ThreadDeath td) {
throw td;
} catch (Throwable throwable) {
try {
throwable.printStackTrace();
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.INFO, "Agent is NOT activated: failed to initialize CodeInjector: '%s'", throwable.toString());
InternalAgentLogger.INSTANCE.error("Agent is NOT activated: failed to initialize CodeInjector: '%s'",
ExceptionUtils.getStackTrace(throwable));
} catch (ThreadDeath td) {
throw td;
} catch (Throwable t2) {
Expand All @@ -68,11 +69,12 @@ public CodeInjector(AgentConfiguration agentConfiguration) {

/**
* Main method that transforms classes
* @param loader The class loader that loaded this class
* @param className The class name
*
* @param loader The class loader that loaded this class
* @param className The class name
* @param classBeingRedefined The class that is being redefined
* @param protectionDomain The protection domain
* @param originalBuffer The class that was loaded before transforming it
* @param protectionDomain The protection domain
* @param originalBuffer The class that was loaded before transforming it
* @return A byte array that contains the transformed original class or the original one if nothing was done.
* @throws IllegalClassFormatException Theoretical, since the following implementation won't throw.
*/
Expand All @@ -91,8 +93,8 @@ public byte[] transform(
throw td;
} catch (Throwable throwable) {
try {
throwable.printStackTrace();
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, "Failed to instrument '%s', exception: '%s': ", className, throwable.toString());
InternalAgentLogger.INSTANCE.error("Failed to instrument '%s', " +
"exception: '%s'", className, ExceptionUtils.getStackTrace(throwable));
} catch (ThreadDeath td) {
throw td;
} catch (Throwable t2) {
Expand All @@ -107,6 +109,7 @@ public byte[] transform(
/**
* The method will try to load the configuration file for the Agent. The file is optional but
* is assumed to be located 'near' the agent jar. Failing to put the file there will cause the file not to be loaded
*
* @param agentConfiguration The configuration
*/
private void loadConfiguration(AgentConfiguration agentConfiguration) {
Expand All @@ -115,9 +118,9 @@ private void loadConfiguration(AgentConfiguration agentConfiguration) {

if (agentConfiguration.getBuiltInConfiguration().isJmxEnabled()) {
jmxConnectorLoader = new JmxConnectorLoader();
if (!jmxConnectorLoader.initialize()) {
jmxConnectorLoader = null;
}
if (!jmxConnectorLoader.initialize()) {
jmxConnectorLoader = null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.microsoft.applicationinsights.agent.internal.agent.*;
import com.microsoft.applicationinsights.agent.internal.coresync.InstrumentedClassType;
import com.microsoft.applicationinsights.agent.internal.logger.InternalAgentLogger;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.objectweb.asm.MethodVisitor;

import java.util.Map;
Expand Down Expand Up @@ -58,10 +59,11 @@ public MethodVisitor create(MethodInstrumentationDecision decision, int access,

classesToInstrument.put(RUNTIME_EXCEPTION_CLASS_NAME, data);
} catch (ThreadDeath td) {
throw td;
throw td;
} catch (Throwable t) {
try {
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, "Failed to load instrumentation for Jedis: '%s'", t.toString()); } catch (ThreadDeath td) {
InternalAgentLogger.INSTANCE.error("Failed to load instrumentation for Jedis: '%s'", ExceptionUtils.getStackTrace(t));
} catch (ThreadDeath td) {
throw td;
} catch (Throwable t2) {
// chomp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package com.microsoft.applicationinsights.agent.internal.agent.redis;

import com.microsoft.applicationinsights.agent.internal.logger.InternalAgentLogger;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.objectweb.asm.MethodVisitor;

import java.util.Map;
Expand All @@ -31,6 +32,7 @@
import com.microsoft.applicationinsights.agent.internal.agent.MethodInstrumentationDecision;
import com.microsoft.applicationinsights.agent.internal.agent.MethodVisitorFactory;
import com.microsoft.applicationinsights.agent.internal.coresync.InstrumentedClassType;

/**
* Created by gupele on 8/6/2015.
*/
Expand Down Expand Up @@ -59,10 +61,11 @@ public MethodVisitor create(MethodInstrumentationDecision decision, int access,

classesToInstrument.put(JEDIS_CLASS_NAME, data);
} catch (ThreadDeath td) {
throw td;
throw td;
} catch (Throwable t) {
try {
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, "Failed to load instrumentation for Jedis: '%s'", t.toString()); } catch (ThreadDeath td) {
InternalAgentLogger.INSTANCE.error("Failed to load instrumentation for Jedis: '%s'", ExceptionUtils.getStackTrace(t));
} catch (ThreadDeath td) {
throw td;
} catch (Throwable t2) {
// chomp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.microsoft.applicationinsights.agent.internal.common.StringUtils;
import com.microsoft.applicationinsights.agent.internal.logger.InternalAgentLogger;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand Down Expand Up @@ -103,7 +104,8 @@ private void FetchStackSize(Element rtExceptionElement, DataOfConfigurationForEx
int maxStackSize = Integer.parseInt(preparedValue);
stackSize = maxStackSize;
} catch (Exception e) {
InternalAgentLogger.INSTANCE.logAlways(InternalAgentLogger.LoggingLevel.ERROR, "Failed to parse attribute %s with value %s, will send full stack", MAX_STACK_SIZE, maxStackSizeAsString);
InternalAgentLogger.INSTANCE.error("Failed to parse attribute %s with value %s, will send full stack" +
"exception : %s", MAX_STACK_SIZE, maxStackSizeAsString, ExceptionUtils.getStackTrace(e));
}
}

Expand Down
Loading