Skip to content

Commit c7676bd

Browse files
authored
Parse latestDepTestLibrary and use as library upper bound (#13478)
1 parent ffeb80e commit c7676bd

File tree

4 files changed

+72
-32
lines changed

4 files changed

+72
-32
lines changed

docs/instrumentation-list.yaml

+14-11
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ apache:
5858
srcPath: instrumentation/apache-httpclient/apache-httpclient-4.3
5959
target_versions:
6060
library:
61-
- org.apache.httpcomponents:httpclient:4.3
61+
- org.apache.httpcomponents:httpclient:[4.3,4.+)
6262
- name: apache-httpclient-4.0
6363
srcPath: instrumentation/apache-httpclient/apache-httpclient-4.0
6464
target_versions:
@@ -137,7 +137,7 @@ aws:
137137
- com.amazonaws:aws-java-sdk-sqs:[1.10.33,)
138138
- com.amazonaws:aws-java-sdk-core:[1.10.33,)
139139
library:
140-
- com.amazonaws:aws-java-sdk-sqs:1.11.106
140+
- com.amazonaws:aws-java-sdk-sqs:[1.11.106,1.12.583)
141141
- com.amazonaws:aws-java-sdk-core:1.11.0
142142
- name: aws-sdk-2.2
143143
srcPath: instrumentation/aws-sdk/aws-sdk-2.2
@@ -352,7 +352,7 @@ graphql:
352352
javaagent:
353353
- com.graphql-java:graphql-java:[12,20)
354354
library:
355-
- com.graphql-java:graphql-java:12.0
355+
- com.graphql-java:graphql-java:[12.0,19.+)
356356
- name: graphql-java-20.0
357357
srcPath: instrumentation/graphql-java/graphql-java-20.0
358358
target_versions:
@@ -651,7 +651,7 @@ jetty:
651651
javaagent:
652652
- org.eclipse.jetty:jetty-client:[9.2,10)
653653
library:
654-
- org.eclipse.jetty:jetty-client:9.2.0.v20140526
654+
- org.eclipse.jetty:jetty-client:[9.2.0.v20140526,9.+)
655655
- name: jetty-11.0
656656
srcPath: instrumentation/jetty/jetty-11.0
657657
target_versions:
@@ -765,8 +765,8 @@ ktor:
765765
- io.ktor:ktor-client-core:[2.0.0,3.0.0)
766766
- io.ktor:ktor-server-core:[2.0.0,3.0.0)
767767
library:
768-
- io.ktor:ktor-server-core:2.0.0
769-
- io.ktor:ktor-client-core:2.0.0
768+
- io.ktor:ktor-client-core:[2.0.0,2.+)
769+
- io.ktor:ktor-server-core:[2.0.0,2.+)
770770
- name: ktor-3.0
771771
srcPath: instrumentation/ktor/ktor-3.0
772772
target_versions:
@@ -780,7 +780,7 @@ ktor:
780780
srcPath: instrumentation/ktor/ktor-1.0
781781
target_versions:
782782
library:
783-
- io.ktor:ktor-server-core:1.0.0
783+
- io.ktor:ktor-server-core:[1.0.0,1.+)
784784
kubernetes:
785785
instrumentations:
786786
- name: kubernetes-client-7.0
@@ -1049,7 +1049,7 @@ oshi:
10491049
javaagent:
10501050
- com.github.oshi:oshi-core:[5.3.1,)
10511051
library:
1052-
- com.github.oshi:oshi-core:$oshiVersion
1052+
- com.github.oshi:oshi-core:5.3.1
10531053
payara:
10541054
instrumentations:
10551055
- name: payara
@@ -1069,8 +1069,11 @@ pekko:
10691069
srcPath: instrumentation/pekko/pekko-http-1.0
10701070
target_versions:
10711071
javaagent:
1072+
- com.softwaremill.sttp.tapir:tapir-pekko-http-server_3:[1.7,)
1073+
- com.softwaremill.sttp.tapir:tapir-pekko-http-server_2.12:[1.7,)
10721074
- org.apache.pekko:pekko-http_2.12:[1.0,)
10731075
- org.apache.pekko:pekko-http_3:[1.0,)
1076+
- com.softwaremill.sttp.tapir:tapir-pekko-http-server_2.13:[1.7,)
10741077
- org.apache.pekko:pekko-http_2.13:[1.0,)
10751078
play:
10761079
instrumentations:
@@ -1233,7 +1236,7 @@ restlet:
12331236
javaagent:
12341237
- org.restlet:org.restlet:[1.1.0, 1.2-M1)
12351238
library:
1236-
- org.restlet:org.restlet:1.1.5
1239+
- org.restlet:org.restlet:[1.1.5,1.+)
12371240
- com.noelios.restlet:com.noelios.restlet:1.1.5
12381241
- name: restlet-2.0
12391242
srcPath: instrumentation/restlet/restlet-2.0
@@ -1301,7 +1304,7 @@ rxjava:
13011304
javaagent:
13021305
- io.reactivex.rxjava3:rxjava:[3.0.0,3.1.0]
13031306
library:
1304-
- io.reactivex.rxjava3:rxjava:3.0.12
1307+
- io.reactivex.rxjava3:rxjava:[3.0.12,3.1.0)
13051308
scala:
13061309
instrumentations:
13071310
- name: scala-fork-join-2.8
@@ -1446,7 +1449,7 @@ spring:
14461449
javaagent:
14471450
- org.springframework.integration:spring-integration-core:[4.1.0.RELEASE,)
14481451
library:
1449-
- org.springframework.integration:spring-integration-core:4.1.0.RELEASE
1452+
- org.springframework.integration:spring-integration-core:[4.1.0.RELEASE,5.+)
14501453
- name: spring-jms-2.0
14511454
srcPath: instrumentation/spring/spring-jms/spring-jms-2.0
14521455
target_versions:

instrumentation-docs/readme.md

+3-7
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ public class SpringWebInstrumentationModule extends InstrumentationModule
5353

5454
### Versions targeted
5555

56-
Javaagent versions are determined by the `muzzle` plugin, so we can attempt to parse the gradle files
56+
We parse gradle files in order to determine the target versions.
5757

58-
Library versions are determined by the library versions used in the gradle files.
59-
60-
### TODO / Notes
61-
62-
- Is the `library` dependency actually the target version? Is there a better way to present the information?
63-
- How to handle oshi target version with a conditional?
58+
- Javaagent versions are determined by the `muzzle` plugin configurations
59+
- Library versions are determined by the library dependency versions

instrumentation-docs/src/main/java/io/opentelemetry/instrumentation/docs/GradleParser.java

+42-13
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,23 @@
1313
import java.util.regex.Pattern;
1414

1515
class GradleParser {
16-
private static final Pattern passBlockPattern =
17-
Pattern.compile("pass\\s*\\{(.*?)}", Pattern.DOTALL);
18-
19-
private static final Pattern libraryPattern =
20-
Pattern.compile("library\\(\"([^\"]+:[^\"]+:[^\"]+)\"\\)");
2116

2217
private static final Pattern variablePattern =
2318
Pattern.compile("val\\s+(\\w+)\\s*=\\s*\"([^\"]+)\"");
2419

20+
private static final Pattern muzzlePassBlockPattern =
21+
Pattern.compile("pass\\s*\\{(.*?)}", Pattern.DOTALL);
22+
23+
private static final Pattern libraryPattern =
24+
Pattern.compile("library\\(\"([^\"]+:[^\"]+):([^\"]+)\"\\)");
25+
2526
private static final Pattern compileOnlyPattern =
2627
Pattern.compile(
27-
"compileOnly\\(\"([^\"]+)\"\\)\\s*\\{\\s*version\\s*\\{(?:\\s*//.*\\n)*\\s*strictly\\(\"([^\"]+)\"\\)\\s*}\\s*}");
28+
"compileOnly\\(\"([^\"]+:[^\"]+)(?::[^\"]+)?\"\\)\\s*\\{\\s*version\\s*\\{.*?strictly\\(\"([^\"]+)\"\\).*?}\\s*",
29+
Pattern.DOTALL);
30+
31+
private static final Pattern latestDepTestLibraryPattern =
32+
Pattern.compile("latestDepTestLibrary\\(\"([^\"]+:[^\"]+):([^\"]+)\"\\)");
2833

2934
/**
3035
* Parses gradle files for muzzle and dependency information
@@ -55,7 +60,7 @@ public static Set<String> parseGradleFile(String gradleFileContents, Instrumenta
5560
*/
5661
private static Set<String> parseMuzzle(String gradleFileContents, Map<String, String> variables) {
5762
Set<String> results = new HashSet<>();
58-
Matcher passBlockMatcher = passBlockPattern.matcher(gradleFileContents);
63+
Matcher passBlockMatcher = muzzlePassBlockPattern.matcher(gradleFileContents);
5964

6065
while (passBlockMatcher.find()) {
6166
String passBlock = passBlockMatcher.group(1);
@@ -74,26 +79,50 @@ private static Set<String> parseMuzzle(String gradleFileContents, Map<String, St
7479

7580
/**
7681
* Parses the "dependencies" block from the given Gradle file content and extracts information
77-
* about what library versions are supported.
82+
* about what library versions are supported. Looks for library() and compileOnly() blocks for
83+
* lower bounds, and latestDepTestLibrary() for upper bounds.
7884
*
7985
* @param gradleFileContents Contents of a Gradle build file as a String
8086
* @param variables Map of variable names to their values
8187
* @return A set of strings summarizing the group, module, and versions
8288
*/
8389
private static Set<String> parseLibraryDependencies(
8490
String gradleFileContents, Map<String, String> variables) {
85-
Set<String> results = new HashSet<>();
91+
Map<String, String> versions = new HashMap<>();
92+
8693
Matcher libraryMatcher = libraryPattern.matcher(gradleFileContents);
94+
8795
while (libraryMatcher.find()) {
88-
String dependency = libraryMatcher.group(1);
89-
results.add(interpolate(dependency, variables));
96+
String groupAndArtifact = libraryMatcher.group(1);
97+
String version = libraryMatcher.group(2);
98+
versions.put(groupAndArtifact, version);
9099
}
91100

92101
Matcher compileOnlyMatcher = compileOnlyPattern.matcher(gradleFileContents);
93102
while (compileOnlyMatcher.find()) {
94-
String dependency = compileOnlyMatcher.group(1) + ":" + compileOnlyMatcher.group(2);
95-
results.add(interpolate(dependency, variables));
103+
String groupAndArtifact = compileOnlyMatcher.group(1);
104+
String version = compileOnlyMatcher.group(2);
105+
versions.put(groupAndArtifact, version);
96106
}
107+
108+
Matcher latestDepTestLibraryMatcher = latestDepTestLibraryPattern.matcher(gradleFileContents);
109+
while (latestDepTestLibraryMatcher.find()) {
110+
String groupAndArtifact = latestDepTestLibraryMatcher.group(1);
111+
String version = latestDepTestLibraryMatcher.group(2);
112+
if (versions.containsKey(groupAndArtifact)) {
113+
versions.put(groupAndArtifact, versions.get(groupAndArtifact) + "," + version);
114+
}
115+
}
116+
117+
Set<String> results = new HashSet<>();
118+
for (Map.Entry<String, String> entry : versions.entrySet()) {
119+
if (entry.getValue().contains(",")) {
120+
results.add(interpolate(entry.getKey() + ":[" + entry.getValue() + ")", variables));
121+
} else {
122+
results.add(interpolate(entry.getKey() + ":" + entry.getValue(), variables));
123+
}
124+
}
125+
97126
return results;
98127
}
99128

instrumentation-docs/src/test/java/io/opentelemetry/instrumentation/docs/GradleParserTest.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,29 @@ void testExtractMuzzleVersions_SinglePassBlock() {
3131

3232
@Test
3333
void testExtractLibraryVersion() {
34+
String gradleBuildFileContent =
35+
"dependencies {\n" + " library(\"org.apache.httpcomponents:httpclient:4.3\")\n" + "}";
36+
Set<String> versions =
37+
GradleParser.parseGradleFile(gradleBuildFileContent, InstrumentationType.LIBRARY);
38+
assertThat(versions.size()).isEqualTo(1);
39+
assertThat(versions.stream().findFirst().get())
40+
.isEqualTo("org.apache.httpcomponents:httpclient:4.3");
41+
}
42+
43+
@Test
44+
void testExtractLibraryUpperVersion() {
3445
String gradleBuildFileContent =
3546
"dependencies {\n"
3647
+ " library(\"org.apache.httpcomponents:httpclient:4.3\")\n"
3748
+ " testImplementation(project(\":instrumentation:apache-httpclient:apache-httpclient-4.3:testing\"))\n"
3849
+ " latestDepTestLibrary(\"org.apache.httpcomponents:httpclient:4.+\") // see apache-httpclient-5.0 module\n"
3950
+ "}";
51+
4052
Set<String> versions =
4153
GradleParser.parseGradleFile(gradleBuildFileContent, InstrumentationType.LIBRARY);
4254
assertThat(versions.size()).isEqualTo(1);
4355
assertThat(versions.stream().findFirst().get())
44-
.isEqualTo("org.apache.httpcomponents:httpclient:4.3");
56+
.isEqualTo("org.apache.httpcomponents:httpclient:[4.3,4.+)");
4557
}
4658

4759
@Test

0 commit comments

Comments
 (0)