-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Spring Boot service version finder / ResourceProvider (#9480)
- Loading branch information
Showing
8 changed files
with
212 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...a/io/opentelemetry/instrumentation/spring/resources/SpringBootServiceVersionDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.resources; | ||
|
||
import static java.util.logging.Level.FINE; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
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; | ||
|
||
@AutoService(ResourceProvider.class) | ||
public class SpringBootServiceVersionDetector implements ResourceProvider { | ||
|
||
private static final Logger logger = | ||
Logger.getLogger(SpringBootServiceVersionDetector.class.getName()); | ||
|
||
private final SystemHelper system; | ||
|
||
public SpringBootServiceVersionDetector() { | ||
this.system = new SystemHelper(); | ||
} | ||
|
||
// Exists for testing | ||
SpringBootServiceVersionDetector(SystemHelper system) { | ||
this.system = system; | ||
} | ||
|
||
@Override | ||
public Resource createResource(ConfigProperties config) { | ||
return getServiceVersionFromBuildInfo() | ||
.map( | ||
version -> { | ||
logger.log(FINE, "Auto-detected Spring Boot service version: {0}", version); | ||
return Resource.builder().put(ResourceAttributes.SERVICE_VERSION, version).build(); | ||
}) | ||
.orElseGet(Resource::empty); | ||
} | ||
|
||
private Optional<String> getServiceVersionFromBuildInfo() { | ||
try (InputStream in = system.openClasspathResource("META-INF", "build-info.properties")) { | ||
return in != null ? getServiceVersionPropertyFromStream(in) : Optional.empty(); | ||
} catch (Exception e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
private static Optional<String> getServiceVersionPropertyFromStream(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")); | ||
} catch (IOException e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...library/src/main/java/io/opentelemetry/instrumentation/spring/resources/SystemHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.resources; | ||
|
||
import java.io.InputStream; | ||
import java.lang.reflect.Method; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.Optional; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
class SystemHelper { | ||
private static final Logger logger = Logger.getLogger(SystemHelper.class.getName()); | ||
|
||
private final ClassLoader classLoader; | ||
private final boolean addBootInfPrefix; | ||
|
||
SystemHelper() { | ||
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); | ||
classLoader = | ||
contextClassLoader != null ? contextClassLoader : ClassLoader.getSystemClassLoader(); | ||
addBootInfPrefix = classLoader.getResource("BOOT-INF/classes/") != null; | ||
if (addBootInfPrefix) { | ||
logger.log(Level.FINER, "Detected presence of BOOT-INF/classes/"); | ||
} | ||
} | ||
|
||
String getenv(String name) { | ||
return System.getenv(name); | ||
} | ||
|
||
String getProperty(String key) { | ||
return System.getProperty(key); | ||
} | ||
|
||
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; | ||
return classLoader.getResourceAsStream(path); | ||
} | ||
|
||
InputStream openFile(String filename) throws Exception { | ||
return Files.newInputStream(Paths.get(filename)); | ||
} | ||
|
||
/** | ||
* Attempts to use ProcessHandle to get the full commandline of the current process (including the | ||
* main method arguments). Will only succeed on java 9+. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
String[] attemptGetCommandLineArgsViaReflection() throws Exception { | ||
Class<?> clazz = Class.forName("java.lang.ProcessHandle"); | ||
Method currentMethod = clazz.getDeclaredMethod("current"); | ||
Method infoMethod = clazz.getDeclaredMethod("info"); | ||
Object currentInstance = currentMethod.invoke(null); | ||
Object info = infoMethod.invoke(currentInstance); | ||
Class<?> infoClass = Class.forName("java.lang.ProcessHandle$Info"); | ||
Method argumentsMethod = infoClass.getMethod("arguments"); | ||
Optional<String[]> optionalArgs = (Optional<String[]>) argumentsMethod.invoke(info); | ||
return optionalArgs.orElse(new String[0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
.../opentelemetry/instrumentation/spring/resources/SpringBootServiceVersionDetectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.resources; | ||
|
||
import static io.opentelemetry.semconv.ResourceAttributes.SERVICE_VERSION; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import java.io.InputStream; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class SpringBootServiceVersionDetectorTest { | ||
|
||
static final String BUILD_PROPS = "build-info.properties"; | ||
static final String META_INFO = "META-INF"; | ||
|
||
@Mock ConfigProperties config; | ||
@Mock SystemHelper system; | ||
|
||
@Test | ||
void givenBuildVersionIsPresentInBuildInfProperties_thenReturnBuildVersion() { | ||
when(system.openClasspathResource(META_INFO, BUILD_PROPS)) | ||
.thenReturn(openClasspathResource(META_INFO + "/" + BUILD_PROPS)); | ||
|
||
SpringBootServiceVersionDetector guesser = new SpringBootServiceVersionDetector(system); | ||
Resource result = guesser.createResource(config); | ||
assertThat(result.getAttribute(SERVICE_VERSION)).isEqualTo("0.0.2"); | ||
} | ||
|
||
@Test | ||
void givenBuildVersionFileNotPresent_thenReturnEmptyResource() { | ||
when(system.openClasspathResource(META_INFO, BUILD_PROPS)).thenReturn(null); | ||
|
||
SpringBootServiceVersionDetector guesser = new SpringBootServiceVersionDetector(system); | ||
Resource result = guesser.createResource(config); | ||
assertThat(result).isEqualTo(Resource.empty()); | ||
} | ||
|
||
@Test | ||
void givenBuildVersionFileIsPresentButBuildVersionPropertyNotPresent_thenReturnEmptyResource() { | ||
when(system.openClasspathResource(META_INFO, BUILD_PROPS)) | ||
.thenReturn(openClasspathResource(BUILD_PROPS)); | ||
|
||
SpringBootServiceVersionDetector guesser = new SpringBootServiceVersionDetector(system); | ||
Resource result = guesser.createResource(config); | ||
assertThat(result).isEqualTo(Resource.empty()); | ||
} | ||
|
||
private InputStream openClasspathResource(String resource) { | ||
return getClass().getClassLoader().getResourceAsStream(resource); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...on/spring/spring-boot-resources/library/src/test/resources/META-INF/build-info.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
build.artifact=something | ||
build.name=some-name | ||
build.version=0.0.2 |
2 changes: 2 additions & 0 deletions
2
...rumentation/spring/spring-boot-resources/library/src/test/resources/build-info.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
build.artifact=something | ||
build.name=some-name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters