Skip to content

Commit

Permalink
Add the situation of specifying spring.application.name in the bootst…
Browse files Browse the repository at this point in the history
…rap file after introducing spring cloud (open-telemetry#9801)
  • Loading branch information
AlchemyDing authored and Abhishekkr3003 committed Nov 7, 2023
1 parent e24a2da commit 070026b
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public Resource createResource(ConfigProperties config) {
// that we have "first one wins" while Spring has "last one wins".
// The docs for Spring are here:
// https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config
// https://docs.spring.io/spring-cloud-commons/docs/4.0.4/reference/html/#the-bootstrap-application-context
Stream<Supplier<String>> finders =
Stream.of(
this::findByCommandlineArgument,
Expand All @@ -84,7 +85,10 @@ public Resource createResource(ConfigProperties config) {
this::findByCurrentDirectoryApplicationYaml,
this::findByClasspathApplicationProperties,
this::findByClasspathApplicationYml,
this::findByClasspathApplicationYaml);
this::findByClasspathApplicationYaml,
this::findByClasspathBootstrapProperties,
this::findByClasspathBootstrapYml,
this::findByClasspathBootstrapYaml);
return finders
.map(Supplier::get)
.filter(Objects::nonNull)
Expand Down Expand Up @@ -130,10 +134,12 @@ private String findBySystemProperties() {

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

@Nullable
private String findByClasspathBootstrapProperties() {
return findByClasspathPropertiesFile("bootstrap.properties");
}

@Nullable
Expand All @@ -153,11 +159,21 @@ private String findByClasspathApplicationYml() {
return findByClasspathYamlFile("application.yml");
}

@Nullable
private String findByClasspathBootstrapYml() {
return findByClasspathYamlFile("bootstrap.yml");
}

@Nullable
private String findByClasspathApplicationYaml() {
return findByClasspathYamlFile("application.yaml");
}

@Nullable
private String findByClasspathBootstrapYaml() {
return findByClasspathYamlFile("bootstrap.yaml");
}

private String findByClasspathYamlFile(String fileName) {
String result = loadFromClasspath(fileName, SpringBootServiceNameDetector::parseNameFromYaml);
if (logger.isLoggable(FINER)) {
Expand Down Expand Up @@ -257,9 +273,16 @@ private static String parseNameFromProcessArgs(String[] args) {
}

@Nullable
private String readNameFromAppProperties() {
return loadFromClasspath(
"application.properties", SpringBootServiceNameDetector::getAppNamePropertyFromStream);
private String findByClasspathPropertiesFile(String filename) {
String result =
loadFromClasspath(filename, SpringBootServiceNameDetector::getAppNamePropertyFromStream);
if (logger.isLoggable(FINER)) {
logger.log(
FINER,
"Checking for spring.application.name in {0} file: {1}",
new Object[] {filename, result});
}
return result;
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@
@ExtendWith(MockitoExtension.class)
class SpringBootServiceNameDetectorTest {

static final String PROPS = "application.properties";
static final String APPLICATION_PROPS = "application.properties";
static final String APPLICATION_YML = "application.yml";

static final String BOOTSTRAP_PROPS = "bootstrap.properties";

static final String BOOTSTRAP_YML = "bootstrap.yml";

@Mock ConfigProperties config;
@Mock SystemHelper system;

Expand All @@ -48,18 +53,28 @@ void findByEnvVar() {

@Test
void classpathApplicationProperties() {
when(system.openClasspathResource(PROPS)).thenReturn(openClasspathResource(PROPS));
when(system.openClasspathResource(APPLICATION_PROPS))
.thenReturn(openClasspathResource(APPLICATION_PROPS));
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
Resource result = guesser.createResource(config);
expectServiceName(result, "dog-store");
}

@Test
void classpathBootstrapProperties() {
when(system.openClasspathResource(BOOTSTRAP_PROPS))
.thenReturn(openClasspathResource(BOOTSTRAP_PROPS));
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
Resource result = guesser.createResource(config);
expectServiceName(result, "dog-store-bootstrap");
}

@Test
void propertiesFileInCurrentDir() throws Exception {
Path propsPath = Paths.get(PROPS);
Path propsPath = Paths.get(APPLICATION_PROPS);
try {
writeString(propsPath, "spring.application.name=fish-tank\n");
when(system.openFile(PROPS)).thenCallRealMethod();
when(system.openFile(APPLICATION_PROPS)).thenCallRealMethod();
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
Resource result = guesser.createResource(config);
expectServiceName(result, "fish-tank");
Expand All @@ -77,6 +92,25 @@ void classpathApplicationYaml(String fileName) {
expectServiceName(result, "cat-store");
}

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

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

@ParameterizedTest
@ValueSource(strings = {"application.yaml", APPLICATION_YML})
void classpathApplicationYamlContainingMultipleYamlDefinitions(String fileName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spring:
application:
name: cat-store-bootstrap

---
some:
other:
property: value

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name: dog-store-bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring:
application:
name: cat-store-bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring:
application:
name: cat-store-bootstrap

0 comments on commit 070026b

Please sign in to comment.