Skip to content

Commit

Permalink
add service.version detection to spring boot starter
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Feb 10, 2024
1 parent 837cce2 commit 674f295
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ target
!**/gradle/wrapper/*
.gradle
**/build/
**/generated/
examples/**/build/

# Eclipse #
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
import io.opentelemetry.instrumentation.resources.ProcessRuntimeResourceProvider;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.info.BuildProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Optional;

@Configuration
@EnableConfigurationProperties({OtelSpringResourceProperties.class, OtelResourceProperties.class})
Expand All @@ -35,9 +37,9 @@ public class OtelResourceAutoConfiguration {
public ResourceProvider otelResourceProvider(
OtelSpringResourceProperties otelSpringResourceProperties,
OtelResourceProperties otelResourceProperties,
BuildProperties buildProperties) {
@Autowired(required = false) BuildProperties buildProperties) {
return new SpringResourceProvider(
otelSpringResourceProperties, otelResourceProperties, buildProperties);
otelSpringResourceProperties, otelResourceProperties, Optional.ofNullable(buildProperties));
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.ResourceAttributes;
import java.util.Optional;
import org.springframework.boot.info.BuildProperties;

public class SpringResourceProvider implements ResourceProvider {

private final OtelSpringResourceProperties otelSpringResourceProperties;
private final OtelResourceProperties otelResourceProperties;
private final BuildProperties buildProperties;
private final Optional<BuildProperties> buildProperties;

public SpringResourceProvider(
OtelSpringResourceProperties otelSpringResourceProperties,
OtelResourceProperties otelResourceProperties,
BuildProperties buildProperties) {
Optional<BuildProperties> buildProperties) {
this.otelSpringResourceProperties = otelSpringResourceProperties;
this.otelResourceProperties = otelResourceProperties;
this.buildProperties = buildProperties;
Expand All @@ -41,10 +42,9 @@ public Resource createResource(ConfigProperties configProperties) {
if (applicationName != null) {
attributesBuilder.put(ResourceAttributes.SERVICE_NAME, applicationName);
}
String version = buildProperties.getVersion();
if (version != null) {
attributesBuilder.put(ResourceAttributes.SERVICE_VERSION, version);
}
buildProperties
.map(entries -> entries.get("build.version"))
.ifPresent(v -> attributesBuilder.put(ResourceAttributes.SERVICE_VERSION, v));
return Resource.create(attributesBuilder.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void shouldDetermineServiceNameBySpringApplicationName() {
"when spring.application.name is set value should be passed to service name attribute")
void shouldDetermineServiceVersionBySpringApplicationVersion() {
Properties properties = new Properties();
properties.put("version", "0.3");
properties.put("build.version", "0.3");
this.contextRunner
.withBean("buildProperties", BuildProperties.class, () -> new BuildProperties(properties))
.withConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* <li>Check for --spring.application.name program argument via sun.java.command system property
* </ul>
*
* Note: should not be used inside a spring application, where the spring.application.name is
* <p>Note: should not be used inside a spring application, where the spring.application.name is
* already available.
*/
@AutoService(ResourceProvider.class)
Expand Down
4 changes: 4 additions & 0 deletions smoke-tests-otel-starter/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ configurations.configureEach {
exclude("org.spockframework", "spock-core")
}

springBoot {
buildInfo()
}

graalvmNative {
binaries.all {
// Workaround for https://github.com/junit-team/junit5/issues/3405
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions;
import io.opentelemetry.sdk.testing.assertj.TracesAssert;
import io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricExporter;
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.opentelemetry.semconv.ResourceAttributes;
import io.opentelemetry.semconv.SemanticAttributes;
import io.opentelemetry.spring.smoketest.OtelSpringStarterSmokeTestApplication;
import io.opentelemetry.spring.smoketest.OtelSpringStarterSmokeTestController;
import java.util.List;
import org.assertj.core.api.AbstractCharSequenceAssert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down Expand Up @@ -99,6 +102,12 @@ void shouldSendTelemetry() throws InterruptedException {
spanDataAssert ->
spanDataAssert
.hasKind(SpanKind.SERVER)
.hasResourceSatisfying(
r ->
r.hasAttributesSatisfying(
OpenTelemetryAssertions.satisfies(
ResourceAttributes.SERVICE_VERSION,
AbstractCharSequenceAssert::isNotBlank)))
.hasAttribute(SemanticAttributes.HTTP_REQUEST_METHOD, "GET")
.hasAttribute(SemanticAttributes.HTTP_RESPONSE_STATUS_CODE, 200L)
.hasAttribute(SemanticAttributes.HTTP_ROUTE, "/ping")));
Expand Down

0 comments on commit 674f295

Please sign in to comment.