Initialize appenders in the spring boot starter (#8888)

Co-authored-by: Lauri Tulmin <ltulmin@splunk.com>
This commit is contained in:
Mateusz Rzeszutek 2023-07-11 17:47:01 +02:00 committed by GitHub
parent 8011cb4af2
commit 8815952300
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 237 additions and 148 deletions

View File

@ -41,6 +41,10 @@ dependencies {
compileOnly("io.opentelemetry:opentelemetry-exporter-otlp")
compileOnly("io.opentelemetry:opentelemetry-exporter-zipkin")
compileOnly(project(":instrumentation-annotations"))
compileOnly(project(":instrumentation:log4j:log4j-appender-2.17:library"))
compileOnly("org.apache.logging.log4j:log4j-core:2.17.0")
compileOnly(project(":instrumentation:logback:logback-appender-1.0:library"))
compileOnly("ch.qos.logback:logback-classic:1.0.0")
compileOnly(project(":instrumentation:resources:library"))
annotationProcessor("com.google.auto.service:auto-service")
@ -77,24 +81,59 @@ if (latestDepTest) {
}
}
tasks.compileTestJava {
options.compilerArgs.add("-parameters")
}
testing {
suites {
val testLogbackAppender by registering(JvmTestSuite::class) {
dependencies {
implementation(project())
implementation(project(":testing-common"))
implementation("io.opentelemetry:opentelemetry-sdk")
implementation("io.opentelemetry:opentelemetry-sdk-testing")
implementation("org.springframework.boot:spring-boot-autoconfigure:$springBootVersion")
tasks.withType<Test>().configureEach {
usesService(gradle.sharedServices.registrations["testcontainersBuildService"].service)
systemProperty("testLatestDeps", latestDepTest)
// required on jdk17
jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED")
jvmArgs("-XX:+IgnoreUnrecognizedVMOptions")
// disable tests on openj9 18 because they often crash JIT compiler
val testJavaVersion = gradle.startParameter.projectProperties["testJavaVersion"]?.let(JavaVersion::toVersion)
val testOnOpenJ9 = gradle.startParameter.projectProperties["testJavaVM"]?.run { this == "openj9" }
?: false
if (testOnOpenJ9 && testJavaVersion?.majorVersion == "18") {
enabled = false
implementation(project(":instrumentation:logback:logback-appender-1.0:library"))
// using the same versions as in the spring-boot-autoconfigure
implementation("ch.qos.logback:logback-classic") {
version {
strictly("1.2.11")
}
}
implementation("org.slf4j:slf4j-api") {
version {
strictly("1.7.32")
}
}
}
}
}
}
tasks {
check {
dependsOn(testing.suites)
}
compileTestJava {
options.compilerArgs.add("-parameters")
}
test {
usesService(gradle.sharedServices.registrations["testcontainersBuildService"].service)
}
withType<Test>().configureEach {
systemProperty("testLatestDeps", latestDepTest)
// required on jdk17
jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED")
jvmArgs("-XX:+IgnoreUnrecognizedVMOptions")
// disable tests on openj9 18 because they often crash JIT compiler
val testJavaVersion = gradle.startParameter.projectProperties["testJavaVersion"]?.let(JavaVersion::toVersion)
val testOnOpenJ9 = gradle.startParameter.projectProperties["testJavaVM"]?.run { this == "openj9" }
?: false
if (testOnOpenJ9 && testJavaVersion?.majorVersion == "18") {
enabled = false
}
}
}

View File

@ -5,7 +5,6 @@
package io.opentelemetry.instrumentation.spring.autoconfigure;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.TracerProvider;
import io.opentelemetry.context.propagation.ContextPropagators;
@ -141,9 +140,6 @@ public class OpenTelemetryAutoConfiguration {
ContextPropagators propagators = propagatorsProvider.getIfAvailable(ContextPropagators::noop);
// global is needed for logging appenders
GlobalOpenTelemetry.set(OpenTelemetrySdk.builder().setLoggerProvider(loggerProvider).build());
return OpenTelemetrySdk.builder()
.setTracerProvider(tracerProvider)
.setMeterProvider(meterProvider)

View File

@ -0,0 +1,61 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.spring.autoconfigure.logging;
import io.opentelemetry.api.OpenTelemetry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@SuppressWarnings("OtelPrivateConstructorForUtilityClass")
@ConditionalOnBean(OpenTelemetry.class)
public class OpenTelemetryAppenderAutoConfiguration {
@Configuration
@ConditionalOnProperty(
prefix = "otel.springboot.log4j-appender",
name = "enabled",
matchIfMissing = true)
@ConditionalOnClass({
io.opentelemetry.instrumentation.log4j.appender.v2_17.OpenTelemetryAppender.class
})
static class Log4jAppenderConfig {
@Bean
ApplicationListener<ApplicationReadyEvent> log4jOtelAppenderInitializer(
OpenTelemetry openTelemetry) {
return event -> {
io.opentelemetry.instrumentation.log4j.appender.v2_17.OpenTelemetryAppender.install(
openTelemetry);
};
}
}
@Configuration
@ConditionalOnProperty(
prefix = "otel.springboot.logback-appender",
name = "enabled",
matchIfMissing = true)
@ConditionalOnClass({
io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender.class
})
static class LogbackAppenderConfig {
@Bean
ApplicationListener<ApplicationReadyEvent> logbackOtelAppenderInitializer(
OpenTelemetry openTelemetry) {
return event -> {
io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender.install(
openTelemetry);
};
}
}
}

View File

@ -10,6 +10,7 @@ io.opentelemetry.instrumentation.spring.autoconfigure.exporters.zipkin.ZipkinSpa
io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.resttemplate.RestTemplateAutoConfiguration,\
io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.webclient.WebClientAutoConfiguration,\
io.opentelemetry.instrumentation.spring.autoconfigure.kafka.KafkaInstrumentationAutoConfiguration,\
io.opentelemetry.instrumentation.spring.autoconfigure.logging.OpenTelemetryAppenderAutoConfiguration,\
io.opentelemetry.instrumentation.spring.autoconfigure.metrics.MicrometerShimAutoConfiguration,\
io.opentelemetry.instrumentation.spring.autoconfigure.propagators.PropagationAutoConfiguration,\
io.opentelemetry.instrumentation.spring.autoconfigure.resources.OtelResourceAutoConfiguration,\

View File

@ -9,6 +9,7 @@ io.opentelemetry.instrumentation.spring.autoconfigure.exporters.zipkin.ZipkinSpa
io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.resttemplate.RestTemplateAutoConfiguration
io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.webclient.WebClientAutoConfiguration
io.opentelemetry.instrumentation.spring.autoconfigure.kafka.KafkaInstrumentationAutoConfiguration
io.opentelemetry.instrumentation.spring.autoconfigure.logging.OpenTelemetryAppenderAutoConfiguration
io.opentelemetry.instrumentation.spring.autoconfigure.metrics.MicrometerShimAutoConfiguration
io.opentelemetry.instrumentation.spring.autoconfigure.propagators.PropagationAutoConfiguration
io.opentelemetry.instrumentation.spring.autoconfigure.resources.OtelResourceAutoConfiguration

View File

@ -8,7 +8,6 @@ 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.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.instrumentation.spring.autoconfigure.resources.OtelResourceAutoConfiguration;
@ -17,7 +16,6 @@ import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -37,11 +35,6 @@ class OpenTelemetryAutoConfigurationTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}
@Test
@DisplayName(
"when Application Context contains OpenTelemetry bean should NOT initialize openTelemetry")

View File

@ -7,9 +7,7 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.aspects;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -24,11 +22,6 @@ public class TraceAspectAutoConfigurationTest {
AutoConfigurations.of(
OpenTelemetryAutoConfiguration.class, TraceAspectAutoConfiguration.class));
@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}
@Test
@DisplayName("when aspects are ENABLED should initialize WithSpanAspect bean")
void aspectsEnabled() {

View File

@ -7,9 +7,7 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.exporters.jaeger;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -28,11 +26,6 @@ class JaegerSpanExporterAutoConfigurationTest {
AutoConfigurations.of(
OpenTelemetryAutoConfiguration.class, JaegerSpanExporterAutoConfiguration.class));
@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}
@Test
@DisplayName("when exporters are ENABLED should initialize JaegerGrpcSpanExporter bean")
void exportersEnabled() {

View File

@ -7,10 +7,8 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.exporters.logging;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.exporter.logging.LoggingMetricExporter;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@ -24,11 +22,6 @@ class LoggingMetricExporterAutoConfigurationTest {
OpenTelemetryAutoConfiguration.class,
LoggingMetricExporterAutoConfiguration.class));
@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}
@Test
void loggingEnabled() {
runner

View File

@ -7,10 +7,8 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.exporters.logging;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -26,11 +24,6 @@ class LoggingSpanExporterAutoConfigurationTest {
OpenTelemetryAutoConfiguration.class,
LoggingSpanExporterAutoConfiguration.class));
@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}
@Test
@DisplayName("when exporters are ENABLED should initialize LoggingSpanExporter bean")
void loggingEnabled() {

View File

@ -7,10 +7,8 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.exporters.otlp;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@ -23,11 +21,6 @@ class OtlpLogExporterAutoConfigurationTest {
AutoConfigurations.of(
OpenTelemetryAutoConfiguration.class, OtlpLoggerExporterAutoConfiguration.class));
@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}
@Test
void otlpEnabled() {
runner

View File

@ -7,10 +7,8 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.exporters.otlp;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@ -23,11 +21,6 @@ class OtlpMetricExporterAutoConfigurationTest {
AutoConfigurations.of(
OpenTelemetryAutoConfiguration.class, OtlpMetricExporterAutoConfiguration.class));
@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}
@Test
void otlpEnabled() {
runner

View File

@ -7,10 +7,8 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.exporters.otlp;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -25,11 +23,6 @@ class OtlpSpanExporterAutoConfigurationTest {
AutoConfigurations.of(
OpenTelemetryAutoConfiguration.class, OtlpSpanExporterAutoConfiguration.class));
@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}
@Test
@DisplayName("when exporters are ENABLED should initialize OtlpGrpcSpanExporter bean")
void otlpEnabled() {

View File

@ -7,10 +7,8 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.exporters.zipkin;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.exporter.zipkin.ZipkinSpanExporter;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -25,11 +23,6 @@ class ZipkinSpanExporterAutoConfigurationTest {
AutoConfigurations.of(
OpenTelemetryAutoConfiguration.class, ZipkinSpanExporterAutoConfiguration.class));
@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}
@Test
@DisplayName("when exporters are ENABLED should initialize ZipkinSpanExporter bean")
void exportersEnabled() {

View File

@ -7,9 +7,7 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.restte
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -24,11 +22,6 @@ class RestTemplateAutoConfigurationTest {
AutoConfigurations.of(
OpenTelemetryAutoConfiguration.class, RestTemplateAutoConfiguration.class));
@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}
@Test
@DisplayName("when httpclients are ENABLED should initialize RestTemplateInterceptor bean")
void httpClientsEnabled() {

View File

@ -7,9 +7,7 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.webcli
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -24,11 +22,6 @@ class WebClientAutoConfigurationTest {
AutoConfigurations.of(
OpenTelemetryAutoConfiguration.class, WebClientAutoConfiguration.class));
@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}
@Test
@DisplayName("when httpclients are ENABLED should initialize WebClientBeanPostProcessor bean")
void httpClientsEnabled() {

View File

@ -8,10 +8,8 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.metrics;
import static org.assertj.core.api.Assertions.assertThat;
import io.micrometer.core.instrument.MeterRegistry;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.micrometer.v1_5.OpenTelemetryMeterRegistry;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -25,11 +23,6 @@ class MicrometerShimAutoConfigurationTest {
AutoConfigurations.of(
OpenTelemetryAutoConfiguration.class, MicrometerShimAutoConfiguration.class));
@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}
@Test
void metricsEnabled() {
runner

View File

@ -7,10 +7,8 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.propagators;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -24,11 +22,6 @@ class PropagationAutoConfigurationTest {
AutoConfigurations.of(
OpenTelemetryAutoConfiguration.class, PropagationAutoConfiguration.class));
@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}
@Test
@DisplayName("when propagation is ENABLED should initialize PropagationAutoConfiguration bean")
void shouldBeConfigured() {

View File

@ -7,10 +7,8 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.propagators;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import java.util.Arrays;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -24,11 +22,6 @@ public class PropagationPropertiesTest {
AutoConfigurations.of(
OpenTelemetryAutoConfiguration.class, PropagationAutoConfiguration.class));
@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}
@Test
@DisplayName("when propagation is SET should set PropagationProperties with given propagators")
void hasType() {

View File

@ -7,9 +7,7 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.resources;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -22,11 +20,6 @@ public class OtelResourceAutoConfigurationTest {
AutoConfigurations.of(
OtelResourceAutoConfiguration.class, OpenTelemetryAutoConfiguration.class));
@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}
@Test
@DisplayName(
"when otel.springboot.resource.enabled is set to true configuration should be initialized")

View File

@ -8,8 +8,6 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.resources;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import io.opentelemetry.api.GlobalOpenTelemetry;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -21,11 +19,6 @@ public class OtelResourcePropertiesTest {
.withPropertyValues("otel.springboot.resource.enabled=true")
.withConfiguration(AutoConfigurations.of(OtelResourceAutoConfiguration.class));
@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}
@Test
@DisplayName("when attributes are SET should set OtelResourceProperties with given attributes")
void hasAttributes() {

View File

@ -8,8 +8,6 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.resources;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import io.opentelemetry.api.GlobalOpenTelemetry;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@ -19,11 +17,6 @@ import org.springframework.expression.spel.standard.SpelExpressionParser;
class SpringResourceConfigPropertiesTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}
@Test
@DisplayName("when map is set in properties in a row it should be available in config")
void shouldInitializeAttributesByMapInArow() {

View File

@ -8,7 +8,6 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.webmvc;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import jakarta.servlet.Filter;
import org.junit.jupiter.api.BeforeEach;
@ -29,7 +28,6 @@ class WebMvcFilterAutoConfigurationSpring6Test {
@BeforeEach
void setUp() {
assumeTrue(Boolean.getBoolean("testLatestDeps"));
GlobalOpenTelemetry.resetForTest();
}
@Test

View File

@ -8,7 +8,6 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.webmvc;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration;
import javax.servlet.Filter;
import org.junit.jupiter.api.BeforeEach;
@ -28,7 +27,6 @@ class WebMvcFilterAutoConfigurationTest {
@BeforeEach
void setUp() {
assumeFalse(Boolean.getBoolean("testLatestDeps"));
GlobalOpenTelemetry.resetForTest();
}
@Test

View File

@ -0,0 +1,87 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.spring.autoconfigure.logging;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender;
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
class LogbackAppenderTest {
@RegisterExtension
static final InstrumentationExtension testing = LibraryInstrumentationExtension.create();
@RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create();
@BeforeEach
void setUp() {
// reset the appender
OpenTelemetryAppender.install(null);
}
@Configuration
static class TestingOpenTelemetryConfiguration {
@Bean
public OpenTelemetry openTelemetry() {
return testing.getOpenTelemetry();
}
}
@Test
void shouldInitializeAppender() {
Map<String, Object> properties = new HashMap<>();
properties.put("logging.config", "classpath:logback-test.xml");
SpringApplication app =
new SpringApplication(
TestingOpenTelemetryConfiguration.class, OpenTelemetryAppenderAutoConfiguration.class);
app.setDefaultProperties(properties);
ConfigurableApplicationContext context = app.run();
cleanup.deferCleanup(context);
LoggerFactory.getLogger("test").info("test log message");
assertThat(testing.logRecords())
.anySatisfy(
logRecord -> {
assertThat(logRecord.getInstrumentationScopeInfo().getName()).isEqualTo("test");
assertThat(logRecord.getBody().asString()).contains("test log message");
});
}
@Test
void shouldNotInitializeAppenderWhenDisabled() {
Map<String, Object> properties = new HashMap<>();
properties.put("logging.config", "classpath:logback-test.xml");
properties.put("otel.springboot.logback-appender.enabled", "false");
SpringApplication app =
new SpringApplication(
TestingOpenTelemetryConfiguration.class, OpenTelemetryAppenderAutoConfiguration.class);
app.setDefaultProperties(properties);
ConfigurableApplicationContext context = app.run();
cleanup.deferCleanup(context);
LoggerFactory.getLogger("test").info("test log message");
assertThat(testing.logRecords()).isEmpty();
}
}

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<appender name="OpenTelemetry"
class="io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender"/>
<root level="INFO">
<appender-ref ref="console"/>
<appender-ref ref="OpenTelemetry"/>
</root>
</configuration>

View File

@ -13,12 +13,15 @@ import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.export.MetricReader;
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
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.ReadWriteSpan;
@ -28,7 +31,6 @@ import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
@ -41,6 +43,7 @@ public final class LibraryTestRunner extends InstrumentationTestRunner {
private static final OpenTelemetrySdk openTelemetry;
private static final InMemorySpanExporter testSpanExporter;
private static final InMemoryMetricExporter testMetricExporter;
private static final InMemoryLogRecordExporter testLogRecordExporter;
private static final MetricReader metricReader;
private static boolean forceFlushCalled;
@ -49,6 +52,7 @@ public final class LibraryTestRunner extends InstrumentationTestRunner {
testSpanExporter = InMemorySpanExporter.create();
testMetricExporter = InMemoryMetricExporter.create(AggregationTemporality.DELTA);
testLogRecordExporter = InMemoryLogRecordExporter.create();
metricReader =
PeriodicMetricReader.builder(testMetricExporter)
@ -66,6 +70,10 @@ public final class LibraryTestRunner extends InstrumentationTestRunner {
.addSpanProcessor(SimpleSpanProcessor.create(testSpanExporter))
.build())
.setMeterProvider(SdkMeterProvider.builder().registerMetricReader(metricReader).build())
.setLoggerProvider(
SdkLoggerProvider.builder()
.addLogRecordProcessor(SimpleLogRecordProcessor.create(testLogRecordExporter))
.build())
.setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
.buildAndRegisterGlobal();
}
@ -98,6 +106,7 @@ public final class LibraryTestRunner extends InstrumentationTestRunner {
openTelemetry.getSdkMeterProvider().forceFlush().join(10, TimeUnit.SECONDS);
testSpanExporter.reset();
testMetricExporter.reset();
testLogRecordExporter.reset();
forceFlushCalled = false;
}
@ -123,8 +132,7 @@ public final class LibraryTestRunner extends InstrumentationTestRunner {
@Override
public List<LogRecordData> getExportedLogRecords() {
// no logs support yet
return Collections.emptyList();
return testLogRecordExporter.getFinishedLogRecordItems();
}
@Override