-
Notifications
You must be signed in to change notification settings - Fork 1k
Proposal for making OpenTelemetry log API calls accessible in other log SDKs #15572
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
Draft
jack-berg
wants to merge
4
commits into
open-telemetry:main
Choose a base branch
from
jack-berg:slf4j-bridge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
7 changes: 7 additions & 0 deletions
7
instrumentation/internal/internal-slf4j-bridge/bootstrap/build.gradle.kts
This file contains hidden or 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,7 @@ | ||
| plugins { | ||
| id("otel.javaagent-bootstrap") | ||
| } | ||
|
|
||
| dependencies { | ||
| compileOnly(project(":javaagent-bootstrap")) | ||
| } |
17 changes: 17 additions & 0 deletions
17
...trap/src/main/java/io/opentelemetry/javaagent/bootstrap/logging/Slf4jBridgeInstaller.java
This file contains hidden or 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,17 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.bootstrap.logging; | ||
|
|
||
| import io.opentelemetry.javaagent.Slf4jLogRecorder; | ||
| import io.opentelemetry.javaagent.bootstrap.Slf4jBridgeLogRecorderHolder; | ||
|
|
||
| public class Slf4jBridgeInstaller { | ||
| private Slf4jBridgeInstaller() {} | ||
|
|
||
| public static void installSlf4jLogger(Slf4jLogRecorder slf4JLogRecorder) { | ||
| Slf4jBridgeLogRecorderHolder.initialize(slf4JLogRecorder); | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
...src/main/java/io/opentelemetry/javaagent/bootstrap/logging/Slf4jBridgeInstallerFlags.java
This file contains hidden or 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,15 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.bootstrap.logging; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| public final class Slf4jBridgeInstallerFlags { | ||
|
|
||
| private Slf4jBridgeInstallerFlags() {} | ||
|
|
||
| public static final AtomicBoolean IS_INSTALLED = new AtomicBoolean(false); | ||
| } |
26 changes: 26 additions & 0 deletions
26
instrumentation/internal/internal-slf4j-bridge/javaagent/build.gradle.kts
This file contains hidden or 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,26 @@ | ||
| plugins { | ||
| id("otel.javaagent-instrumentation") | ||
| } | ||
|
|
||
| muzzle { | ||
| pass { | ||
| group.set("org.slf4j") | ||
| module.set("slf4j-api") | ||
| versions.set("[2.0.0,)") | ||
| assertInverse.set(true) | ||
| } | ||
| } | ||
|
|
||
| val latestDepTest = findProperty("testLatestDeps") as Boolean | ||
| dependencies { | ||
| bootstrap(project(":instrumentation:internal:internal-slf4j-bridge:bootstrap")) | ||
|
|
||
| compileOnly(project(":javaagent-bootstrap")) | ||
|
|
||
| compileOnly("org.slf4j:slf4j-api") { | ||
| version { | ||
| // 2.0.0 introduced fluent API | ||
| strictly("2.0.0") | ||
| } | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
...elemetry/javaagent/instrumentation/internal/slf4jbridge/LoggerFactoryInstrumentation.java
This file contains hidden or 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,43 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.internal.slf4jbridge; | ||
|
|
||
| import static net.bytebuddy.matcher.ElementMatchers.named; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
|
||
| import io.opentelemetry.javaagent.bootstrap.logging.Slf4jBridgeInstallerFlags; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
| import net.bytebuddy.asm.Advice; | ||
| import net.bytebuddy.description.type.TypeDescription; | ||
| import net.bytebuddy.matcher.ElementMatcher; | ||
|
|
||
| public class LoggerFactoryInstrumentation implements TypeInstrumentation { | ||
|
|
||
| @Override | ||
| public ElementMatcher<TypeDescription> typeMatcher() { | ||
| return named("org.slf4j.LoggerFactory"); | ||
| } | ||
|
|
||
| @Override | ||
| public void transform(TypeTransformer transformer) { | ||
| // once a call to getILoggerFactory() exits we can be certain that slf4j is properly initialized | ||
| transformer.applyAdviceToMethod( | ||
| named("getILoggerFactory").and(takesArguments(0)), | ||
| this.getClass().getName() + "$GetLoggerFactoryAdvice"); | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public static class GetLoggerFactoryAdvice { | ||
|
|
||
| @Advice.OnMethodExit(suppress = Throwable.class) | ||
| public static void onExit() { | ||
| if (Slf4jBridgeInstallerFlags.IS_INSTALLED.compareAndSet(false, true)) { | ||
| Slf4jLogRecorderImpl.install(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
...try/javaagent/instrumentation/internal/slf4jbridge/Slf4jBridgeIgnoredTypesConfigurer.java
This file contains hidden or 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,20 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.internal.slf4jbridge; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import io.opentelemetry.javaagent.extension.ignore.IgnoredTypesBuilder; | ||
| import io.opentelemetry.javaagent.extension.ignore.IgnoredTypesConfigurer; | ||
| import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
|
|
||
| @AutoService(IgnoredTypesConfigurer.class) | ||
| public final class Slf4jBridgeIgnoredTypesConfigurer implements IgnoredTypesConfigurer { | ||
|
|
||
| @Override | ||
| public void configure(IgnoredTypesBuilder builder, ConfigProperties config) { | ||
| builder.allowClass("org.slf4j.LoggerFactory"); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
...etry/javaagent/instrumentation/internal/slf4jbridge/Slf4jBridgeInstrumentationModule.java
This file contains hidden or 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,26 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.internal.slf4jbridge; | ||
|
|
||
| import static java.util.Collections.singletonList; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
| import java.util.List; | ||
|
|
||
| @AutoService(InstrumentationModule.class) | ||
| public class Slf4jBridgeInstrumentationModule extends InstrumentationModule { | ||
|
|
||
| public Slf4jBridgeInstrumentationModule() { | ||
| super("internal-slf4j-bridge"); | ||
| } | ||
|
|
||
| @Override | ||
| public List<TypeInstrumentation> typeInstrumentations() { | ||
| return singletonList(new LoggerFactoryInstrumentation()); | ||
| } | ||
| } |
110 changes: 110 additions & 0 deletions
110
...io/opentelemetry/javaagent/instrumentation/internal/slf4jbridge/Slf4jLogRecorderImpl.java
This file contains hidden or 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,110 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.internal.slf4jbridge; | ||
|
|
||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.common.Value; | ||
| import io.opentelemetry.api.logs.LoggerProvider; | ||
| import io.opentelemetry.api.logs.Severity; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.javaagent.Slf4jLogRecorder; | ||
| import io.opentelemetry.javaagent.bootstrap.CallDepth; | ||
| import io.opentelemetry.javaagent.bootstrap.logging.Slf4jBridgeInstaller; | ||
| import javax.annotation.Nullable; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.slf4j.event.Level; | ||
| import org.slf4j.spi.LoggingEventBuilder; | ||
|
|
||
| public final class Slf4jLogRecorderImpl implements Slf4jLogRecorder { | ||
|
|
||
| private Slf4jLogRecorderImpl() {} | ||
|
|
||
| public static void install() { | ||
| Slf4jBridgeInstaller.installSlf4jLogger(new Slf4jLogRecorderImpl()); | ||
| } | ||
|
|
||
| @Override | ||
| public void record( | ||
| Context context, | ||
| String scopeName, | ||
| @Nullable String eventName, | ||
| @Nullable Value<?> bodyValue, | ||
| Attributes attributes, | ||
| Severity severity) { | ||
| CallDepth callDepth = CallDepth.forClass(LoggerProvider.class); | ||
| try { | ||
| if (callDepth.getAndIncrement() > 0) { | ||
| return; | ||
| } | ||
| recordToSlf4j(scopeName, eventName, bodyValue, attributes, severity); | ||
| } finally { | ||
| callDepth.decrementAndGet(); | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("CheckReturnValue") | ||
| public static void recordToSlf4j( | ||
| String scopeName, | ||
| @Nullable String eventName, | ||
| @Nullable Value<?> bodyValue, | ||
| Attributes attributes, | ||
| Severity severity) { | ||
| Logger logger = LoggerFactory.getLogger(scopeName); | ||
| Level level = toSlf4jLevel(severity); | ||
| if (!logger.isEnabledForLevel(level)) { | ||
| return; | ||
| } | ||
| LoggingEventBuilder builder = logger.atLevel(level); | ||
| if (bodyValue != null) { | ||
| builder.setMessage(bodyValue.asString()); | ||
| } | ||
| attributes.forEach((key, value) -> builder.addKeyValue(key.getKey(), value)); | ||
|
|
||
| // append event_name last to take priority over attributes | ||
| if (eventName != null) { | ||
| builder.addKeyValue("event_name", eventName); | ||
| } | ||
| builder.log(); | ||
| } | ||
|
|
||
| private static Level toSlf4jLevel(Severity severity) { | ||
| switch (severity) { | ||
| case TRACE: | ||
| case TRACE2: | ||
| case TRACE3: | ||
| case TRACE4: | ||
| return Level.TRACE; | ||
| case DEBUG: | ||
| case DEBUG2: | ||
| case DEBUG3: | ||
| case DEBUG4: | ||
| return Level.DEBUG; | ||
| case INFO: | ||
| case INFO2: | ||
| case INFO3: | ||
| case INFO4: | ||
| return Level.INFO; | ||
| case WARN: | ||
| case WARN2: | ||
| case WARN3: | ||
| case WARN4: | ||
| return Level.WARN; | ||
| case ERROR: | ||
| case ERROR2: | ||
| case ERROR3: | ||
| case ERROR4: | ||
| case FATAL: | ||
| case FATAL2: | ||
| case FATAL3: | ||
| case FATAL4: | ||
| return Level.ERROR; | ||
| case UNDEFINED_SEVERITY_NUMBER: | ||
| return Level.INFO; | ||
| } | ||
| throw new IllegalArgumentException("Unknown severity: " + severity); | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| classification: internal |
This file contains hidden or 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 hidden or 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
24 changes: 24 additions & 0 deletions
24
javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/Slf4jLogRecorder.java
This file contains hidden or 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,24 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent; | ||
|
|
||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.common.Value; | ||
| import io.opentelemetry.api.logs.Severity; | ||
| import io.opentelemetry.context.Context; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| @FunctionalInterface | ||
| public interface Slf4jLogRecorder { | ||
|
|
||
| void record( | ||
| Context context, | ||
| String scopeName, | ||
| @Nullable String eventName, | ||
| @Nullable Value<?> body, | ||
| Attributes attributes, | ||
| Severity severity); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
What's happening here:
slf4j-apifrom the application classpathOpenTelemetryInstallerfromjavaagent-toolingneeds to register aLogRecordProcessorwhich calls routs to the application classpathslf4j-apiSlf4jLogRecorderinjavaagent-bootstrapwhich can be accessed from both the instrumentation and theOpenTelemetryInstallerLogRecordProcessor#onEmitis called, it callsSlf4jLogRecorderHolder.get()to get a reference to aSlf4jLogRecorderand record the log.Slf4jLogRecorderinstance starts as a noop, but this instrumentation here detects the first usage ofslf4j-apiin the application, and installs callsSlf4jLogRecordHolder.initialize(Slf4jLogRecordHolder)with a proper instance connected to theslf4j-apiin the application classpath.Voila!
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.
If / when we agree on the basic concepts here, I can polish this up and it ready to merge:
opentelemetry-javafor the non agent case.LogRecordProcessoris always added. Should be configurable and need to think about the defaults.