Skip to content
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

Initialize appenders in the spring boot starter #8888

Merged
merged 4 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 53 additions & 14 deletions instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Comment on lines +44 to +47
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think about pulling in the appender instrumentation by default, similar to the other library instrumentations?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

created #8925 to track/discuss further, will merge this PR as is for the upcoming 1.28.0 release


compileOnly(project(":instrumentation:resources:library"))
annotationProcessor("com.google.auto.service:auto-service")
Expand Down Expand Up @@ -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")

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.withType<Test>().configureEach {
usesService(gradle.sharedServices.registrations["testcontainersBuildService"].service)
tasks {
check {
dependsOn(testing.suites)
}

compileTestJava {
options.compilerArgs.add("-parameters")
}

test {
usesService(gradle.sharedServices.registrations["testcontainersBuildService"].service)
}

systemProperty("testLatestDeps", latestDepTest)
withType<Test>().configureEach {
systemProperty("testLatestDeps", latestDepTest)

// required on jdk17
jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED")
jvmArgs("-XX:+IgnoreUnrecognizedVMOptions")
// 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
// 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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -141,9 +140,6 @@ public OpenTelemetry openTelemetry(

ContextPropagators propagators = propagatorsProvider.getIfAvailable(ContextPropagators::noop);

// global is needed for logging appenders
GlobalOpenTelemetry.set(OpenTelemetrySdk.builder().setLoggerProvider(loggerProvider).build());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An OpenTelemtry instance is built twice: on line 145 and just after


return OpenTelemetrySdk.builder()
.setTracerProvider(tracerProvider)
.setMeterProvider(meterProvider)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 {
mateuszrzeszutek marked this conversation as resolved.
Show resolved Hide resolved

@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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think about adding the otel appender if it doesn't exist? One thing less for the user to do.

Here's how I did it: https://github.com/grafana/grafana-opentelemetry-starter/blob/main/src/main/java/com/grafana/opentelemetry/Log4jConfig.java

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great. We would have to provide a way to customize the OTel automatically added. Today, users can do this with an XML configuration in their Spring Boot projects. It could be done for example by providing a Spring bean of the OTel appender automatically added. The user could then customize this bean with a Spring post-processor.
The automatic addition of the OTel appenders could perhaps be discussed in an issue and not be implemented in this PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would have to provide a way to customize the OTel automatically added

Users can still add the appender (using XML) if they want to have some special settings.
My proposal is to add the appender only if no otel appender is found.

separate issue

sure, I don't mind 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have created an issue for tracking and discussions: #8920

openTelemetry);
};
}
}

@Configuration
@ConditionalOnProperty(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not to enable the OpenTelemetry injection by default? Check on line 44 is not sufficient?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is enabled by default; in case the property is absent it still gets evaluated to true. I added it to make it possible to disable the appender auto configuration.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was not rather explicit. My point is that I don't see the use case for which the user would like to disable the automatic injection of the OpenTelemetry instance into the OTel appender. I would prefer to remove the property if we don't have a use case in mind.

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) {
System.out.println("listener bean");
mateuszrzeszutek marked this conversation as resolved.
Show resolved Hide resolved
return event -> {
System.out.println("running the listener");
mateuszrzeszutek marked this conversation as resolved.
Show resolved Hide resolved
io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender.install(
openTelemetry);
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
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;
Expand All @@ -17,7 +16,6 @@
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;
Expand All @@ -37,11 +35,6 @@ public OpenTelemetry customOpenTelemetry() {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();

@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}

@Test
@DisplayName(
"when Application Context contains OpenTelemetry bean should NOT initialize openTelemetry")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.exporter.jaeger.JaegerGrpcSpanExporter;
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;
Expand All @@ -25,11 +23,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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

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;
Expand All @@ -24,11 +22,6 @@ class LoggingMetricExporterAutoConfigurationTest {
OpenTelemetryAutoConfiguration.class,
LoggingMetricExporterAutoConfiguration.class));

@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}

@Test
void loggingEnabled() {
runner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

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;
Expand All @@ -23,11 +21,6 @@ class OtlpLogExporterAutoConfigurationTest {
AutoConfigurations.of(
OpenTelemetryAutoConfiguration.class, OtlpLoggerExporterAutoConfiguration.class));

@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}

@Test
void otlpEnabled() {
runner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

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;
Expand All @@ -23,11 +21,6 @@ class OtlpMetricExporterAutoConfigurationTest {
AutoConfigurations.of(
OpenTelemetryAutoConfiguration.class, OtlpMetricExporterAutoConfiguration.class));

@BeforeEach
void resetGlobalLoggerProvider() {
GlobalOpenTelemetry.resetForTest();
}

@Test
void otlpEnabled() {
runner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

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;
Expand All @@ -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() {
Expand Down
Loading
Loading