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

NR-2447 Add log forwarding and tests for JUL. #1049

Merged
merged 2 commits into from
Oct 20, 2022
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
*
* * Copyright 2022 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.jul;

import com.newrelic.agent.bridge.AgentBridge;
import com.newrelic.agent.bridge.logging.LogAttributeKey;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import static com.newrelic.agent.bridge.logging.AppLoggingUtils.DEFAULT_NUM_OF_LOG_EVENT_ATTRIBUTES;
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.ERROR_CLASS;
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.ERROR_MESSAGE;
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.ERROR_STACK;
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.LEVEL;
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.LOGGER_FQCN;
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.LOGGER_NAME;
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.MESSAGE;
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.THREAD_ID;
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.THREAD_NAME;
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.TIMESTAMP;
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.UNKNOWN;

public class AgentUtil {

/**
* Record a LogEvent to be sent to New Relic.
*
* @param record to parse
*/
public static void recordNewRelicLogEvent(LogRecord record) {
if (record != null) {
String message = record.getMessage();
Throwable throwable = record.getThrown();

if (shouldCreateLogEvent(message, throwable)) {
// JUL does not directly support MDC, so we only initialize the map size based on standard attributes
Map<LogAttributeKey, Object> logEventMap = new HashMap<>(DEFAULT_NUM_OF_LOG_EVENT_ATTRIBUTES);
logEventMap.put(MESSAGE, message);
logEventMap.put(TIMESTAMP, record.getMillis());

Level level = record.getLevel();
if (level != null) {
String levelName = level.getName();
if (levelName.isEmpty()) {
logEventMap.put(LEVEL, UNKNOWN);
} else {
logEventMap.put(LEVEL, levelName);
}
}

String errorStack = ExceptionUtil.getErrorStack(throwable);
if (errorStack != null) {
logEventMap.put(ERROR_STACK, errorStack);
}

String errorMessage = ExceptionUtil.getErrorMessage(throwable);
if (errorMessage != null) {
logEventMap.put(ERROR_MESSAGE, errorMessage);
}

String errorClass = ExceptionUtil.getErrorClass(throwable);
if (errorClass != null) {
logEventMap.put(ERROR_CLASS, errorClass);
}

String threadName = Thread.currentThread().getName();
if (threadName != null) {
logEventMap.put(THREAD_NAME, threadName);
}

logEventMap.put(THREAD_ID, record.getThreadID());

String loggerName = record.getLoggerName();
if (loggerName != null) {
logEventMap.put(LOGGER_NAME, loggerName);
}

String loggerFqcn = record.getSourceClassName();
if (loggerFqcn != null) {
logEventMap.put(LOGGER_FQCN, loggerFqcn);
}

AgentBridge.getAgent().getLogSender().recordLogEvent(logEventMap);
}
}
}

/**
* A LogEvent MUST NOT be reported if neither a log message nor an error is logged. If either is present report the LogEvent.
*
* @param message Message to validate
* @param throwable Throwable to validate
* @return true if a LogEvent should be created, otherwise false
*/
private static boolean shouldCreateLogEvent(String message, Throwable throwable) {
return (message != null) || !ExceptionUtil.isThrowableNull(throwable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
*
* * Copyright 2022 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.jul;

public class ExceptionUtil {
public static final int MAX_STACK_SIZE = 300;

public static boolean isThrowableNull(Throwable throwable) {
return throwable == null;
}

public static String getErrorStack(Throwable throwable) {
if (isThrowableNull(throwable)) {
return null;
}

StackTraceElement[] stack = throwable.getStackTrace();
return getErrorStack(stack);
}

public static String getErrorStack(StackTraceElement[] stack) {
return getErrorStack(stack, MAX_STACK_SIZE);
}

public static String getErrorStack(StackTraceElement[] stack, Integer maxStackSize) {
if (stack == null || stack.length == 0) {
return null;
}

StringBuilder stackBuilder = new StringBuilder();
int stackSizeLimit = Math.min(maxStackSize, stack.length);
for (int i = 0; i < stackSizeLimit; i++) {
stackBuilder.append(" at ").append(stack[i].toString()).append("\n");
}
return stackBuilder.toString();
}

public static String getErrorMessage(Throwable throwable) {
if (isThrowableNull(throwable)) {
return null;
}
return throwable.getMessage();
}

public static String getErrorClass(Throwable throwable) {
if (isThrowableNull(throwable)) {
return null;
}
return throwable.getClass().getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
*
* * Copyright 2022 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package java.util.logging;

import com.newrelic.api.agent.weaver.Weave;

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;

@Weave(originalName = "java.util.logging.LogRecord")
public class LogRecord_Instrumentation {
private String message;

public String getMessage() {
if (isApplicationLoggingEnabled() && isApplicationLoggingLocalDecoratingEnabled()) {
// Append New Relic linking metadata from agent to log message
return message + getLinkingMetadataBlob();
} else {
return message;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,56 @@
package java.util.logging;

import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.weaver.NewField;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.WeaveAllConstructors;
import com.newrelic.api.agent.weaver.Weaver;

import java.util.concurrent.atomic.AtomicBoolean;

import static com.newrelic.agent.bridge.logging.AppLoggingUtils.isApplicationLoggingEnabled;
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.isApplicationLoggingForwardingEnabled;
import static com.newrelic.agent.bridge.logging.AppLoggingUtils.isApplicationLoggingMetricsEnabled;
import static com.nr.instrumentation.jul.AgentUtil.recordNewRelicLogEvent;

@Weave(originalName = "java.util.logging.Logger")
public class Logger_Instrumentation {
@NewField
public static AtomicBoolean instrumented = new AtomicBoolean(false);

@WeaveAllConstructors
Logger_Instrumentation() {
// Generate the instrumentation module supportability metric only once
if (!instrumented.getAndSet(true)) {
NewRelic.incrementCounter("Supportability/Logging/Java/JavaUtilLogging/enabled");
}
}

// Get the current filter for this Logger.
public Filter getFilter() {
return Weaver.callOriginal();
}

// Check if a message of the given level would actually be logged by this logger.
// This check is based on the Loggers effective level, which may be inherited from its parent.
public boolean isLoggable(Level level) {
return Boolean.TRUE.equals(Weaver.callOriginal());
}

public void log(LogRecord record) {
// Do nothing if application_logging.enabled: false
if (isApplicationLoggingEnabled()) {
if (isApplicationLoggingMetricsEnabled()) {
if (isLoggable(record.getLevel()) && getFilter() == null || getFilter().isLoggable(record)) {
// Generate log level metrics
NewRelic.incrementCounter("Logging/lines");
NewRelic.incrementCounter("Logging/lines/" + record.getLevel().toString());
}
boolean shouldLog = isLoggable(record.getLevel()) && getFilter() == null || getFilter().isLoggable(record);
if (isApplicationLoggingMetricsEnabled() && shouldLog) {
// Generate log level metrics
NewRelic.incrementCounter("Logging/lines");
NewRelic.incrementCounter("Logging/lines/" + record.getLevel().toString());
}
if (isApplicationLoggingForwardingEnabled() && shouldLog) {
// Record and send LogEvent to New Relic
recordNewRelicLogEvent(record);
}
}
Weaver.callOriginal();
}

}
3 changes: 3 additions & 0 deletions instrumentation/java.logging-jdk8/src/test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The instrumentation test framework cannot be used to test instrumentation modules that weave core JDK classes, in such cases we use functional tests instead.

See tests here: `newrelic-java-agent/functional_test/src/test/java/test/newrelic/test/agent/JavaUtilLoggerTest.java`
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
*
* * Copyright 2022 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.nr.instrumentation.jul;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class ExceptionUtilTest {

@Test
public void testIsThrowableNull() {
Throwable nullThrowable = null;
Throwable nonNullThrowable = new Throwable("Hi");

assertTrue(ExceptionUtil.isThrowableNull(nullThrowable));
assertFalse(ExceptionUtil.isThrowableNull(nonNullThrowable));
}

@Test
public void testGetErrorStack() {
int maxStackSize = 3;
StackTraceElement stackTraceElement1 = new StackTraceElement("Class1", "method1", "File1", 1);
StackTraceElement stackTraceElement2 = new StackTraceElement("Class2", "method2", "File2", 2);
StackTraceElement stackTraceElement3 = new StackTraceElement("Class3", "method3", "File3", 3);
StackTraceElement stackTraceElement4 = new StackTraceElement("Class4", "method4", "File4", 4);
StackTraceElement stackTraceElement5 = new StackTraceElement("Class5", "method5", "File5", 5);
StackTraceElement[] stack = new StackTraceElement[] { stackTraceElement1, stackTraceElement2, stackTraceElement3, stackTraceElement4,
stackTraceElement5 };
String errorStack = ExceptionUtil.getErrorStack(stack, maxStackSize);

// Processed stack should be limited to only the first three lines
assertTrue(errorStack.contains(stackTraceElement1.toString()));
assertTrue(errorStack.contains(stackTraceElement2.toString()));
assertTrue(errorStack.contains(stackTraceElement3.toString()));
// Processed stack should omit the last two lines
assertFalse(errorStack.contains(stackTraceElement4.toString()));
assertFalse(errorStack.contains(stackTraceElement5.toString()));
}

@Test
public void testGetErrorMessage() {
String expectedMessage = "Hi";
Throwable nullThrowable = null;
Throwable nonNullThrowable = new Throwable(expectedMessage);

assertNull(ExceptionUtil.getErrorMessage(nullThrowable));
assertEquals(expectedMessage, ExceptionUtil.getErrorMessage(nonNullThrowable));
}

@Test
public void testGetErrorClass() {
String expectedExceptionClass = "java.lang.RuntimeException";
Throwable nullThrowable = null;
RuntimeException runtimeException = new RuntimeException("Hi");

assertNull(ExceptionUtil.getErrorClass(nullThrowable));
assertEquals(expectedExceptionClass, ExceptionUtil.getErrorClass(runtimeException));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
common: &default_settings
application_logging:
enabled: true
forwarding:
enabled: true
max_samples_stored: 10000
metrics:
enabled: true
local_decorating:
enabled: false