Skip to content

Commit

Permalink
Support application.yaml files in SpringBootServiceNameDetector (#9515)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek committed Sep 21, 2023
1 parent 97a9d69 commit d6c6e8a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.instrumentation.spring.resources;

import static java.util.logging.Level.FINE;
import static java.util.logging.Level.FINER;

import com.google.auto.service.AutoService;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
Expand All @@ -24,7 +25,6 @@
import java.util.Properties;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -73,7 +73,7 @@ public SpringBootServiceNameDetector() {
@Override
public Resource createResource(ConfigProperties config) {

logger.log(Level.FINER, "Performing Spring Boot service name auto-detection...");
logger.log(FINER, "Performing Spring Boot service name auto-detection...");
// Note: The order should be consistent with the order of Spring matching, but noting
// that we have "first one wins" while Spring has "last one wins".
// The docs for Spring are here:
Expand All @@ -84,8 +84,10 @@ public Resource createResource(ConfigProperties config) {
this::findBySystemProperties,
this::findByEnvironmentVariable,
this::findByCurrentDirectoryApplicationProperties,
this::findByCurrentDirectoryApplicationYml,
this::findByCurrentDirectoryApplicationYaml,
this::findByClasspathApplicationProperties,
this::findByClasspathApplicationYml,
this::findByClasspathApplicationYaml);
return finders
.map(Supplier::get)
Expand Down Expand Up @@ -119,24 +121,22 @@ public int order() {
@Nullable
private String findByEnvironmentVariable() {
String result = system.getenv("SPRING_APPLICATION_NAME");
logger.log(Level.FINER, "Checking for SPRING_APPLICATION_NAME in env: {0}", result);
logger.log(FINER, "Checking for SPRING_APPLICATION_NAME in env: {0}", result);
return result;
}

@Nullable
private String findBySystemProperties() {
String result = system.getProperty("spring.application.name");
logger.log(Level.FINER, "Checking for spring.application.name system property: {0}", result);
logger.log(FINER, "Checking for spring.application.name system property: {0}", result);
return result;
}

@Nullable
private String findByClasspathApplicationProperties() {
String result = readNameFromAppProperties();
logger.log(
Level.FINER,
"Checking for spring.application.name in application.properties file: {0}",
result);
FINER, "Checking for spring.application.name in application.properties file: {0}", result);
return result;
}

Expand All @@ -148,27 +148,49 @@ private String findByCurrentDirectoryApplicationProperties() {
} catch (Exception e) {
// expected to fail sometimes
}
logger.log(Level.FINER, "Checking application.properties in current dir: {0}", result);
logger.log(FINER, "Checking application.properties in current dir: {0}", result);
return result;
}

@Nullable
private String findByClasspathApplicationYml() {
return findByClasspathYamlFile("application.yml");
}

@Nullable
private String findByClasspathApplicationYaml() {
String result =
loadFromClasspath("application.yml", SpringBootServiceNameDetector::parseNameFromYaml);
logger.log(Level.FINER, "Checking application.yml in classpath: {0}", result);
return findByClasspathYamlFile("application.yaml");
}

private String findByClasspathYamlFile(String fileName) {
String result = loadFromClasspath(fileName, SpringBootServiceNameDetector::parseNameFromYaml);
if (logger.isLoggable(FINER)) {
logger.log(FINER, "Checking {0} in classpath: {1}", new Object[] {fileName, result});
}
return result;
}

@Nullable
private String findByCurrentDirectoryApplicationYml() {
return findByCurrentDirectoryYamlFile("application.yml");
}

@Nullable
private String findByCurrentDirectoryApplicationYaml() {
return findByCurrentDirectoryYamlFile("application.yaml");
}

@Nullable
private String findByCurrentDirectoryYamlFile(String fileName) {
String result = null;
try (InputStream in = system.openFile("application.yml")) {
try (InputStream in = system.openFile(fileName)) {
result = parseNameFromYaml(in);
} catch (Exception e) {
// expected to fail sometimes
}
logger.log(Level.FINER, "Checking application.yml in current dir: {0}", result);
if (logger.isLoggable(FINER)) {
logger.log(FINER, "Checking {0} in current dir: {1}", new Object[] {fileName, result});
}
return result;
}

Expand Down Expand Up @@ -203,7 +225,7 @@ private String findByCommandlineArgument() {
String javaCommand = system.getProperty("sun.java.command");
result = parseNameFromCommandLine(javaCommand);
}
logger.log(Level.FINER, "Checking application commandline args: {0}", result);
logger.log(FINER, "Checking application commandline args: {0}", result);
return result;
}

Expand Down Expand Up @@ -276,7 +298,7 @@ static class SystemHelper {
contextClassLoader != null ? contextClassLoader : ClassLoader.getSystemClassLoader();
addBootInfPrefix = classLoader.getResource("BOOT-INF/classes/") != null;
if (addBootInfPrefix) {
logger.log(Level.FINER, "Detected presence of BOOT-INF/classes/");
logger.log(FINER, "Detected presence of BOOT-INF/classes/");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.nio.file.Paths;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

Expand Down Expand Up @@ -66,33 +68,35 @@ void propertiesFileInCurrentDir() throws Exception {
}
}

@Test
void classpathApplicationYaml() {
when(system.openClasspathResource(APPLICATION_YML))
.thenReturn(openClasspathResource(APPLICATION_YML));
@ParameterizedTest
@ValueSource(strings = {"application.yaml", APPLICATION_YML})
void classpathApplicationYaml(String fileName) {
when(system.openClasspathResource(fileName)).thenReturn(openClasspathResource(APPLICATION_YML));
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
Resource result = guesser.createResource(config);
expectServiceName(result, "cat-store");
}

@Test
void classpathApplicationYamlContainingMultipleYamlDefinitions() {
when(system.openClasspathResource(APPLICATION_YML))
@ParameterizedTest
@ValueSource(strings = {"application.yaml", APPLICATION_YML})
void classpathApplicationYamlContainingMultipleYamlDefinitions(String fileName) {
when(system.openClasspathResource(fileName))
.thenReturn(
ClassLoader.getSystemClassLoader().getResourceAsStream("application-multi.yml"));
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
Resource result = guesser.createResource(config);
expectServiceName(result, "cat-store");
}

@Test
void yamlFileInCurrentDir() throws Exception {
Path yamlPath = Paths.get(APPLICATION_YML);
@ParameterizedTest
@ValueSource(strings = {"application.yaml", APPLICATION_YML})
void yamlFileInCurrentDir(String fileName) throws Exception {
Path yamlPath = Paths.get(fileName);
try {
URL url = getClass().getClassLoader().getResource(APPLICATION_YML);
String content = readString(Paths.get(url.toURI()));
writeString(yamlPath, content);
when(system.openFile(APPLICATION_YML)).thenCallRealMethod();
when(system.openFile(fileName)).thenCallRealMethod();
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
Resource result = guesser.createResource(config);
expectServiceName(result, "cat-store");
Expand All @@ -117,7 +121,7 @@ void getFromCommandlineArgsWithProcessHandle() throws Exception {
}

@Test
void getFromCommandlineArgsWithSystemProperty() throws Exception {
void getFromCommandlineArgsWithSystemProperty() {
when(system.getProperty("sun.java.command"))
.thenReturn("/bin/java sweet-spring.jar --spring.application.name=bullpen --quiet=never");
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
Expand Down

0 comments on commit d6c6e8a

Please sign in to comment.