From e67c9c4796919e1d4360c6b3824b15c4944a180d Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 4 Mar 2022 21:01:38 -0800 Subject: [PATCH] Fix jboss-logmanager testLatestDeps (#5503) --- .../javaagent/build.gradle.kts | 4 - .../groovy/JBossJavaUtilLoggingTest.groovy | 14 --- .../jboss-testing/build.gradle.kts | 25 +++++ .../groovy/JavaUtilLoggingJBossTest.groovy | 105 ++++++++++++++++++ settings.gradle.kts | 1 + 5 files changed, 131 insertions(+), 18 deletions(-) delete mode 100644 instrumentation/java-util-logging/javaagent/src/test/groovy/JBossJavaUtilLoggingTest.groovy create mode 100644 instrumentation/java-util-logging/jboss-testing/build.gradle.kts create mode 100644 instrumentation/java-util-logging/jboss-testing/src/test/groovy/JavaUtilLoggingJBossTest.groovy diff --git a/instrumentation/java-util-logging/javaagent/build.gradle.kts b/instrumentation/java-util-logging/javaagent/build.gradle.kts index 3804b4bcdd..165f30bd16 100644 --- a/instrumentation/java-util-logging/javaagent/build.gradle.kts +++ b/instrumentation/java-util-logging/javaagent/build.gradle.kts @@ -7,10 +7,6 @@ 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/test/groovy/JBossJavaUtilLoggingTest.groovy b/instrumentation/java-util-logging/javaagent/src/test/groovy/JBossJavaUtilLoggingTest.groovy deleted file mode 100644 index 3feb2a0169..0000000000 --- a/instrumentation/java-util-logging/javaagent/src/test/groovy/JBossJavaUtilLoggingTest.groovy +++ /dev/null @@ -1,14 +0,0 @@ -/* - * 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/jboss-testing/build.gradle.kts b/instrumentation/java-util-logging/jboss-testing/build.gradle.kts new file mode 100644 index 0000000000..d68113e50c --- /dev/null +++ b/instrumentation/java-util-logging/jboss-testing/build.gradle.kts @@ -0,0 +1,25 @@ +plugins { + id("otel.javaagent-testing") +} + +// separate testing module is needed, because presence of jboss-logmanager 2.1.6 or later +// on the classpath causes the normal java.util.logging test to use it + +dependencies { + compileOnly(project(":instrumentation:java-util-logging:shaded-stub-for-instrumenting")) + + compileOnly(project(":instrumentation-appender-api-internal")) + + testInstrumentation(project(":instrumentation:java-util-logging:javaagent")) + + // 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") +} + +tasks.withType().configureEach { + // TODO run tests both with and without experimental log attributes + jvmArgs("-Dotel.instrumentation.java-util-logging.experimental-log-attributes=true") +} diff --git a/instrumentation/java-util-logging/jboss-testing/src/test/groovy/JavaUtilLoggingJBossTest.groovy b/instrumentation/java-util-logging/jboss-testing/src/test/groovy/JavaUtilLoggingJBossTest.groovy new file mode 100644 index 0000000000..e2c5096ec7 --- /dev/null +++ b/instrumentation/java-util-logging/jboss-testing/src/test/groovy/JavaUtilLoggingJBossTest.groovy @@ -0,0 +1,105 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification +import io.opentelemetry.sdk.logs.data.Severity +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes +import org.jboss.logmanager.LogContext +import spock.lang.Shared +import spock.lang.Unroll + +import java.util.logging.Level + +import static org.assertj.core.api.Assertions.assertThat +import static org.awaitility.Awaitility.await + +class JavaUtilLoggingJBossTest extends AgentInstrumentationSpecification { + + @Shared + private final Object logger = LogContext.create().getLogger("abc") + + @Unroll + def "test method=#testMethod with testArgs=#testArgs and parent=#parent"() { + when: + if (parent) { + runWithSpan("parent") { + if (testArgs == "exception") { + logger.log(Level."${testMethod.toUpperCase()}", "xyz", new IllegalStateException("hello")) + } else if (testArgs == "params") { + logger.log(Level."${testMethod.toUpperCase()}", "xyz: {0}", 123) + } else { + logger."$testMethod"("xyz") + } + } + } else { + if (testArgs == "exception") { + logger.log(Level."${testMethod.toUpperCase()}", "xyz", new IllegalStateException("hello")) + } else if (testArgs == "params") { + logger.log(Level."${testMethod.toUpperCase()}", "xyz: {0}", 123) + } else { + logger."$testMethod"("xyz") + } + } + + then: + if (parent) { + waitForTraces(1) + } + + if (severity != null) { + await() + .untilAsserted( + () -> { + assertThat(logs).hasSize(1) + }) + def log = logs.get(0) + if (testArgs == "params") { + assertThat(log.getBody().asString()).isEqualTo("xyz: 123") + } else { + assertThat(log.getBody().asString()).isEqualTo("xyz") + } + assertThat(log.getInstrumentationLibraryInfo().getName()).isEqualTo("abc") + assertThat(log.getSeverity()).isEqualTo(severity) + assertThat(log.getSeverityText()).isEqualTo(severityText) + if (testArgs == "exception") { + assertThat(log.getAttributes().size()).isEqualTo(5) + assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_TYPE)).isEqualTo(IllegalStateException.getName()) + assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_MESSAGE)).isEqualTo("hello") + assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_STACKTRACE)).contains(JavaUtilLoggingJBossTest.name) + } else { + assertThat(log.getAttributes().size()).isEqualTo(2) + assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_TYPE)).isNull() + assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_MESSAGE)).isNull() + assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_STACKTRACE)).isNull() + } + assertThat(log.getAttributes().get(SemanticAttributes.THREAD_NAME)).isEqualTo(Thread.currentThread().getName()) + assertThat(log.getAttributes().get(SemanticAttributes.THREAD_ID)).isEqualTo(Thread.currentThread().getId()) + if (parent) { + assertThat(log.getSpanContext()).isEqualTo(traces.get(0).get(0).getSpanContext()) + } else { + assertThat(log.getSpanContext().isValid()).isFalse() + } + } else { + Thread.sleep(500) // sleep a bit just to make sure no log is captured + logs.size() == 0 + } + + where: + [args, testArgs, parent] << [ + [ + ["fine", null, null], + ["info", Severity.INFO, "INFO"], + ["warning", Severity.WARN, "WARNING"], + ["severe", Severity.ERROR, "SEVERE"] + ], + ["none", "exception", "param"], + [true, false] + ].combinations() + + testMethod = args[0] + severity = args[1] + severityText = args[2] + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index f55360b57e..e15ffe52dc 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -209,6 +209,7 @@ include(":instrumentation:http-url-connection:javaagent") include(":instrumentation:hystrix-1.4:javaagent") include(":instrumentation:java-http-client:javaagent") include(":instrumentation:java-util-logging:javaagent") +include(":instrumentation:java-util-logging:jboss-testing") include(":instrumentation:java-util-logging:shaded-stub-for-instrumenting") include(":instrumentation:jaxrs:jaxrs-common:bootstrap") include(":instrumentation:jaxrs:jaxrs-1.0:javaagent")