Skip to content

Commit d6c6e8a

Browse files
author
Mateusz Rzeszutek
authored
Support application.yaml files in SpringBootServiceNameDetector (#9515)
1 parent 97a9d69 commit d6c6e8a

File tree

2 files changed

+53
-27
lines changed

2 files changed

+53
-27
lines changed

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

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.opentelemetry.instrumentation.spring.resources;
77

88
import static java.util.logging.Level.FINE;
9+
import static java.util.logging.Level.FINER;
910

1011
import com.google.auto.service.AutoService;
1112
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
@@ -24,7 +25,6 @@
2425
import java.util.Properties;
2526
import java.util.function.Function;
2627
import java.util.function.Supplier;
27-
import java.util.logging.Level;
2828
import java.util.logging.Logger;
2929
import java.util.regex.Matcher;
3030
import java.util.regex.Pattern;
@@ -73,7 +73,7 @@ public SpringBootServiceNameDetector() {
7373
@Override
7474
public Resource createResource(ConfigProperties config) {
7575

76-
logger.log(Level.FINER, "Performing Spring Boot service name auto-detection...");
76+
logger.log(FINER, "Performing Spring Boot service name auto-detection...");
7777
// Note: The order should be consistent with the order of Spring matching, but noting
7878
// that we have "first one wins" while Spring has "last one wins".
7979
// The docs for Spring are here:
@@ -84,8 +84,10 @@ public Resource createResource(ConfigProperties config) {
8484
this::findBySystemProperties,
8585
this::findByEnvironmentVariable,
8686
this::findByCurrentDirectoryApplicationProperties,
87+
this::findByCurrentDirectoryApplicationYml,
8788
this::findByCurrentDirectoryApplicationYaml,
8889
this::findByClasspathApplicationProperties,
90+
this::findByClasspathApplicationYml,
8991
this::findByClasspathApplicationYaml);
9092
return finders
9193
.map(Supplier::get)
@@ -119,24 +121,22 @@ public int order() {
119121
@Nullable
120122
private String findByEnvironmentVariable() {
121123
String result = system.getenv("SPRING_APPLICATION_NAME");
122-
logger.log(Level.FINER, "Checking for SPRING_APPLICATION_NAME in env: {0}", result);
124+
logger.log(FINER, "Checking for SPRING_APPLICATION_NAME in env: {0}", result);
123125
return result;
124126
}
125127

126128
@Nullable
127129
private String findBySystemProperties() {
128130
String result = system.getProperty("spring.application.name");
129-
logger.log(Level.FINER, "Checking for spring.application.name system property: {0}", result);
131+
logger.log(FINER, "Checking for spring.application.name system property: {0}", result);
130132
return result;
131133
}
132134

133135
@Nullable
134136
private String findByClasspathApplicationProperties() {
135137
String result = readNameFromAppProperties();
136138
logger.log(
137-
Level.FINER,
138-
"Checking for spring.application.name in application.properties file: {0}",
139-
result);
139+
FINER, "Checking for spring.application.name in application.properties file: {0}", result);
140140
return result;
141141
}
142142

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

155+
@Nullable
156+
private String findByClasspathApplicationYml() {
157+
return findByClasspathYamlFile("application.yml");
158+
}
159+
155160
@Nullable
156161
private String findByClasspathApplicationYaml() {
157-
String result =
158-
loadFromClasspath("application.yml", SpringBootServiceNameDetector::parseNameFromYaml);
159-
logger.log(Level.FINER, "Checking application.yml in classpath: {0}", result);
162+
return findByClasspathYamlFile("application.yaml");
163+
}
164+
165+
private String findByClasspathYamlFile(String fileName) {
166+
String result = loadFromClasspath(fileName, SpringBootServiceNameDetector::parseNameFromYaml);
167+
if (logger.isLoggable(FINER)) {
168+
logger.log(FINER, "Checking {0} in classpath: {1}", new Object[] {fileName, result});
169+
}
160170
return result;
161171
}
162172

173+
@Nullable
174+
private String findByCurrentDirectoryApplicationYml() {
175+
return findByCurrentDirectoryYamlFile("application.yml");
176+
}
177+
163178
@Nullable
164179
private String findByCurrentDirectoryApplicationYaml() {
180+
return findByCurrentDirectoryYamlFile("application.yaml");
181+
}
182+
183+
@Nullable
184+
private String findByCurrentDirectoryYamlFile(String fileName) {
165185
String result = null;
166-
try (InputStream in = system.openFile("application.yml")) {
186+
try (InputStream in = system.openFile(fileName)) {
167187
result = parseNameFromYaml(in);
168188
} catch (Exception e) {
169189
// expected to fail sometimes
170190
}
171-
logger.log(Level.FINER, "Checking application.yml in current dir: {0}", result);
191+
if (logger.isLoggable(FINER)) {
192+
logger.log(FINER, "Checking {0} in current dir: {1}", new Object[] {fileName, result});
193+
}
172194
return result;
173195
}
174196

@@ -203,7 +225,7 @@ private String findByCommandlineArgument() {
203225
String javaCommand = system.getProperty("sun.java.command");
204226
result = parseNameFromCommandLine(javaCommand);
205227
}
206-
logger.log(Level.FINER, "Checking application commandline args: {0}", result);
228+
logger.log(FINER, "Checking application commandline args: {0}", result);
207229
return result;
208230
}
209231

@@ -276,7 +298,7 @@ static class SystemHelper {
276298
contextClassLoader != null ? contextClassLoader : ClassLoader.getSystemClassLoader();
277299
addBootInfPrefix = classLoader.getResource("BOOT-INF/classes/") != null;
278300
if (addBootInfPrefix) {
279-
logger.log(Level.FINER, "Detected presence of BOOT-INF/classes/");
301+
logger.log(FINER, "Detected presence of BOOT-INF/classes/");
280302
}
281303
}
282304

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.nio.file.Paths;
2323
import org.junit.jupiter.api.Test;
2424
import org.junit.jupiter.api.extension.ExtendWith;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.ValueSource;
2527
import org.mockito.Mock;
2628
import org.mockito.junit.jupiter.MockitoExtension;
2729

@@ -66,33 +68,35 @@ void propertiesFileInCurrentDir() throws Exception {
6668
}
6769
}
6870

69-
@Test
70-
void classpathApplicationYaml() {
71-
when(system.openClasspathResource(APPLICATION_YML))
72-
.thenReturn(openClasspathResource(APPLICATION_YML));
71+
@ParameterizedTest
72+
@ValueSource(strings = {"application.yaml", APPLICATION_YML})
73+
void classpathApplicationYaml(String fileName) {
74+
when(system.openClasspathResource(fileName)).thenReturn(openClasspathResource(APPLICATION_YML));
7375
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
7476
Resource result = guesser.createResource(config);
7577
expectServiceName(result, "cat-store");
7678
}
7779

78-
@Test
79-
void classpathApplicationYamlContainingMultipleYamlDefinitions() {
80-
when(system.openClasspathResource(APPLICATION_YML))
80+
@ParameterizedTest
81+
@ValueSource(strings = {"application.yaml", APPLICATION_YML})
82+
void classpathApplicationYamlContainingMultipleYamlDefinitions(String fileName) {
83+
when(system.openClasspathResource(fileName))
8184
.thenReturn(
8285
ClassLoader.getSystemClassLoader().getResourceAsStream("application-multi.yml"));
8386
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
8487
Resource result = guesser.createResource(config);
8588
expectServiceName(result, "cat-store");
8689
}
8790

88-
@Test
89-
void yamlFileInCurrentDir() throws Exception {
90-
Path yamlPath = Paths.get(APPLICATION_YML);
91+
@ParameterizedTest
92+
@ValueSource(strings = {"application.yaml", APPLICATION_YML})
93+
void yamlFileInCurrentDir(String fileName) throws Exception {
94+
Path yamlPath = Paths.get(fileName);
9195
try {
9296
URL url = getClass().getClassLoader().getResource(APPLICATION_YML);
9397
String content = readString(Paths.get(url.toURI()));
9498
writeString(yamlPath, content);
95-
when(system.openFile(APPLICATION_YML)).thenCallRealMethod();
99+
when(system.openFile(fileName)).thenCallRealMethod();
96100
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
97101
Resource result = guesser.createResource(config);
98102
expectServiceName(result, "cat-store");
@@ -117,7 +121,7 @@ void getFromCommandlineArgsWithProcessHandle() throws Exception {
117121
}
118122

119123
@Test
120-
void getFromCommandlineArgsWithSystemProperty() throws Exception {
124+
void getFromCommandlineArgsWithSystemProperty() {
121125
when(system.getProperty("sun.java.command"))
122126
.thenReturn("/bin/java sweet-spring.jar --spring.application.name=bullpen --quiet=never");
123127
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);

0 commit comments

Comments
 (0)