Skip to content

Commit

Permalink
detect service.name based on build-info.properties
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Feb 11, 2024
1 parent d0763d9 commit 2400cdb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,27 @@
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.resources.ResourceBuilder;
import io.opentelemetry.semconv.ResourceAttributes;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.Properties;
import java.util.logging.Logger;

/**
* Detects <code>service.name</code> and <code>service.version</code> from Spring Boot's <code>
* build-info.properties</code> file.
*
* <p><Use the following snippet in your gradle file to generate the build-info.properties file:
*
* <pre>{@code
* springBoot {
* buildInfo {
* }
* }
* }</pre>
*/
@AutoService(ResourceProvider.class)
public class SpringBootServiceVersionDetector implements ResourceProvider {

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

@Override
public Resource createResource(ConfigProperties config) {
return getServiceVersionFromBuildInfo()
return getPropertiesFromBuildInfo()
.map(
version -> {
logger.log(FINE, "Auto-detected Spring Boot service version: {0}", version);
return Resource.builder().put(ResourceAttributes.SERVICE_VERSION, version).build();
properties -> {
logger.log(FINE, "Auto-detected Spring Boot service version: {0}", properties);
ResourceBuilder builder = Resource.builder();

String version = properties.getProperty("build.version");
if (version != null) {
builder.put(ResourceAttributes.SERVICE_VERSION, version);
}

String name = properties.getProperty("build.name");
if (name != null) {
builder.put(ResourceAttributes.SERVICE_NAME, name);
}

return builder.build();
})
.orElseGet(Resource::empty);
}

private Optional<String> getServiceVersionFromBuildInfo() {
private Optional<Properties> getPropertiesFromBuildInfo() {
try (InputStream in = system.openClasspathResource("META-INF", "build-info.properties")) {
return in != null ? getServiceVersionPropertyFromStream(in) : Optional.empty();
return in != null ? getPropertiesFromStream(in) : Optional.empty();
} catch (Exception e) {
return Optional.empty();
}
}

private static Optional<String> getServiceVersionPropertyFromStream(InputStream in) {
private static Optional<Properties> getPropertiesFromStream(InputStream in) {
Properties properties = new Properties();
try {
// Note: load() uses ISO 8859-1 encoding, same as spring uses by default for property files
properties.load(in);
return Optional.ofNullable(properties.getProperty("build.version"));
return Optional.of(properties);
} catch (IOException e) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.instrumentation.spring.resources;

import static io.opentelemetry.semconv.ResourceAttributes.SERVICE_NAME;
import static io.opentelemetry.semconv.ResourceAttributes.SERVICE_VERSION;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -34,6 +35,7 @@ void givenBuildVersionIsPresentInBuildInfProperties_thenReturnBuildVersion() {
SpringBootServiceVersionDetector guesser = new SpringBootServiceVersionDetector(system);
Resource result = guesser.createResource(config);
assertThat(result.getAttribute(SERVICE_VERSION)).isEqualTo("0.0.2");
assertThat(result.getAttribute(SERVICE_NAME)).isEqualTo("some-name");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
build.artifact=something
build.name=some-name

0 comments on commit 2400cdb

Please sign in to comment.