-
Notifications
You must be signed in to change notification settings - Fork 867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Spring Boot service version finder / ResourceProvider #9480
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8982fcc
Add Spring Boot service version finder / ResourceProvider
Hayanesh cc1c5c7
Apply spotless
Hayanesh 282da0d
Handle build-info.properties file not present case
Hayanesh 67e285b
Merge branch 'open-telemetry:main' into main
Hayanesh fc03175
Added smoke test for SpringBootServiceVersionDetector
Hayanesh fb8d394
Merge branch 'main' into main
Hayanesh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
68 changes: 68 additions & 0 deletions
68
...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,68 @@ | ||||||
/* | ||||||
* 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.instrumentation.spring.resources.SpringBootServiceNameDetector.SystemHelper; | ||||||
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 | ||||||
public SpringBootServiceVersionDetector(SystemHelper system) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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("build-info.properties", "META-INF")) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think putting
Suggested change
|
||||||
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); | ||||||
laurit marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return Optional.ofNullable(properties.getProperty("build.version")); | ||||||
} catch (IOException e) { | ||||||
return Optional.empty(); | ||||||
} | ||||||
} | ||||||
} |
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 SpringBootServiceNameDetector.SystemHelper system; | ||
|
||
@Test | ||
void givenBuildVersionIsPresentInBuildInfProperties_thenReturnBuildVersion() { | ||
when(system.openClasspathResource(BUILD_PROPS, META_INFO)) | ||
.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(BUILD_PROPS, META_INFO)).thenReturn(null); | ||
|
||
SpringBootServiceVersionDetector guesser = new SpringBootServiceVersionDetector(system); | ||
Resource result = guesser.createResource(config); | ||
assertThat(result).isEqualTo(Resource.empty()); | ||
} | ||
|
||
@Test | ||
void givenBuildVersionFileIsPresentButBuildVersionPropertyNotPresent_thenReturnEmptyResource() { | ||
when(system.openClasspathResource(BUILD_PROPS, META_INFO)) | ||
.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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that the
SystemHelper
is used in two classes, WDYT about making it a top-level class?