-
Notifications
You must be signed in to change notification settings - Fork 873
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
jeanbisutti
wants to merge
2
commits into
open-telemetry:main
from
jeanbisutti:otel-injection-in-appenders
+77
−1
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
70 changes: 70 additions & 0 deletions
70
.../opentelemetry/instrumentation/spring/autoconfigure/log/OtelLogbackAutoConfiguration.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,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 { | ||
|
||
@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(); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
work as well? (and another one for log4j if anyone's using that)