Skip to content

Commit fc03175

Browse files
committed
Added smoke test for SpringBootServiceVersionDetector
1 parent 67e285b commit fc03175

File tree

6 files changed

+85
-68
lines changed

6 files changed

+85
-68
lines changed

instrumentation/spring/spring-boot-resources/library/src/main/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceNameDetector.java

-59
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@
1515
import io.opentelemetry.semconv.ResourceAttributes;
1616
import java.io.IOException;
1717
import java.io.InputStream;
18-
import java.lang.reflect.Method;
19-
import java.nio.file.Files;
20-
import java.nio.file.Paths;
2118
import java.util.Map;
2219
import java.util.Objects;
23-
import java.util.Optional;
2420
import java.util.Properties;
2521
import java.util.function.Function;
2622
import java.util.function.Supplier;
@@ -264,59 +260,4 @@ private String loadFromClasspath(String filename, Function<InputStream, String>
264260
return null;
265261
}
266262
}
267-
268-
// Exists for testing
269-
static class SystemHelper {
270-
private final ClassLoader classLoader;
271-
private final boolean addBootInfPrefix;
272-
273-
SystemHelper() {
274-
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
275-
classLoader =
276-
contextClassLoader != null ? contextClassLoader : ClassLoader.getSystemClassLoader();
277-
addBootInfPrefix = classLoader.getResource("BOOT-INF/classes/") != null;
278-
if (addBootInfPrefix) {
279-
logger.log(Level.FINER, "Detected presence of BOOT-INF/classes/");
280-
}
281-
}
282-
283-
String getenv(String name) {
284-
return System.getenv(name);
285-
}
286-
287-
String getProperty(String key) {
288-
return System.getProperty(key);
289-
}
290-
291-
InputStream openClasspathResource(String filename) {
292-
String path = addBootInfPrefix ? "BOOT-INF/classes/" + filename : filename;
293-
return classLoader.getResourceAsStream(path);
294-
}
295-
296-
InputStream openClasspathResource(String filename, String location) {
297-
String path = location + "/" + filename;
298-
return classLoader.getResourceAsStream(path);
299-
}
300-
301-
InputStream openFile(String filename) throws Exception {
302-
return Files.newInputStream(Paths.get(filename));
303-
}
304-
305-
/**
306-
* Attempts to use ProcessHandle to get the full commandline of the current process (including
307-
* the main method arguments). Will only succeed on java 9+.
308-
*/
309-
@SuppressWarnings("unchecked")
310-
String[] attemptGetCommandLineArgsViaReflection() throws Exception {
311-
Class<?> clazz = Class.forName("java.lang.ProcessHandle");
312-
Method currentMethod = clazz.getDeclaredMethod("current");
313-
Method infoMethod = clazz.getDeclaredMethod("info");
314-
Object currentInstance = currentMethod.invoke(null);
315-
Object info = infoMethod.invoke(currentInstance);
316-
Class<?> infoClass = Class.forName("java.lang.ProcessHandle$Info");
317-
Method argumentsMethod = infoClass.getMethod("arguments");
318-
Optional<String[]> optionalArgs = (Optional<String[]>) argumentsMethod.invoke(info);
319-
return optionalArgs.orElse(new String[0]);
320-
}
321-
}
322263
}

instrumentation/spring/spring-boot-resources/library/src/main/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceVersionDetector.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import static java.util.logging.Level.FINE;
99

1010
import com.google.auto.service.AutoService;
11-
import io.opentelemetry.instrumentation.spring.resources.SpringBootServiceNameDetector.SystemHelper;
1211
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
1312
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
1413
import io.opentelemetry.sdk.resources.Resource;
@@ -32,7 +31,7 @@ public SpringBootServiceVersionDetector() {
3231
}
3332

3433
// Exists for testing
35-
public SpringBootServiceVersionDetector(SystemHelper system) {
34+
SpringBootServiceVersionDetector(SystemHelper system) {
3635
this.system = system;
3736
}
3837

@@ -48,7 +47,7 @@ public Resource createResource(ConfigProperties config) {
4847
}
4948

5049
private Optional<String> getServiceVersionFromBuildInfo() {
51-
try (InputStream in = system.openClasspathResource("build-info.properties", "META-INF")) {
50+
try (InputStream in = system.openClasspathResource("META-INF", "build-info.properties")) {
5251
return in != null ? getServiceVersionPropertyFromStream(in) : Optional.empty();
5352
} catch (Exception e) {
5453
return Optional.empty();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.spring.resources;
7+
8+
import java.io.InputStream;
9+
import java.lang.reflect.Method;
10+
import java.nio.file.Files;
11+
import java.nio.file.Paths;
12+
import java.util.Optional;
13+
import java.util.logging.Level;
14+
import java.util.logging.Logger;
15+
16+
class SystemHelper {
17+
private static final Logger logger = Logger.getLogger(SystemHelper.class.getName());
18+
19+
private final ClassLoader classLoader;
20+
private final boolean addBootInfPrefix;
21+
22+
SystemHelper() {
23+
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
24+
classLoader =
25+
contextClassLoader != null ? contextClassLoader : ClassLoader.getSystemClassLoader();
26+
addBootInfPrefix = classLoader.getResource("BOOT-INF/classes/") != null;
27+
if (addBootInfPrefix) {
28+
logger.log(Level.FINER, "Detected presence of BOOT-INF/classes/");
29+
}
30+
}
31+
32+
String getenv(String name) {
33+
return System.getenv(name);
34+
}
35+
36+
String getProperty(String key) {
37+
return System.getProperty(key);
38+
}
39+
40+
InputStream openClasspathResource(String filename) {
41+
String path = addBootInfPrefix ? "BOOT-INF/classes/" + filename : filename;
42+
return classLoader.getResourceAsStream(path);
43+
}
44+
45+
InputStream openClasspathResource(String directory, String filename) {
46+
String path = directory + "/" + filename;
47+
return classLoader.getResourceAsStream(path);
48+
}
49+
50+
InputStream openFile(String filename) throws Exception {
51+
return Files.newInputStream(Paths.get(filename));
52+
}
53+
54+
/**
55+
* Attempts to use ProcessHandle to get the full commandline of the current process (including the
56+
* main method arguments). Will only succeed on java 9+.
57+
*/
58+
@SuppressWarnings("unchecked")
59+
String[] attemptGetCommandLineArgsViaReflection() throws Exception {
60+
Class<?> clazz = Class.forName("java.lang.ProcessHandle");
61+
Method currentMethod = clazz.getDeclaredMethod("current");
62+
Method infoMethod = clazz.getDeclaredMethod("info");
63+
Object currentInstance = currentMethod.invoke(null);
64+
Object info = infoMethod.invoke(currentInstance);
65+
Class<?> infoClass = Class.forName("java.lang.ProcessHandle$Info");
66+
Method argumentsMethod = infoClass.getMethod("arguments");
67+
Optional<String[]> optionalArgs = (Optional<String[]>) argumentsMethod.invoke(info);
68+
return optionalArgs.orElse(new String[0]);
69+
}
70+
}

instrumentation/spring/spring-boot-resources/library/src/test/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceNameDetectorTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class SpringBootServiceNameDetectorTest {
3131
static final String PROPS = "application.properties";
3232
static final String APPLICATION_YML = "application.yml";
3333
@Mock ConfigProperties config;
34-
@Mock SpringBootServiceNameDetector.SystemHelper system;
34+
@Mock SystemHelper system;
3535

3636
@Test
3737
void findByEnvVar() {

instrumentation/spring/spring-boot-resources/library/src/test/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceVersionDetectorTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ class SpringBootServiceVersionDetectorTest {
2424
static final String META_INFO = "META-INF";
2525

2626
@Mock ConfigProperties config;
27-
@Mock SpringBootServiceNameDetector.SystemHelper system;
27+
@Mock SystemHelper system;
2828

2929
@Test
3030
void givenBuildVersionIsPresentInBuildInfProperties_thenReturnBuildVersion() {
31-
when(system.openClasspathResource(BUILD_PROPS, META_INFO))
31+
when(system.openClasspathResource(META_INFO, BUILD_PROPS))
3232
.thenReturn(openClasspathResource(META_INFO + "/" + BUILD_PROPS));
3333

3434
SpringBootServiceVersionDetector guesser = new SpringBootServiceVersionDetector(system);
@@ -38,7 +38,7 @@ void givenBuildVersionIsPresentInBuildInfProperties_thenReturnBuildVersion() {
3838

3939
@Test
4040
void givenBuildVersionFileNotPresent_thenReturnEmptyResource() {
41-
when(system.openClasspathResource(BUILD_PROPS, META_INFO)).thenReturn(null);
41+
when(system.openClasspathResource(META_INFO, BUILD_PROPS)).thenReturn(null);
4242

4343
SpringBootServiceVersionDetector guesser = new SpringBootServiceVersionDetector(system);
4444
Resource result = guesser.createResource(config);
@@ -47,7 +47,7 @@ void givenBuildVersionFileNotPresent_thenReturnEmptyResource() {
4747

4848
@Test
4949
void givenBuildVersionFileIsPresentButBuildVersionPropertyNotPresent_thenReturnEmptyResource() {
50-
when(system.openClasspathResource(BUILD_PROPS, META_INFO))
50+
when(system.openClasspathResource(META_INFO, BUILD_PROPS))
5151
.thenReturn(openClasspathResource(BUILD_PROPS));
5252

5353
SpringBootServiceVersionDetector guesser = new SpringBootServiceVersionDetector(system);

smoke-tests/src/test/groovy/io/opentelemetry/smoketest/SpringBootSmokeTest.groovy

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import static java.util.stream.Collectors.toSet
2121
class SpringBootSmokeTest extends SmokeTest {
2222

2323
protected String getTargetImage(String jdk) {
24-
"ghcr.io/open-telemetry/opentelemetry-java-instrumentation/smoke-test-spring-boot:jdk$jdk-20230321.4484174638"
24+
"ghcr.io/open-telemetry/opentelemetry-java-instrumentation/smoke-test-spring-boot:jdk$jdk-20230920.6251727205"
2525
}
2626

2727
@Override
@@ -94,6 +94,13 @@ class SpringBootSmokeTest extends SmokeTest {
9494
serviceName.isPresent()
9595
serviceName.get() == "otel-spring-test-app"
9696

97+
then: "service version is autodetected"
98+
def serviceVersion = findResourceAttribute(traces, "service.version")
99+
.map { it.stringValue }
100+
.findAny()
101+
serviceVersion.isPresent()
102+
serviceVersion.get() == "1.31.0-alpha-SNAPSHOT"
103+
97104
cleanup:
98105
stopTarget()
99106

0 commit comments

Comments
 (0)