From 1d28098ff7b10538363b6acca631edbb263b7726 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Thu, 3 Mar 2022 16:17:20 -0800 Subject: [PATCH] Add JBoss java.util.logging support --- .../java-util-logging/javaagent/build.gradle.kts | 4 ++++ .../instrumentation/jul/JavaUtilLoggingHelper.java | 2 +- .../jul/JavaUtilLoggingInstrumentation.java | 7 +++++++ .../test/groovy/JBossJavaUtilLoggingTest.groovy | 14 ++++++++++++++ .../src/test/groovy/JavaUtilLoggingTest.groovy | 8 +++++++- .../java/application/java/util/logging/Logger.java | 4 ++++ 6 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 instrumentation/java-util-logging/javaagent/src/test/groovy/JBossJavaUtilLoggingTest.groovy diff --git a/instrumentation/java-util-logging/javaagent/build.gradle.kts b/instrumentation/java-util-logging/javaagent/build.gradle.kts index 165f30bd164d..3804b4bcddad 100644 --- a/instrumentation/java-util-logging/javaagent/build.gradle.kts +++ b/instrumentation/java-util-logging/javaagent/build.gradle.kts @@ -7,6 +7,10 @@ dependencies { compileOnly(project(":instrumentation-appender-api-internal")) + // the JBoss instrumentation in this artifact is needed + // for jboss-logmanager versions 1.1.0.GA through latest 2.x + testLibrary("org.jboss.logmanager:jboss-logmanager:1.1.0.GA") + testImplementation("org.awaitility:awaitility") } diff --git a/instrumentation/java-util-logging/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jul/JavaUtilLoggingHelper.java b/instrumentation/java-util-logging/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jul/JavaUtilLoggingHelper.java index 8ffcfa6a56b3..9ce6bbff8134 100644 --- a/instrumentation/java-util-logging/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jul/JavaUtilLoggingHelper.java +++ b/instrumentation/java-util-logging/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jul/JavaUtilLoggingHelper.java @@ -36,7 +36,7 @@ public static void capture(Logger logger, LogRecord logRecord) { return; } - String instrumentationName = logRecord.getLoggerName(); + String instrumentationName = logger.getName(); if (instrumentationName == null || instrumentationName.isEmpty()) { instrumentationName = "ROOT"; } diff --git a/instrumentation/java-util-logging/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jul/JavaUtilLoggingInstrumentation.java b/instrumentation/java-util-logging/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jul/JavaUtilLoggingInstrumentation.java index a8cc53cc41fa..1eff30eb294c 100644 --- a/instrumentation/java-util-logging/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jul/JavaUtilLoggingInstrumentation.java +++ b/instrumentation/java-util-logging/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jul/JavaUtilLoggingInstrumentation.java @@ -38,6 +38,13 @@ public void transform(TypeTransformer transformer) { .and(takesArguments(1)) .and(takesArgument(0, named("java.util.logging.LogRecord"))), JavaUtilLoggingInstrumentation.class.getName() + "$LogAdvice"); + transformer.applyAdviceToMethod( + isMethod() + .and(isPublic()) + .and(named("logRaw")) + .and(takesArguments(1)) + .and(takesArgument(0, named("org.jboss.logmanager.ExtLogRecord"))), + JavaUtilLoggingInstrumentation.class.getName() + "$LogAdvice"); } @SuppressWarnings("unused") diff --git a/instrumentation/java-util-logging/javaagent/src/test/groovy/JBossJavaUtilLoggingTest.groovy b/instrumentation/java-util-logging/javaagent/src/test/groovy/JBossJavaUtilLoggingTest.groovy new file mode 100644 index 000000000000..3feb2a016932 --- /dev/null +++ b/instrumentation/java-util-logging/javaagent/src/test/groovy/JBossJavaUtilLoggingTest.groovy @@ -0,0 +1,14 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +import org.jboss.logmanager.LogContext + +class JBossJavaUtilLoggingTest extends JavaUtilLoggingTest { + + @Override + Object createLogger(String name) { + LogContext.create().getLogger(name) + } +} diff --git a/instrumentation/java-util-logging/javaagent/src/test/groovy/JavaUtilLoggingTest.groovy b/instrumentation/java-util-logging/javaagent/src/test/groovy/JavaUtilLoggingTest.groovy index 8f4b610df074..a022712f9636 100644 --- a/instrumentation/java-util-logging/javaagent/src/test/groovy/JavaUtilLoggingTest.groovy +++ b/instrumentation/java-util-logging/javaagent/src/test/groovy/JavaUtilLoggingTest.groovy @@ -6,6 +6,7 @@ import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification import io.opentelemetry.sdk.logs.data.Severity import io.opentelemetry.semconv.trace.attributes.SemanticAttributes +import spock.lang.Shared import spock.lang.Unroll import java.util.logging.Level @@ -16,7 +17,12 @@ import static org.awaitility.Awaitility.await class JavaUtilLoggingTest extends AgentInstrumentationSpecification { - private static final Logger logger = Logger.getLogger("abc") + @Shared + private final Object logger = createLogger("abc") + + Object createLogger(String name) { + Logger.getLogger(name) + } @Unroll def "test method=#testMethod with exception=#exception and parent=#parent"() { diff --git a/instrumentation/java-util-logging/shaded-stub-for-instrumenting/src/main/java/application/java/util/logging/Logger.java b/instrumentation/java-util-logging/shaded-stub-for-instrumenting/src/main/java/application/java/util/logging/Logger.java index 0a57455653c8..32a68274f4fa 100644 --- a/instrumentation/java-util-logging/shaded-stub-for-instrumenting/src/main/java/application/java/util/logging/Logger.java +++ b/instrumentation/java-util-logging/shaded-stub-for-instrumenting/src/main/java/application/java/util/logging/Logger.java @@ -12,6 +12,10 @@ // after java.util.logging.Logger is shaded to the "PatchLogger" public class Logger { + public String getName() { + throw new UnsupportedOperationException(); + } + public boolean isLoggable(@SuppressWarnings("unused") Level level) { throw new UnsupportedOperationException(); }