diff --git a/instrumentation/spring/spring-boot-resources/library/src/main/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceNameDetector.java b/instrumentation/spring/spring-boot-resources/library/src/main/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceNameDetector.java index 83e2daf73780..b0a598846abe 100644 --- a/instrumentation/spring/spring-boot-resources/library/src/main/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceNameDetector.java +++ b/instrumentation/spring/spring-boot-resources/library/src/main/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceNameDetector.java @@ -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> finders = Stream.of( this::findByCommandlineArgument, @@ -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) @@ -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 @@ -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)) { @@ -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 diff --git a/instrumentation/spring/spring-boot-resources/library/src/test/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceNameDetectorTest.java b/instrumentation/spring/spring-boot-resources/library/src/test/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceNameDetectorTest.java index bd0c5bd6c953..935cfab9a998 100644 --- a/instrumentation/spring/spring-boot-resources/library/src/test/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceNameDetectorTest.java +++ b/instrumentation/spring/spring-boot-resources/library/src/test/java/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceNameDetectorTest.java @@ -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; @@ -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"); @@ -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) { diff --git a/instrumentation/spring/spring-boot-resources/library/src/test/resources/bootstrap-multi.yml b/instrumentation/spring/spring-boot-resources/library/src/test/resources/bootstrap-multi.yml new file mode 100644 index 000000000000..8b5ad5424708 --- /dev/null +++ b/instrumentation/spring/spring-boot-resources/library/src/test/resources/bootstrap-multi.yml @@ -0,0 +1,9 @@ +spring: + application: + name: cat-store-bootstrap + +--- +some: + other: + property: value + diff --git a/instrumentation/spring/spring-boot-resources/library/src/test/resources/bootstrap.properties b/instrumentation/spring/spring-boot-resources/library/src/test/resources/bootstrap.properties new file mode 100644 index 000000000000..c4c64893bf48 --- /dev/null +++ b/instrumentation/spring/spring-boot-resources/library/src/test/resources/bootstrap.properties @@ -0,0 +1 @@ +spring.application.name: dog-store-bootstrap diff --git a/instrumentation/spring/spring-boot-resources/library/src/test/resources/bootstrap.yaml b/instrumentation/spring/spring-boot-resources/library/src/test/resources/bootstrap.yaml new file mode 100644 index 000000000000..9e06c48c08c7 --- /dev/null +++ b/instrumentation/spring/spring-boot-resources/library/src/test/resources/bootstrap.yaml @@ -0,0 +1,3 @@ +spring: + application: + name: cat-store-bootstrap diff --git a/instrumentation/spring/spring-boot-resources/library/src/test/resources/bootstrap.yml b/instrumentation/spring/spring-boot-resources/library/src/test/resources/bootstrap.yml new file mode 100644 index 000000000000..9e06c48c08c7 --- /dev/null +++ b/instrumentation/spring/spring-boot-resources/library/src/test/resources/bootstrap.yml @@ -0,0 +1,3 @@ +spring: + application: + name: cat-store-bootstrap