-
Notifications
You must be signed in to change notification settings - Fork 923
Sketch out slf4j bridge #7905
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
1
commit 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.
+135
−0
Draft
Sketch out slf4j bridge #7905
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
26 changes: 26 additions & 0 deletions
26
api/all/src/main/java/io/opentelemetry/api/logs/Loopback.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.api.logs; | ||
|
|
||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.ContextKey; | ||
|
|
||
| public final class Loopback { | ||
|
|
||
| private Loopback() {} | ||
|
|
||
| public static final ContextKey<Boolean> loopbackContextKey = | ||
| ContextKey.named("otel.loopback"); | ||
|
|
||
| public static Context withLoopback(Context context) { | ||
| return context.with(loopbackContextKey, true); | ||
| } | ||
|
|
||
| public static boolean isLoopback(Context context) { | ||
| Boolean loopback = context.get(loopbackContextKey); | ||
| return loopback != null && loopback; | ||
| } | ||
| } |
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
106 changes: 106 additions & 0 deletions
106
...or/src/main/java/io/opentelemetry/sdk/extension/incubator/slf4j/Slf4jBridgeProcessor.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,106 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.sdk.extension.incubator.slf4j; | ||
|
|
||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.common.Value; | ||
| import io.opentelemetry.api.logs.Loopback; | ||
| import io.opentelemetry.api.logs.Severity; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.Scope; | ||
| import io.opentelemetry.sdk.logs.LogRecordProcessor; | ||
| import io.opentelemetry.sdk.logs.ReadWriteLogRecord; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.slf4j.event.Level; | ||
| import org.slf4j.spi.LoggingEventBuilder; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| public final class Slf4jBridgeProcessor implements LogRecordProcessor { | ||
| private Slf4jBridgeProcessor() {} | ||
|
|
||
| public static Slf4jBridgeProcessor create() { | ||
| return new Slf4jBridgeProcessor(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onEmit(Context context, ReadWriteLogRecord logRecord) { | ||
| if (Loopback.isLoopback(context)) { | ||
| return; | ||
| } | ||
|
|
||
| try (Scope scope = Loopback.withLoopback(context).makeCurrent()) { | ||
| recordToSlf4j( | ||
| logRecord.getInstrumentationScopeInfo().getName(), | ||
| logRecord.getEventName(), | ||
| logRecord.getBodyValue(), | ||
| logRecord.getAttributes(), | ||
| logRecord.getSeverity()); | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("CheckReturnValue") | ||
| private 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); | ||
| } | ||
| } | ||
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.
We also have to check in instrumentation for this I suppose?
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.
Yes exactly, for example: https://github.com/open-telemetry/opentelemetry-java-instrumentation/pull/15572/files#diff-19adc31cb9699cb43b2c7701d9838a060a4ae299710b1e0b382493cfd1cc382f
There's another version of this we can consider as well:
SdkLoggerProvidersuch every call toLogRecordProcessor#onEmit(Context, ReadWriteLogRecord)hasContextw/otel.loopback=true(implicit and explicit context)SdkLoggerProvidersuch that calls toSdkLogRecordBuilder#emit()short circuit ifotel.loopback=trueI went with the approach you see here because modifying the internals of the SDK is arguably spec territory and this approach allows solving without changing internals.
Also, even if
SdkLogRecordBuilder#emit()is short circuiting ifotel.loopback=true, you'd still want instrumentation to short circuit to avoid the unnecessary mapping work which involves memory allocation.But it might be worth to do both things. Or to start with this type of solution which doesn't rely on modying SDK internal, and later evolve if something lands in the spec.