Skip to content

Commit 9582b24

Browse files
author
Guillaume Polaert
authored
Merge pull request #19 from DataDog/gpolaert/version-checker
Proposal for checking instrumentation/framework versions
2 parents faca859 + 9ee1b82 commit 9582b24

File tree

20 files changed

+593
-336
lines changed

20 files changed

+593
-336
lines changed

dd-java-agent/pom.xml

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,6 @@
141141
</dependency>
142142

143143
<!-- JMS instrumentation -->
144-
<!--<dependency>-->
145-
<!--<groupId>io.opentracing.contrib</groupId>-->
146-
<!--<artifactId>opentracing-jms-1</artifactId>-->
147-
<!--<version>${ot.contrib.jms.version}</version>-->
148-
<!--<exclusions>-->
149-
<!--<exclusion>-->
150-
<!--<groupId>javax.jms</groupId>-->
151-
<!--<artifactId>jms-api</artifactId>-->
152-
<!--</exclusion>-->
153-
<!--</exclusions>-->
154-
<!--</dependency>-->
155-
<!--<dependency>-->
156-
<!--<groupId>javax.jms</groupId>-->
157-
<!--<artifactId>jms-api</artifactId>-->
158-
<!--<version>1.1-rev-1</version>-->
159-
<!--<scope>provided</scope>-->
160-
<!--</dependency>-->
161144
<dependency>
162145
<groupId>io.opentracing.contrib</groupId>
163146
<artifactId>opentracing-jms-2</artifactId>
@@ -253,6 +236,32 @@
253236
</dependency>
254237

255238

239+
<!-- JUnit tests -->
240+
<dependency>
241+
<groupId>io.opentracing</groupId>
242+
<artifactId>opentracing-mock</artifactId>
243+
<version>${opentracing.version}</version>
244+
<scope>test</scope>
245+
</dependency>
246+
<dependency>
247+
<groupId>junit</groupId>
248+
<artifactId>junit</artifactId>
249+
<version>4.12</version>
250+
<scope>test</scope>
251+
</dependency>
252+
<dependency>
253+
<groupId>org.assertj</groupId>
254+
<artifactId>assertj-core</artifactId>
255+
<version>3.6.2</version>
256+
<scope>test</scope>
257+
</dependency>
258+
<dependency>
259+
<groupId>org.mockito</groupId>
260+
<artifactId>mockito-core</artifactId>
261+
<version>2.7.22</version>
262+
<scope>test</scope>
263+
</dependency>
264+
256265
</dependencies>
257266
<build>
258267
<finalName>${project.artifactId}</finalName>
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package io.opentracing.contrib.agent;
2+
3+
import com.datadoghq.trace.resolver.FactoryUtils;
4+
5+
import java.io.File;
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.regex.Matcher;
11+
import java.util.regex.Pattern;
12+
13+
14+
/**
15+
* Utility class to check the validity of the classpath concerning the java automated instrumentations
16+
*/
17+
public class InstrumentationChecker {
18+
19+
private static final String CONFIG_FILE = "dd-trace-supported-framework.yaml";
20+
private final Map<String, List<Map<String, String>>> rules;
21+
private final Map<String, String> frameworks;
22+
23+
private static InstrumentationChecker INSTANCE;
24+
25+
/* For testing purpose */
26+
InstrumentationChecker(Map<String, List<Map<String, String>>> rules, Map<String, String> frameworks) {
27+
this.rules = rules;
28+
this.frameworks = frameworks;
29+
INSTANCE = this;
30+
}
31+
32+
private InstrumentationChecker() {
33+
rules = FactoryUtils.loadConfigFromResource(CONFIG_FILE, Map.class);
34+
frameworks = scanLoadedLibraries();
35+
36+
}
37+
38+
/**
39+
* Return a list of unsupported rules regarding loading deps
40+
*
41+
* @return the list of unsupported rules
42+
*/
43+
public synchronized static List<String> getUnsupportedRules() {
44+
45+
if (INSTANCE == null) {
46+
INSTANCE = new InstrumentationChecker();
47+
}
48+
49+
return INSTANCE.doGetUnsupportedRules();
50+
}
51+
52+
private List<String> doGetUnsupportedRules() {
53+
54+
List<String> unsupportedRules = new ArrayList<>();
55+
for (String rule : rules.keySet()) {
56+
57+
// Check rules
58+
boolean supported = false;
59+
for (Map<String, String> check : rules.get(rule)) {
60+
if (frameworks.containsKey(check.get("artifact"))) {
61+
boolean matched = Pattern.matches(check.get("supported_version"), frameworks.get(check.get("artifact")));
62+
if (!matched) {
63+
supported = false;
64+
break;
65+
}
66+
supported = true;
67+
}
68+
}
69+
70+
if (!supported) {
71+
unsupportedRules.add(rule);
72+
}
73+
}
74+
75+
return unsupportedRules;
76+
77+
}
78+
79+
80+
private static Map<String, String> scanLoadedLibraries() {
81+
82+
Map<String, String> frameworks = new HashMap<>();
83+
84+
// Scan classpath provided jars
85+
List<File> jars = getJarFiles(System.getProperty("java.class.path"));
86+
for (File file : jars) {
87+
88+
String jarName = file.getName();
89+
String version = extractJarVersion(jarName);
90+
91+
if (version != null) {
92+
93+
// Extract artifactId
94+
String artifactId = file.getName().substring(0, jarName.indexOf(version) - 1);
95+
96+
// Store it
97+
frameworks.put(artifactId, version);
98+
}
99+
}
100+
101+
return frameworks;
102+
}
103+
104+
105+
private static List<File> getJarFiles(String paths) {
106+
List<File> filesList = new ArrayList<File>();
107+
for (final String path : paths.split(File.pathSeparator)) {
108+
final File file = new File(path);
109+
if (file.isDirectory()) {
110+
recurse(filesList, file);
111+
} else {
112+
if (file.getName().endsWith(".jar")) {
113+
filesList.add(file);
114+
}
115+
}
116+
}
117+
return filesList;
118+
}
119+
120+
private static void recurse(List<File> filesList, File f) {
121+
File list[] = f.listFiles();
122+
for (File file : list) {
123+
getJarFiles(file.getPath());
124+
}
125+
}
126+
127+
private static String extractJarVersion(String jarName) {
128+
129+
Pattern versionPattern = Pattern.compile("-(\\d+\\..+)\\.jar");
130+
Matcher matcher = versionPattern.matcher(jarName);
131+
if (matcher.find()) {
132+
return matcher.group(1);
133+
} else {
134+
return null;
135+
}
136+
}
137+
}

dd-java-agent/src/main/java/io/opentracing/contrib/agent/JarVersionsChecker.java

Lines changed: 0 additions & 119 deletions
This file was deleted.

dd-java-agent/src/main/java/io/opentracing/contrib/agent/TraceAnnotationsManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static void initialize(Retransformer trans) throws Exception {
5656
List<String> loadedScripts = loadRules(ClassLoader.getSystemClassLoader());
5757

5858
//Check if some rules have to be uninstalled
59-
List<String> uninstallScripts = new ArrayList<>();
59+
List<String> uninstallScripts = InstrumentationChecker.getUnsupportedRules();
6060
if(agentTracerConfig != null){
6161
List<String> disabledInstrumentations = agentTracerConfig.getDisabledInstrumentations();
6262
if(disabledInstrumentations!=null && !disabledInstrumentations.isEmpty()){

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ protected MongoClientOptions.Builder doPatch(MongoClientOptions.Builder builder)
2828
TracingCommandListener listener = new TracingCommandListener(tracer);
2929
builder.addCommandListener(listener);
3030

31+
setState(builder, 1);
32+
3133
return builder;
3234

3335
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
### This file define all supported libraries
2+
### Syntax:
3+
### <rulename>: # the rule name defined in the otarules.btm
4+
### - check: # a list of tests, if one not matched, thus the rule is removed
5+
### articfact: <artifactId> # the artifact name to be tested
6+
### supported_version: # a regex expression to express the version required by the rule
7+
### - check: ...
8+
9+
opentracing-apache-httpclient:
10+
- check:
11+
artifact: httpclient
12+
supported_version: 4\.[3|4|5]\..*
13+
- check:
14+
artifact: commons-httpclient
15+
supported_version: none
16+
17+
opentracing-aws-sdk:
18+
- check:
19+
artifact: aws-java-sdk
20+
supported_version: 1\.11\..*
21+
22+
opentracing-elasticsearch-client:
23+
- check:
24+
artifact: transport
25+
supported_version: 5\.4\..*
26+
27+
opentracing-cassandra-driver:
28+
- check:
29+
artifact: cassandra-driver-core
30+
supported_version: 3\.2.*
31+
32+
opentracing-web-servlet-filter:
33+
- check:
34+
artifact: jetty-server
35+
supported_version: (8\.|9\.).*
36+
- check:
37+
artifact: tomcat_catalina
38+
supported_version: (8\.|9\.).*
39+
- check:
40+
artifact: tomcat-embed-core
41+
supported_version: (8\.|9\.).*
42+
43+
opentracing-okhttp3:
44+
- check:
45+
artifact: okhttp
46+
supported_version: 3\..*
47+
48+
# For rules opentracing-jms-2_{consumer,producer}
49+
opentracing-jms-2:
50+
- check:
51+
artifact: jms-api
52+
supported_version: 2\..*
53+
54+
opentracing-mongo-driver:
55+
- check:
56+
artifact: mongo-java-driver
57+
supported_version: 3\..*
58+
- check:
59+
artifact: mongodb-driver-async
60+
supported_version: 3\..*
61+

0 commit comments

Comments
 (0)