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
22 changes: 22 additions & 0 deletions dd-java-agent-ittests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,28 @@
<scope>test</scope>
</dependency>


<!--JMS -->
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.activemq.tooling</groupId>
<artifactId>activemq-junit</artifactId>
<version>5.14.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
<version>5.14.5</version>
<scope>test</scope>
</dependency>


</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.datadoghq.trace.instrument;

import io.opentracing.contrib.jms.TracingMessageProducer;
import io.opentracing.contrib.jms.common.TracingMessageConsumer;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.junit.EmbeddedActiveMQBroker;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;

import javax.jms.*;

import static org.assertj.core.api.Assertions.assertThat;

public class JMSInstrumentationTest {


@ClassRule
public static EmbeddedActiveMQBroker broker = new EmbeddedActiveMQBroker();
private static Session session;
private static ActiveMQQueue destination;

@BeforeClass
public static void start() throws JMSException {

broker.start();
ActiveMQConnectionFactory connectionFactory = broker.createConnectionFactory();

destination = new ActiveMQQueue("someQueue");
Connection connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

}

@Test
public void test() throws Exception {


MessageProducer producer = session.createProducer(destination);
MessageConsumer consumer = session.createConsumer(destination);

assertThat(producer).isInstanceOf(TracingMessageProducer.class);
assertThat(consumer).isInstanceOf(TracingMessageConsumer.class);
}

}
46 changes: 46 additions & 0 deletions dd-java-agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<ot.contrib.httpclient.version>0.0.2</ot.contrib.httpclient.version>
<ot.contrib.cassandra.version>0.0.2</ot.contrib.cassandra.version>
<ot.contrib.elasticsearch.version>0.0.2</ot.contrib.elasticsearch.version>
<ot.contrib.jms.version>0.0.3</ot.contrib.jms.version>
<!-- Others versions -->
<java.version>1.7</java.version>
</properties>
Expand Down Expand Up @@ -139,6 +140,25 @@
<scope>provided</scope>
</dependency>

<!-- JMS instrumentation -->
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-jms-2</artifactId>
<version>${ot.contrib.jms.version}</version>
<exclusions>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
<scope>provided</scope>
</dependency>

<!-- AWS SDK instrumentation -->
<dependency>
<groupId>io.opentracing.contrib</groupId>
Expand Down Expand Up @@ -216,6 +236,32 @@
</dependency>


<!-- JUnit tests -->
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-mock</artifactId>
<version>${opentracing.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.7.22</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package io.opentracing.contrib.agent;

import com.datadoghq.trace.resolver.FactoryUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
* Utility class to check the validity of the classpath concerning the java automated instrumentations
*/
public class InstrumentationChecker {

private static final String CONFIG_FILE = "dd-trace-supported-framework.yaml";
private final Map<String, List<Map<String, String>>> rules;
private final Map<String, String> frameworks;

private static InstrumentationChecker INSTANCE;

/* For testing purpose */
InstrumentationChecker(Map<String, List<Map<String, String>>> rules, Map<String, String> frameworks) {
this.rules = rules;
this.frameworks = frameworks;
INSTANCE = this;
}

private InstrumentationChecker() {
rules = FactoryUtils.loadConfigFromResource(CONFIG_FILE, Map.class);
frameworks = scanLoadedLibraries();

}

/**
* Return a list of unsupported rules regarding loading deps
*
* @return the list of unsupported rules
*/
public synchronized static List<String> getUnsupportedRules() {

if (INSTANCE == null) {
INSTANCE = new InstrumentationChecker();
}

return INSTANCE.doGetUnsupportedRules();
}

private List<String> doGetUnsupportedRules() {

List<String> unsupportedRules = new ArrayList<>();
for (String rule : rules.keySet()) {

// Check rules
boolean supported = false;
for (Map<String, String> check : rules.get(rule)) {
if (frameworks.containsKey(check.get("artifact"))) {
boolean matched = Pattern.matches(check.get("supported_version"), frameworks.get(check.get("artifact")));
if (!matched) {
supported = false;
break;
}
supported = true;
}
}

if (!supported) {
unsupportedRules.add(rule);
}
}

return unsupportedRules;

}


private static Map<String, String> scanLoadedLibraries() {

Map<String, String> frameworks = new HashMap<>();

// Scan classpath provided jars
List<File> jars = getJarFiles(System.getProperty("java.class.path"));
for (File file : jars) {

String jarName = file.getName();
String version = extractJarVersion(jarName);

if (version != null) {

// Extract artifactId
String artifactId = file.getName().substring(0, jarName.indexOf(version) - 1);

// Store it
frameworks.put(artifactId, version);
}
}

return frameworks;
}


private static List<File> getJarFiles(String paths) {
List<File> filesList = new ArrayList<File>();
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(List<File> filesList, File f) {
File list[] = f.listFiles();
for (File file : list) {
getJarFiles(file.getPath());
}
}

private static String extractJarVersion(String jarName) {

Pattern versionPattern = Pattern.compile("-(\\d+\\..+)\\.jar");
Matcher matcher = versionPattern.matcher(jarName);
if (matcher.find()) {
return matcher.group(1);
} else {
return null;
}
}
}
Loading