Skip to content
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

Revert "Add local decorating support for adding linking metadata when using Log4j2 JsonLayout" #1545

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package com.newrelic.agent.bridge.logging;

import com.newrelic.agent.bridge.logging.LogAttributeKey;
import com.newrelic.agent.bridge.logging.LogAttributeType;
import com.newrelic.api.agent.NewRelic;

import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -52,32 +54,11 @@ public class AppLoggingUtils {
* @return agent linking metadata string blob
*/
public static String getLinkingMetadataBlob() {
return constructLinkingMetadataBlob(NewRelic.getAgent().getLinkingMetadata());
}

/**
* Gets a String representing the agent linking metadata in blob format:
* NR-LINKING|entity.guid|hostname|trace.id|span.id|entity.name|
*
* @param agentLinkingMetadata map of linking metadata
* @return agent linking metadata string blob
*/
public static String getLinkingMetadataBlobFromMap(Map<String, String> agentLinkingMetadata) {
return constructLinkingMetadataBlob(agentLinkingMetadata);
}

/**
* Constructs a String representing the agent linking metadata in blob format:
* NR-LINKING|entity.guid|hostname|trace.id|span.id|entity.name|
*
* @param agentLinkingMetadata map of linking metadata
* @return agent linking metadata string blob
*/
private static String constructLinkingMetadataBlob(Map<String, String> agentLinkingMetadata) {
Map<String, String> agentLinkingMetadata = NewRelic.getAgent().getLinkingMetadata();
StringBuilder blob = new StringBuilder();
blob.append(" ").append(BLOB_PREFIX).append(BLOB_DELIMITER);

if (agentLinkingMetadata != null && !agentLinkingMetadata.isEmpty()) {
if (agentLinkingMetadata != null && agentLinkingMetadata.size() > 0) {
appendAttributeToBlob(agentLinkingMetadata.get(ENTITY_GUID), blob);
appendAttributeToBlob(agentLinkingMetadata.get(HOSTNAME), blob);
appendAttributeToBlob(agentLinkingMetadata.get(TRACE_ID), blob);
Expand Down
5 changes: 3 additions & 2 deletions instrumentation/apache-log4j-2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ jar {

dependencies {
implementation(project(":agent-bridge"))
implementation("org.apache.logging.log4j:log4j-core:2.20.0")
implementation("org.apache.logging.log4j:log4j-core:2.17.1")
}

verifyInstrumentation {
passesOnly("org.apache.logging.log4j:log4j-core:[2.6,3.0.0-alpha1)")
passesOnly("org.apache.logging.log4j:log4j-core:[2.6,)")
excludeRegex '.*(alpha|beta|rc).*'
}

site {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.util.ReadOnlyStringMap;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -127,28 +128,4 @@ private static int calculateInitialMapSize(Map<String, String> mdcPropertyMap) {
? mdcPropertyMap.size() + DEFAULT_NUM_OF_LOG_EVENT_ATTRIBUTES
: DEFAULT_NUM_OF_LOG_EVENT_ATTRIBUTES;
}

/**
* Checks pretty or compact JSON layout strings for a series of characters and returns the index of
* the characters or -1 if they were not found. This is used to find the log "message" substring
* so that the NR-LINKING metadata blob can be inserted when using local decorating with JsonLayout.
*
* @param writerString String representing JSON formatted log event
* @return positive int if index was found, else -1
*/
public static int getIndexToModifyJson(String writerString) {
return writerString.indexOf("\",", writerString.indexOf("message"));
}

/**
* Check if a valid match was found when calling String.indexOf.
* If index value is -1 then no valid match was found, a positive integer represents a valid index.
*
* @param indexToModifyJson int representing index returned by indexOf
* @return true if a valid index was found, else false
*/
public static boolean foundIndexToInsertLinkingMetadata(int indexToModifyJson) {
return indexToModifyJson != -1;
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;

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.isApplicationLoggingEnabled;
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.isApplicationLoggingLocalDecoratingEnabled;
Expand All @@ -31,15 +30,11 @@ public void encode(final StringBuilder source, final ByteBufferDestination desti
}

private void appendAgentMetadata(StringBuilder source) {
String sourceString = source.toString();
// It is possible that the log might already have NR-LINKING metadata from JUL instrumentation
if (!sourceString.contains(BLOB_PREFIX)) {
int breakLine = sourceString.lastIndexOf("\n");
if (breakLine != -1) {
source.replace(breakLine, breakLine + 1, "");
}
source.append(getLinkingMetadataBlob()).append("\n");
int breakLine = source.toString().lastIndexOf("\n");
if (breakLine != -1) {
source.replace(breakLine, breakLine + 1, "");
}
source.append(getLinkingMetadataBlob()).append("\n");
}

}
}
Loading