Skip to content

Commit

Permalink
Added smoke test for SpringBootServiceVersionDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
Hayanesh committed Sep 21, 2023
1 parent 67e285b commit fc03175
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@
import io.opentelemetry.semconv.ResourceAttributes;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.function.Function;
import java.util.function.Supplier;
Expand Down Expand Up @@ -264,59 +260,4 @@ private String loadFromClasspath(String filename, Function<InputStream, String>
return null;
}
}

// Exists for testing
static class SystemHelper {
private final ClassLoader classLoader;
private final boolean addBootInfPrefix;

SystemHelper() {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
classLoader =
contextClassLoader != null ? contextClassLoader : ClassLoader.getSystemClassLoader();
addBootInfPrefix = classLoader.getResource("BOOT-INF/classes/") != null;
if (addBootInfPrefix) {
logger.log(Level.FINER, "Detected presence of BOOT-INF/classes/");
}
}

String getenv(String name) {
return System.getenv(name);
}

String getProperty(String key) {
return System.getProperty(key);
}

InputStream openClasspathResource(String filename) {
String path = addBootInfPrefix ? "BOOT-INF/classes/" + filename : filename;
return classLoader.getResourceAsStream(path);
}

InputStream openClasspathResource(String filename, String location) {
String path = location + "/" + filename;
return classLoader.getResourceAsStream(path);
}

InputStream openFile(String filename) throws Exception {
return Files.newInputStream(Paths.get(filename));
}

/**
* Attempts to use ProcessHandle to get the full commandline of the current process (including
* the main method arguments). Will only succeed on java 9+.
*/
@SuppressWarnings("unchecked")
String[] attemptGetCommandLineArgsViaReflection() throws Exception {
Class<?> clazz = Class.forName("java.lang.ProcessHandle");
Method currentMethod = clazz.getDeclaredMethod("current");
Method infoMethod = clazz.getDeclaredMethod("info");
Object currentInstance = currentMethod.invoke(null);
Object info = infoMethod.invoke(currentInstance);
Class<?> infoClass = Class.forName("java.lang.ProcessHandle$Info");
Method argumentsMethod = infoClass.getMethod("arguments");
Optional<String[]> optionalArgs = (Optional<String[]>) argumentsMethod.invoke(info);
return optionalArgs.orElse(new String[0]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static java.util.logging.Level.FINE;

import com.google.auto.service.AutoService;
import io.opentelemetry.instrumentation.spring.resources.SpringBootServiceNameDetector.SystemHelper;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
import io.opentelemetry.sdk.resources.Resource;
Expand All @@ -32,7 +31,7 @@ public SpringBootServiceVersionDetector() {
}

// Exists for testing
public SpringBootServiceVersionDetector(SystemHelper system) {
SpringBootServiceVersionDetector(SystemHelper system) {
this.system = system;
}

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

private Optional<String> getServiceVersionFromBuildInfo() {
try (InputStream in = system.openClasspathResource("build-info.properties", "META-INF")) {
try (InputStream in = system.openClasspathResource("META-INF", "build-info.properties")) {
return in != null ? getServiceVersionPropertyFromStream(in) : Optional.empty();
} catch (Exception e) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.spring.resources;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

class SystemHelper {
private static final Logger logger = Logger.getLogger(SystemHelper.class.getName());

private final ClassLoader classLoader;
private final boolean addBootInfPrefix;

SystemHelper() {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
classLoader =
contextClassLoader != null ? contextClassLoader : ClassLoader.getSystemClassLoader();
addBootInfPrefix = classLoader.getResource("BOOT-INF/classes/") != null;
if (addBootInfPrefix) {
logger.log(Level.FINER, "Detected presence of BOOT-INF/classes/");
}
}

String getenv(String name) {
return System.getenv(name);
}

String getProperty(String key) {
return System.getProperty(key);
}

InputStream openClasspathResource(String filename) {
String path = addBootInfPrefix ? "BOOT-INF/classes/" + filename : filename;
return classLoader.getResourceAsStream(path);
}

InputStream openClasspathResource(String directory, String filename) {
String path = directory + "/" + filename;
return classLoader.getResourceAsStream(path);
}

InputStream openFile(String filename) throws Exception {
return Files.newInputStream(Paths.get(filename));
}

/**
* Attempts to use ProcessHandle to get the full commandline of the current process (including the
* main method arguments). Will only succeed on java 9+.
*/
@SuppressWarnings("unchecked")
String[] attemptGetCommandLineArgsViaReflection() throws Exception {
Class<?> clazz = Class.forName("java.lang.ProcessHandle");
Method currentMethod = clazz.getDeclaredMethod("current");
Method infoMethod = clazz.getDeclaredMethod("info");
Object currentInstance = currentMethod.invoke(null);
Object info = infoMethod.invoke(currentInstance);
Class<?> infoClass = Class.forName("java.lang.ProcessHandle$Info");
Method argumentsMethod = infoClass.getMethod("arguments");
Optional<String[]> optionalArgs = (Optional<String[]>) argumentsMethod.invoke(info);
return optionalArgs.orElse(new String[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SpringBootServiceNameDetectorTest {
static final String PROPS = "application.properties";
static final String APPLICATION_YML = "application.yml";
@Mock ConfigProperties config;
@Mock SpringBootServiceNameDetector.SystemHelper system;
@Mock SystemHelper system;

@Test
void findByEnvVar() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class SpringBootServiceVersionDetectorTest {
static final String META_INFO = "META-INF";

@Mock ConfigProperties config;
@Mock SpringBootServiceNameDetector.SystemHelper system;
@Mock SystemHelper system;

@Test
void givenBuildVersionIsPresentInBuildInfProperties_thenReturnBuildVersion() {
when(system.openClasspathResource(BUILD_PROPS, META_INFO))
when(system.openClasspathResource(META_INFO, BUILD_PROPS))
.thenReturn(openClasspathResource(META_INFO + "/" + BUILD_PROPS));

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

@Test
void givenBuildVersionFileNotPresent_thenReturnEmptyResource() {
when(system.openClasspathResource(BUILD_PROPS, META_INFO)).thenReturn(null);
when(system.openClasspathResource(META_INFO, BUILD_PROPS)).thenReturn(null);

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

@Test
void givenBuildVersionFileIsPresentButBuildVersionPropertyNotPresent_thenReturnEmptyResource() {
when(system.openClasspathResource(BUILD_PROPS, META_INFO))
when(system.openClasspathResource(META_INFO, BUILD_PROPS))
.thenReturn(openClasspathResource(BUILD_PROPS));

SpringBootServiceVersionDetector guesser = new SpringBootServiceVersionDetector(system);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import static java.util.stream.Collectors.toSet
class SpringBootSmokeTest extends SmokeTest {

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

@Override
Expand Down Expand Up @@ -94,6 +94,13 @@ class SpringBootSmokeTest extends SmokeTest {
serviceName.isPresent()
serviceName.get() == "otel-spring-test-app"

then: "service version is autodetected"
def serviceVersion = findResourceAttribute(traces, "service.version")
.map { it.stringValue }
.findAny()
serviceVersion.isPresent()
serviceVersion.get() == "1.31.0-alpha-SNAPSHOT"

cleanup:
stopTarget()

Expand Down

0 comments on commit fc03175

Please sign in to comment.