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

feature(logback-classic): add ability to include stack traces when forwarding logs #849

Closed
wants to merge 1 commit into from
Closed
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 @@ -67,7 +67,7 @@ public LoggingEvent_Instrumentation(String fqcn, Logger logger, Level level, Str

if (applicationLoggingEnabled && isApplicationLoggingForwardingEnabled()) {
// Record and send LogEvent to New Relic
recordNewRelicLogEvent(getFormattedMessage(), timeStamp, level);
recordNewRelicLogEvent(getFormattedMessage(), timeStamp, level, throwable);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.newrelic.agent.bridge.AgentBridge;
import com.newrelic.api.agent.NewRelic;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
Expand All @@ -21,6 +23,7 @@ public class AgentUtil {
public static final int DEFAULT_NUM_OF_LOG_EVENT_ATTRIBUTES = 3;
// Log message attributes
public static final String MESSAGE = "message";
public static final String STACKTRACE = "stacktrace";
public static final String TIMESTAMP = "timestamp";
public static final String LOG_LEVEL = "log.level";
public static final String UNKNOWN = "UNKNOWN";
Expand All @@ -36,6 +39,7 @@ public class AgentUtil {
private static final boolean APP_LOGGING_DEFAULT_ENABLED = true;
private static final boolean APP_LOGGING_METRICS_DEFAULT_ENABLED = true;
private static final boolean APP_LOGGING_FORWARDING_DEFAULT_ENABLED = true;
private static final boolean APP_LOGGING_FORWARD_STACK_TRACES_ENABLED = false;
private static final boolean APP_LOGGING_LOCAL_DECORATING_DEFAULT_ENABLED = false;

/**
Expand All @@ -45,7 +49,7 @@ public class AgentUtil {
* @param timeStampMillis log timestamp
* @param level log level
*/
public static void recordNewRelicLogEvent(String message, long timeStampMillis, Level level) {
public static void recordNewRelicLogEvent(String message, long timeStampMillis, Level level, Throwable t) {
// Bail out and don't create a LogEvent if log message is empty
if (!message.isEmpty()) {
HashMap<String, Object> logEventMap = new HashMap<>(DEFAULT_NUM_OF_LOG_EVENT_ATTRIBUTES);
Expand All @@ -58,6 +62,13 @@ public static void recordNewRelicLogEvent(String message, long timeStampMillis,
logEventMap.put(LOG_LEVEL, level);
}

if (t != null && isApplicationForwardStackTracesEnabled()) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My co-worker and expected this to be the default behavior, we'd be in-favor of this not even being behind a flag.
I assume this was omitted to save data egress / ingress.

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
logEventMap.put(STACKTRACE, sw.toString());
}

AgentBridge.getAgent().getLogSender().recordLogEvent(logEventMap);
}
}
Expand Down Expand Up @@ -134,6 +145,15 @@ public static boolean isApplicationLoggingForwardingEnabled() {
return NewRelic.getAgent().getConfig().getValue("application_logging.forwarding.enabled", APP_LOGGING_FORWARDING_DEFAULT_ENABLED);
}

/**
* Check if the application_logging forward_stack_traces feature is enabled.
*
* @return true if enabled, else false
*/
public static boolean isApplicationForwardStackTracesEnabled() {
return NewRelic.getAgent().getConfig().getValue("application_logging.forward_stack_traces.enabled", APP_LOGGING_FORWARD_STACK_TRACES_ENABLED);
}

/**
* Check if the application_logging local_decorating feature is enabled.
*
Expand Down