-
Notifications
You must be signed in to change notification settings - Fork 869
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
Add support for capturing logback mdc attributes #4968
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
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
106 changes: 0 additions & 106 deletions
106
.../java/io/opentelemetry/javaagent/instrumentation/logback/appender/v1_0/LogbackHelper.java
This file was deleted.
Oops, something went wrong.
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
20 changes: 20 additions & 0 deletions
20
instrumentation/logback/logback-appender-1.0/library/build.gradle.kts
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,20 @@ | ||
plugins { | ||
id("otel.library-instrumentation") | ||
} | ||
|
||
dependencies { | ||
api(project(":instrumentation-api-appender")) | ||
|
||
library("ch.qos.logback:logback-classic:0.9.16") | ||
|
||
testImplementation(project(":instrumentation-sdk-appender")) | ||
testImplementation("io.opentelemetry:opentelemetry-sdk-logs") | ||
testImplementation("io.opentelemetry:opentelemetry-sdk-testing") | ||
|
||
testImplementation("org.mockito:mockito-core") | ||
} | ||
|
||
tasks.withType<Test>().configureEach { | ||
// TODO run tests both with and without experimental log attributes | ||
jvmArgs("-Dotel.instrumentation.logback-appender.experimental.capture-mdc-attributes=*") | ||
} |
163 changes: 163 additions & 0 deletions
163
...a/io/opentelemetry/instrumentation/logback/appender/v1_0/internal/LoggingEventMapper.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,163 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.logback.appender.v1_0.internal; | ||
|
||
import static java.util.Collections.emptyList; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.classic.spi.ThrowableProxy; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.instrumentation.api.appender.GlobalLogEmitterProvider; | ||
import io.opentelemetry.instrumentation.api.appender.LogBuilder; | ||
import io.opentelemetry.instrumentation.api.appender.Severity; | ||
import io.opentelemetry.instrumentation.api.cache.Cache; | ||
import io.opentelemetry.instrumentation.api.config.Config; | ||
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public final class LoggingEventMapper { | ||
|
||
public static final LoggingEventMapper INSTANCE = new LoggingEventMapper(); | ||
|
||
private static final Cache<String, AttributeKey<String>> mdcAttributeKeys = Cache.bounded(100); | ||
|
||
private final List<String> captureMdcAttributes; | ||
|
||
// cached as an optimization | ||
private final boolean captureAllMdcAttributes; | ||
|
||
private LoggingEventMapper() { | ||
this( | ||
Config.get() | ||
.getList( | ||
"otel.instrumentation.logback-appender.experimental.capture-mdc-attributes", | ||
emptyList())); | ||
} | ||
|
||
// visible for testing | ||
LoggingEventMapper(List<String> captureMdcAttributes) { | ||
this.captureMdcAttributes = captureMdcAttributes; | ||
this.captureAllMdcAttributes = | ||
captureMdcAttributes.size() == 1 && captureMdcAttributes.get(0).equals("*"); | ||
} | ||
|
||
public void capture(final ILoggingEvent event) { | ||
LogBuilder builder = | ||
GlobalLogEmitterProvider.get() | ||
.logEmitterBuilder(event.getLoggerName()) | ||
.build() | ||
.logBuilder(); | ||
mapLoggingEvent(builder, event); | ||
builder.emit(); | ||
} | ||
|
||
/** | ||
* Map the {@link ILoggingEvent} data model onto the {@link LogBuilder}. Unmapped fields include: | ||
* | ||
* <ul> | ||
* <li>Thread name - {@link ILoggingEvent#getThreadName()} | ||
* <li>Marker - {@link ILoggingEvent#getMarker()} | ||
* <li>Mapped diagnostic context - {@link ILoggingEvent#getMDCPropertyMap()} | ||
* </ul> | ||
*/ | ||
private void mapLoggingEvent(LogBuilder builder, ILoggingEvent loggingEvent) { | ||
// message | ||
String message = loggingEvent.getMessage(); | ||
if (message != null) { | ||
builder.setBody(message); | ||
} | ||
|
||
// time | ||
long timestamp = loggingEvent.getTimeStamp(); | ||
builder.setEpoch(timestamp, TimeUnit.MILLISECONDS); | ||
|
||
// level | ||
Level level = loggingEvent.getLevel(); | ||
if (level != null) { | ||
builder.setSeverity(levelToSeverity(level)); | ||
builder.setSeverityText(level.levelStr); | ||
} | ||
|
||
AttributesBuilder attributes = Attributes.builder(); | ||
|
||
// throwable | ||
Object throwableProxy = loggingEvent.getThrowableProxy(); | ||
Throwable throwable = null; | ||
if (throwableProxy instanceof ThrowableProxy) { | ||
// there is only one other subclass of ch.qos.logback.classic.spi.IThrowableProxy | ||
// and it is only used for logging exceptions over the wire | ||
throwable = ((ThrowableProxy) throwableProxy).getThrowable(); | ||
} | ||
if (throwable != null) { | ||
setThrowable(attributes, throwable); | ||
} | ||
|
||
captureMdcAttributes(attributes, loggingEvent.getMDCPropertyMap()); | ||
|
||
builder.setAttributes(attributes.build()); | ||
|
||
// span context | ||
builder.setContext(Context.current()); | ||
} | ||
|
||
// visible for testing | ||
void captureMdcAttributes(AttributesBuilder attributes, Map<String, String> mdcProperties) { | ||
|
||
if (captureAllMdcAttributes) { | ||
for (Map.Entry<String, String> entry : mdcProperties.entrySet()) { | ||
attributes.put(getMdcAttributeKey(entry.getKey()), entry.getValue()); | ||
} | ||
return; | ||
} | ||
|
||
for (String key : captureMdcAttributes) { | ||
String value = mdcProperties.get(key); | ||
if (value != null) { | ||
attributes.put(getMdcAttributeKey(key), value); | ||
} | ||
} | ||
} | ||
|
||
public static AttributeKey<String> getMdcAttributeKey(String key) { | ||
return mdcAttributeKeys.computeIfAbsent(key, k -> AttributeKey.stringKey("logback.mdc." + k)); | ||
} | ||
|
||
private static void setThrowable(AttributesBuilder attributes, Throwable throwable) { | ||
// TODO (trask) extract method for recording exception into instrumentation-api-appender | ||
attributes.put(SemanticAttributes.EXCEPTION_TYPE, throwable.getClass().getName()); | ||
attributes.put(SemanticAttributes.EXCEPTION_MESSAGE, throwable.getMessage()); | ||
StringWriter writer = new StringWriter(); | ||
throwable.printStackTrace(new PrintWriter(writer)); | ||
attributes.put(SemanticAttributes.EXCEPTION_STACKTRACE, writer.toString()); | ||
} | ||
|
||
private static Severity levelToSeverity(Level level) { | ||
switch (level.levelInt) { | ||
case Level.ALL_INT: | ||
case Level.TRACE_INT: | ||
return Severity.TRACE; | ||
case Level.DEBUG_INT: | ||
return Severity.DEBUG; | ||
case Level.INFO_INT: | ||
return Severity.INFO; | ||
case Level.WARN_INT: | ||
return Severity.WARN; | ||
case Level.ERROR_INT: | ||
return Severity.ERROR; | ||
case Level.OFF_INT: | ||
default: | ||
return Severity.UNDEFINED_SEVERITY_NUMBER; | ||
} | ||
} | ||
} |
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.
Do you think there'd be a benefit in having a shared cache in an
.internal.
class in instrumentation-api?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.
Created #5003 to discuss/track