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

OTel Spring Boot starter: OpenTelemetry injection into the OTel Logack appenders #8887

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ dependencies {
compileOnly("io.opentelemetry:opentelemetry-exporter-zipkin")
compileOnly(project(":instrumentation-annotations"))

compileOnly("ch.qos.logback:logback-classic:1.3.0")
compileOnly("io.opentelemetry.instrumentation:opentelemetry-logback-appender-1.0:1.28.0-alpha-SNAPSHOT")

compileOnly(project(":instrumentation: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
@@ -0,0 +1,70 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.spring.autoconfigure.log;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender;
import java.util.Iterator;
import java.util.Optional;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;

@ConditionalOnClass({OpenTelemetryAppender.class, LoggerContext.class})
@AutoConfiguration
public class OtelLogbackAutoConfiguration {

static class OtelInjectorForLogback implements BeanPostProcessor, Ordered {
Copy link
Member

Choose a reason for hiding this comment

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

Funny thing, I actually noticed that this was missing and started working on that today too 😅

Wouldn't this

  @Bean
  @ConditionalOnProperty(
      prefix = "otel.springboot.logback-appender",
      name = "enabled",
      matchIfMissing = true)
  @ConditionalOnClass({
    io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender.class
  })
  ApplicationListener<ApplicationPreparedEvent> logbackOtelAppenderInitializer(
      OpenTelemetry openTelemetry) {
    return event -> {
      io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender.install(
          openTelemetry);
    };
  }

work as well? (and another one for log4j if anyone's using that)


@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof OpenTelemetry) {
OpenTelemetry openTelemetry = (OpenTelemetry) bean;
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Optional<OpenTelemetryAppender> potentialOtelAppender =
findOtelAppenderWithLogback(loggerContext);
if (potentialOtelAppender.isPresent()) {
OpenTelemetryAppender openTelemetryAppender = potentialOtelAppender.get();
openTelemetryAppender.setOpenTelemetry(openTelemetry);
}
}
return bean;
}

private static Optional<OpenTelemetryAppender> findOtelAppenderWithLogback(
LoggerContext loggerContext) {
for (Logger logger : loggerContext.getLoggerList()) {
Iterator<Appender<ILoggingEvent>> appenderIterator = logger.iteratorForAppenders();
while (appenderIterator.hasNext()) {
Appender<ILoggingEvent> appender = appenderIterator.next();
if (appender instanceof OpenTelemetryAppender) {
return Optional.of((OpenTelemetryAppender) appender);
}
}
}
return Optional.empty();
}

@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE - 1;
}
}

@Bean
public OtelInjectorForLogback otelPostProcessor() {
return new OtelInjectorForLogback();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ io.opentelemetry.instrumentation.spring.autoconfigure.kafka.KafkaInstrumentation
io.opentelemetry.instrumentation.spring.autoconfigure.metrics.MicrometerShimAutoConfiguration,\
io.opentelemetry.instrumentation.spring.autoconfigure.propagators.PropagationAutoConfiguration,\
io.opentelemetry.instrumentation.spring.autoconfigure.resources.OtelResourceAutoConfiguration,\
io.opentelemetry.instrumentation.spring.autoconfigure.webmvc.WebMvcFilterAutoConfiguration
io.opentelemetry.instrumentation.spring.autoconfigure.webmvc.WebMvcFilterAutoConfiguration,\
io.opentelemetry.instrumentation.spring.autoconfigure.log.OtelLogbackAutoConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ io.opentelemetry.instrumentation.spring.autoconfigure.metrics.MicrometerShimAuto
io.opentelemetry.instrumentation.spring.autoconfigure.propagators.PropagationAutoConfiguration
io.opentelemetry.instrumentation.spring.autoconfigure.resources.OtelResourceAutoConfiguration
io.opentelemetry.instrumentation.spring.autoconfigure.webmvc.WebMvcFilterAutoConfigurationSpring6
io.opentelemetry.instrumentation.spring.autoconfigure.log.OtelLogbackAutoConfiguration