-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add local decorating support for adding linking metadata when using Log4j2 JsonLayout
- Loading branch information
Showing
6 changed files
with
205 additions
and
13 deletions.
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
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
80 changes: 80 additions & 0 deletions
80
.../apache-log4j-2/src/main/java/org/apache/logging/log4j/core/LogEvent_Instrumentation.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,80 @@ | ||
/* | ||
* | ||
* * Copyright 2023 New Relic Corporation. All rights reserved. | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.apache.logging.log4j.core; | ||
|
||
import com.newrelic.api.agent.NewRelic; | ||
import com.newrelic.api.agent.weaver.MatchType; | ||
import com.newrelic.api.agent.weaver.NewField; | ||
import com.newrelic.api.agent.weaver.Weave; | ||
import org.apache.logging.log4j.Level; | ||
import org.apache.logging.log4j.Marker; | ||
import org.apache.logging.log4j.ThreadContext; | ||
import org.apache.logging.log4j.core.impl.ThrowableProxy; | ||
import org.apache.logging.log4j.core.time.Instant; | ||
import org.apache.logging.log4j.message.Message; | ||
import org.apache.logging.log4j.util.ReadOnlyStringMap; | ||
|
||
import java.util.Map; | ||
|
||
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.isApplicationLoggingLocalDecoratingEnabled; | ||
|
||
@Weave(originalName = "org.apache.logging.log4j.core.LogEvent", type = MatchType.Interface) | ||
public abstract class LogEvent_Instrumentation { | ||
|
||
/* | ||
* In cases where the LogEvent is sent to an AsyncAppender, getLinkingMetadata would get called on a new thread and the trace.id and span.id | ||
* would be missing. To work around this we save the linking metadata on the LogEvent on the thread where it was created and use it later. | ||
*/ | ||
@NewField | ||
public Map<String, String> agentLinkingMetadata = isApplicationLoggingLocalDecoratingEnabled() ? NewRelic.getAgent().getLinkingMetadata() : null; | ||
|
||
public abstract LogEvent toImmutable(); | ||
|
||
@Deprecated | ||
public abstract Map<String, String> getContextMap(); | ||
|
||
public abstract ReadOnlyStringMap getContextData(); | ||
|
||
public abstract ThreadContext.ContextStack getContextStack(); | ||
|
||
public abstract String getLoggerFqcn(); | ||
|
||
public abstract Level getLevel(); | ||
|
||
public abstract String getLoggerName(); | ||
|
||
public abstract Marker getMarker(); | ||
|
||
public abstract Message getMessage(); | ||
|
||
public abstract long getTimeMillis(); | ||
|
||
public abstract Instant getInstant(); | ||
|
||
public abstract StackTraceElement getSource(); | ||
|
||
public abstract String getThreadName(); | ||
|
||
public abstract long getThreadId(); | ||
|
||
public abstract int getThreadPriority(); | ||
|
||
public abstract Throwable getThrown(); | ||
|
||
public abstract ThrowableProxy getThrownProxy(); | ||
|
||
public abstract boolean isEndOfBatch(); | ||
|
||
public abstract boolean isIncludeLocation(); | ||
|
||
public abstract void setEndOfBatch(boolean endOfBatch); | ||
|
||
public abstract void setIncludeLocation(boolean locationRequired); | ||
|
||
public abstract long getNanoTime(); | ||
} |
66 changes: 66 additions & 0 deletions
66
...main/java/org/apache/logging/log4j/core/layout/AbstractJacksonLayout_Instrumentation.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,66 @@ | ||
/* | ||
* | ||
* * Copyright 2023 New Relic Corporation. All rights reserved. | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.apache.logging.log4j.core.layout; | ||
|
||
import com.newrelic.api.agent.weaver.MatchType; | ||
import com.newrelic.api.agent.weaver.Weave; | ||
import com.newrelic.api.agent.weaver.Weaver; | ||
import org.apache.logging.log4j.core.LogEvent_Instrumentation; | ||
import org.apache.logging.log4j.core.util.StringBuilderWriter; | ||
|
||
import java.io.IOException; | ||
import java.io.Writer; | ||
|
||
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.BLOB_PREFIX; | ||
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.getLinkingMetadataBlob; | ||
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.getLinkingMetadataBlobFromMap; | ||
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.isApplicationLoggingEnabled; | ||
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.isApplicationLoggingLocalDecoratingEnabled; | ||
import static com.nr.agent.instrumentation.log4j2.AgentUtil.foundIndexToInsertLinkingMetadata; | ||
import static com.nr.agent.instrumentation.log4j2.AgentUtil.getIndexToModifyJson; | ||
|
||
@Weave(originalName = "org.apache.logging.log4j.core.layout.AbstractJacksonLayout", type = MatchType.ExactClass) | ||
abstract class AbstractJacksonLayout_Instrumentation { | ||
|
||
public abstract void toSerializable(final LogEvent_Instrumentation event, final Writer writer) throws IOException; | ||
|
||
public String toSerializable(final LogEvent_Instrumentation event) { | ||
final StringBuilderWriter writer = new StringBuilderWriter(); | ||
try { | ||
toSerializable(event, writer); | ||
String writerString = writer.toString(); | ||
String modified = writerString; | ||
|
||
// Append linking metadata to the log message if local decorating is enabled | ||
if (isApplicationLoggingEnabled() && isApplicationLoggingLocalDecoratingEnabled()) { | ||
// It is possible that the log might already have NR-LINKING metadata from JUL instrumentation | ||
if (!writerString.contains(BLOB_PREFIX)) { | ||
int indexToModifyJson = getIndexToModifyJson(writerString); | ||
if (foundIndexToInsertLinkingMetadata(indexToModifyJson)) { | ||
// Replace the JSON string with modified version that includes NR-LINKING metadata | ||
if (event != null && event.agentLinkingMetadata != null && (!event.agentLinkingMetadata.isEmpty())) { | ||
// Get linking metadata stored on LogEvent if available. This ensures that | ||
// the trace.id and span.id will be available when using an async appender. | ||
modified = new StringBuilder(writerString).insert(indexToModifyJson, getLinkingMetadataBlobFromMap(event.agentLinkingMetadata)) | ||
.toString(); | ||
} else { | ||
// Get linking metadata from current thread if it is not available on LogEvent. | ||
modified = new StringBuilder(writerString).insert(indexToModifyJson, getLinkingMetadataBlob()).toString(); | ||
} | ||
} | ||
} | ||
return modified; | ||
} | ||
|
||
return writerString; | ||
} catch (final IOException e) { | ||
return Weaver.callOriginal(); | ||
} | ||
} | ||
|
||
} |
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