Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.instrumentation.spring.resources;

import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME;
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_VERSION;
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
Expand All @@ -30,4 +31,17 @@ void testServiceName() {
Resource result = guesser.createResource(config);
assertThat(result.getAttribute(SERVICE_NAME)).isEqualTo("otel-spring-test-app");
}

@Test
void testServiceVersion() {
// verify that the test app, that is added as a dependency to this project, has the expected
// layout: build-info.properties lives at the jar root under META-INF/, not under
// BOOT-INF/classes/
assertThat(getClass().getResource("/META-INF/build-info.properties")).isNotNull();
assertThat(getClass().getResource("/BOOT-INF/classes/META-INF/build-info.properties")).isNull();

SpringBootServiceVersionDetector guesser = new SpringBootServiceVersionDetector();
Resource result = guesser.createResource(config);
assertThat(result.getAttribute(SERVICE_VERSION)).isEqualTo("1.2.3");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ private Resource create() {
}

private Optional<String> getServiceVersionFromBuildInfo() {
try (InputStream in = system.openClasspathResource("META-INF", "build-info.properties")) {
// build-info.properties is placed by Spring Boot's buildInfo() at the jar root under META-INF/
// (not under BOOT-INF/classes/), so look it up at the jar root.
try (InputStream in = system.openJarRootResource("META-INF/build-info.properties")) {
return in != null ? getServiceVersionPropertyFromStream(in) : Optional.empty();
} catch (IOException e) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,22 @@ String getProperty(String key) {
return System.getProperty(key);
}

/**
* Opens a classpath resource that lives alongside the application's classes. In Spring Boot
* bootJar layouts the application classes live under {@code BOOT-INF/classes/}, so the prefix is
* applied when that layout is detected.
*/
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;
/**
* Opens a classpath resource that always lives at the jar root, regardless of bootJar layout.
* This is used for things like {@code META-INF/build-info.properties}, which Spring Boot places
* at the jar root rather than under {@code BOOT-INF/classes/}.
*/
InputStream openJarRootResource(String path) {
return classLoader.getResourceAsStream(path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@
@ExtendWith(MockitoExtension.class)
class SpringBootServiceVersionDetectorTest {

private static final String BUILD_PROPS = "build-info.properties";
private static final String META_INFO = "META-INF";
private static final String BUILD_INFO_PATH = "META-INF/build-info.properties";

@Mock ConfigProperties config;
@Mock SystemHelper system;

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

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

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

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

@Test
void givenBuildVersionFileIsPresentButBuildVersionPropertyNotPresent_thenReturnEmptyResource() {
when(system.openClasspathResource(META_INFO, BUILD_PROPS))
.thenReturn(openClasspathResource(BUILD_PROPS));
when(system.openJarRootResource(BUILD_INFO_PATH))
.thenReturn(openClasspathResource("build-info.properties"));

SpringBootServiceVersionDetector guesser = new SpringBootServiceVersionDetector(system);
Resource result = guesser.createResource(config);
Expand Down
Loading