Skip to content

Commit 2400cdb

Browse files
committed
detect service.name based on build-info.properties
1 parent d0763d9 commit 2400cdb

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

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

+34-8
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,27 @@
1111
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
1212
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
1313
import io.opentelemetry.sdk.resources.Resource;
14+
import io.opentelemetry.sdk.resources.ResourceBuilder;
1415
import io.opentelemetry.semconv.ResourceAttributes;
1516
import java.io.IOException;
1617
import java.io.InputStream;
1718
import java.util.Optional;
1819
import java.util.Properties;
1920
import java.util.logging.Logger;
2021

22+
/**
23+
* Detects <code>service.name</code> and <code>service.version</code> from Spring Boot's <code>
24+
* build-info.properties</code> file.
25+
*
26+
* <p><Use the following snippet in your gradle file to generate the build-info.properties file:
27+
*
28+
* <pre>{@code
29+
* springBoot {
30+
* buildInfo {
31+
* }
32+
* }
33+
* }</pre>
34+
*/
2135
@AutoService(ResourceProvider.class)
2236
public class SpringBootServiceVersionDetector implements ResourceProvider {
2337

@@ -37,29 +51,41 @@ public SpringBootServiceVersionDetector() {
3751

3852
@Override
3953
public Resource createResource(ConfigProperties config) {
40-
return getServiceVersionFromBuildInfo()
54+
return getPropertiesFromBuildInfo()
4155
.map(
42-
version -> {
43-
logger.log(FINE, "Auto-detected Spring Boot service version: {0}", version);
44-
return Resource.builder().put(ResourceAttributes.SERVICE_VERSION, version).build();
56+
properties -> {
57+
logger.log(FINE, "Auto-detected Spring Boot service version: {0}", properties);
58+
ResourceBuilder builder = Resource.builder();
59+
60+
String version = properties.getProperty("build.version");
61+
if (version != null) {
62+
builder.put(ResourceAttributes.SERVICE_VERSION, version);
63+
}
64+
65+
String name = properties.getProperty("build.name");
66+
if (name != null) {
67+
builder.put(ResourceAttributes.SERVICE_NAME, name);
68+
}
69+
70+
return builder.build();
4571
})
4672
.orElseGet(Resource::empty);
4773
}
4874

49-
private Optional<String> getServiceVersionFromBuildInfo() {
75+
private Optional<Properties> getPropertiesFromBuildInfo() {
5076
try (InputStream in = system.openClasspathResource("META-INF", "build-info.properties")) {
51-
return in != null ? getServiceVersionPropertyFromStream(in) : Optional.empty();
77+
return in != null ? getPropertiesFromStream(in) : Optional.empty();
5278
} catch (Exception e) {
5379
return Optional.empty();
5480
}
5581
}
5682

57-
private static Optional<String> getServiceVersionPropertyFromStream(InputStream in) {
83+
private static Optional<Properties> getPropertiesFromStream(InputStream in) {
5884
Properties properties = new Properties();
5985
try {
6086
// Note: load() uses ISO 8859-1 encoding, same as spring uses by default for property files
6187
properties.load(in);
62-
return Optional.ofNullable(properties.getProperty("build.version"));
88+
return Optional.of(properties);
6389
} catch (IOException e) {
6490
return Optional.empty();
6591
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.instrumentation.spring.resources;
77

8+
import static io.opentelemetry.semconv.ResourceAttributes.SERVICE_NAME;
89
import static io.opentelemetry.semconv.ResourceAttributes.SERVICE_VERSION;
910
import static org.assertj.core.api.Assertions.assertThat;
1011
import static org.mockito.Mockito.when;
@@ -34,6 +35,7 @@ void givenBuildVersionIsPresentInBuildInfProperties_thenReturnBuildVersion() {
3435
SpringBootServiceVersionDetector guesser = new SpringBootServiceVersionDetector(system);
3536
Resource result = guesser.createResource(config);
3637
assertThat(result.getAttribute(SERVICE_VERSION)).isEqualTo("0.0.2");
38+
assertThat(result.getAttribute(SERVICE_NAME)).isEqualTo("some-name");
3739
}
3840

3941
@Test
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
build.artifact=something
2-
build.name=some-name

0 commit comments

Comments
 (0)