-
Notifications
You must be signed in to change notification settings - Fork 870
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spring Boot Starter service-name is constant
Pattern-based resource configuration
- Loading branch information
1 parent
d2bd06e
commit db474af
Showing
9 changed files
with
317 additions
and
2 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
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
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
79 changes: 79 additions & 0 deletions
79
.../io/opentelemetry/instrumentation/spring/autoconfigure/OtelResourceAutoConfiguration.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,79 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.autoconfigure; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import io.opentelemetry.sdk.extension.resources.ContainerResource; | ||
import io.opentelemetry.sdk.extension.resources.HostResource; | ||
import io.opentelemetry.sdk.extension.resources.OsResource; | ||
import io.opentelemetry.sdk.extension.resources.ProcessResource; | ||
import io.opentelemetry.sdk.extension.resources.ProcessRuntimeResource; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
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.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.env.Environment; | ||
|
||
@Configuration | ||
@EnableConfigurationProperties(OtelResourceProperties.class) | ||
@AutoConfigureBefore(OpenTelemetryAutoConfiguration.class) | ||
@ConditionalOnProperty(prefix = "otel.springboot.resource", name = "enabled", matchIfMissing = true) | ||
public class OtelResourceAutoConfiguration { | ||
|
||
@Bean | ||
public Supplier<Resource> otelResourceProvider( | ||
Environment env, OtelResourceProperties otelResourceProperties) { | ||
return () -> { | ||
AttributesBuilder attributesBuilder = Attributes.builder(); | ||
for (Map.Entry<String, String> entry : otelResourceProperties.getAttributes().entrySet()) { | ||
attributesBuilder.put(entry.getKey(), entry.getValue()); | ||
} | ||
String otelServiceName = env.getProperty("otel.springboot.resource.attributes.service.name"); | ||
if (otelServiceName != null) { | ||
attributesBuilder.put(ResourceAttributes.SERVICE_NAME, otelServiceName); | ||
} | ||
Attributes attributes = attributesBuilder.build(); | ||
return Resource.create(attributes); | ||
}; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnClass(OsResource.class) | ||
public Supplier<Resource> otelOsResourceProvider() { | ||
return OsResource::get; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnClass(ProcessResource.class) | ||
public Supplier<Resource> otelProcessResourceProvider() { | ||
return ProcessResource::get; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnClass(ProcessRuntimeResource.class) | ||
public Supplier<Resource> otelProcessRuntimeResourceProvider() { | ||
return ProcessRuntimeResource::get; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnClass(HostResource.class) | ||
public Supplier<Resource> otelHostResourceProvider() { | ||
return HostResource::get; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnClass(ContainerResource.class) | ||
public Supplier<Resource> otelContainerResource() { | ||
return ContainerResource::get; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...in/java/io/opentelemetry/instrumentation/spring/autoconfigure/OtelResourceProperties.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,23 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.autoconfigure; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@ConfigurationProperties(prefix = "otel.springboot.resource") | ||
public class OtelResourceProperties { | ||
private Map<String, String> attributes = Collections.emptyMap(); | ||
|
||
public Map<String, String> getAttributes() { | ||
return attributes; | ||
} | ||
|
||
public void setAttributes(Map<String, String> attributes) { | ||
this.attributes = attributes; | ||
} | ||
} |
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
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
89 changes: 89 additions & 0 deletions
89
...opentelemetry/instrumentation/spring/autoconfigure/OtelResourceAutoConfigurationTest.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,89 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.autoconfigure; | ||
|
||
import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.SERVICE_NAME; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
|
||
public class OtelResourceAutoConfigurationTest { | ||
private final ApplicationContextRunner contextRunner = | ||
new ApplicationContextRunner() | ||
.withConfiguration( | ||
AutoConfigurations.of( | ||
OtelResourceAutoConfiguration.class, OpenTelemetryAutoConfiguration.class)); | ||
|
||
@AfterEach | ||
void tearDown() { | ||
GlobalOpenTelemetry.resetForTest(); | ||
} | ||
|
||
@Test | ||
@DisplayName("when otel service name is set it should be set as service name attribute") | ||
void shouldDetermineServiceNameByOtelServiceName() { | ||
this.contextRunner | ||
.withPropertyValues( | ||
"otel.springboot.resource.attributes.service.name=otel-name-backend", | ||
"otel.springboot.resource.enabled=true") | ||
.run( | ||
context -> { | ||
Resource otelResource = context.getBean("otelResource", Resource.class); | ||
|
||
assertThat(otelResource.getAttribute(SERVICE_NAME)).isEqualTo("otel-name-backend"); | ||
}); | ||
} | ||
|
||
@Test | ||
@DisplayName( | ||
"when otel.springboot.resource.enabled is not specified configuration should be initialized") | ||
void shouldInitAutoConfigurationByDefault() { | ||
this.contextRunner | ||
.withPropertyValues("otel.springboot.resource.attributes.service.name=otel-name-backend") | ||
.run( | ||
context -> { | ||
Resource otelResource = context.getBean("otelResource", Resource.class); | ||
|
||
assertThat(otelResource.getAttribute(SERVICE_NAME)).isEqualTo("otel-name-backend"); | ||
}); | ||
} | ||
|
||
@Test | ||
@DisplayName( | ||
"when otel.springboot.resource.enabled is set to false configuration should NOT be initialized") | ||
void shouldNotInitAutoConfiguration() { | ||
this.contextRunner | ||
.withPropertyValues( | ||
"otel.springboot.resource.attributes.service.name=otel-name-backend", | ||
"otel.springboot.resource.enabled=false") | ||
.run(context -> assertThat(context.containsBean("otelResourceProvider")).isFalse()); | ||
} | ||
|
||
@Test | ||
@DisplayName("when otel attributes are set in properties they should be put in resource") | ||
void shouldInitializeAttributes() { | ||
this.contextRunner | ||
.withPropertyValues( | ||
"otel.springboot.resource.attributes.xyz=foo", | ||
"otel.springboot.resource.attributes.environment=dev", | ||
"otel.springboot.resource.enabled=true") | ||
.run( | ||
context -> { | ||
Resource otelResource = context.getBean("otelResource", Resource.class); | ||
|
||
assertThat(otelResource.getAttribute(AttributeKey.stringKey("environment"))) | ||
.isEqualTo("dev"); | ||
assertThat(otelResource.getAttribute(AttributeKey.stringKey("xyz"))).isEqualTo("foo"); | ||
}); | ||
} | ||
} |
Oops, something went wrong.