Skip to content

Commit

Permalink
spring starter shouldn't depend on spring-boot-resources, which uses …
Browse files Browse the repository at this point in the history
…snake yaml, which doesn't work with graal native
  • Loading branch information
zeitlinger committed Feb 28, 2024
1 parent a98a2c2 commit 8709349
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ dependencies {
compileOnly(project(":instrumentation-annotations"))

compileOnly(project(":instrumentation:resources:library"))
compileOnly(project(":instrumentation:spring:spring-boot-resources:library"))
annotationProcessor("com.google.auto.service:auto-service")
compileOnly("com.google.auto.service:auto-service-annotations")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
import io.opentelemetry.instrumentation.resources.ProcessRuntimeResourceProvider;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.SdkEnabled;
import io.opentelemetry.instrumentation.spring.resources.SpringBootServiceNameDetector;
import io.opentelemetry.instrumentation.spring.resources.SpringBootServiceVersionDetector;
import io.opentelemetry.sdk.autoconfigure.internal.EnvironmentResourceProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
import java.util.Optional;
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.Conditional;
import org.springframework.context.annotation.Configuration;
Expand All @@ -42,27 +43,16 @@ public ResourceProvider otelEnvironmentResourceProvider() {
}

@Bean
public ResourceProvider otelSpringResourceProvider() {
return new SpringResourceProvider();
public ResourceProvider otelSpringResourceProvider(
@Autowired(required = false) BuildProperties buildProperties) {
return new SpringResourceProvider(Optional.ofNullable(buildProperties));
}

@Bean
public ResourceProvider otelDistroVersionResourceProvider() {
return new DistroVersionResourceProvider();
}

@Bean
@ConditionalOnClass(SpringBootServiceNameDetector.class)
public ResourceProvider otelSpringBootServiceNameResourceProvider() {
return new SpringBootServiceNameDetector();
}

@Bean
@ConditionalOnClass(SpringBootServiceVersionDetector.class)
public ResourceProvider otelSpringBootServiceVersionResourceProvider() {
return new SpringBootServiceVersionDetector();
}

@Bean
@ConditionalOnClass(OsResource.class)
public ResourceProvider otelOsResourceProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,33 @@
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 Optional<BuildProperties> buildProperties;

public SpringResourceProvider(Optional<BuildProperties> buildProperties) {
this.buildProperties = buildProperties;
}

@Override
public Resource createResource(ConfigProperties configProperties) {
AttributesBuilder attributesBuilder = Attributes.builder();
buildProperties
.map(BuildProperties::getName)
.ifPresent(v -> attributesBuilder.put(ResourceAttributes.SERVICE_NAME, v));

String springApplicationName = configProperties.getString("spring.application.name");
if (springApplicationName != null) {
attributesBuilder.put(ResourceAttributes.SERVICE_NAME, springApplicationName);
}

buildProperties
.map(BuildProperties::getVersion)
.ifPresent(v -> attributesBuilder.put(ResourceAttributes.SERVICE_VERSION, v));

return Resource.create(attributesBuilder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.instrumentation.spring.autoconfigure;

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 io.opentelemetry.api.OpenTelemetry;
Expand All @@ -16,9 +17,11 @@
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import java.util.Properties;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.info.BuildProperties;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -117,6 +120,27 @@ void shouldDetermineServiceNameBySpringApplicationName() {
});
}

@Test
@DisplayName(
"when spring.application.name is set value should be passed to service name attribute")
void shouldDetermineServiceNameAndVersionBySpringApplicationVersion() {
Properties properties = new Properties();
properties.put("name", "demo");
properties.put("version", "0.3");
this.contextRunner
.withBean("buildProperties", BuildProperties.class, () -> new BuildProperties(properties))
.withConfiguration(
AutoConfigurations.of(
OtelResourceAutoConfiguration.class, OpenTelemetryAutoConfiguration.class))
.run(
context -> {
Resource otelResource = context.getBean("otelResource", Resource.class);

assertThat(otelResource.getAttribute(SERVICE_NAME)).isEqualTo("demo");
assertThat(otelResource.getAttribute(SERVICE_VERSION)).isEqualTo("0.3");
});
}

@Test
@DisplayName(
"when spring application name and otel service name are not set service name should be default")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
* <li>Check for --spring.application.name program argument via sun.java.command system property
* </ul>
*
* <p>Note: should not be used inside a spring application, where the spring.application.name is
* already available.
* <p>Note: The spring starter already includes provider in
* io.opentelemetry.instrumentation.spring.autoconfigure.resources.SpringResourceProvider
*/
@AutoService(ResourceProvider.class)
public class SpringBootServiceNameDetector implements ConditionalResourceProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import java.util.Properties;
import java.util.logging.Logger;

/**
* Note: The spring starter already includes provider in
* io.opentelemetry.instrumentation.spring.autoconfigure.resources.SpringResourceProvider
*/
@AutoService(ResourceProvider.class)
public class SpringBootServiceVersionDetector implements ResourceProvider {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dependencies {
api(project(":instrumentation:spring:spring-boot-autoconfigure"))
api(project(":instrumentation-annotations"))
implementation(project(":instrumentation:resources:library"))
implementation(project(":instrumentation:spring:spring-boot-resources:library"))
api("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi")
api("io.opentelemetry:opentelemetry-api")
api("io.opentelemetry:opentelemetry-exporter-logging")
Expand Down

0 comments on commit 8709349

Please sign in to comment.