|
@@ -3,43 +3,41 @@
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
*/
|
|
|
|
|
|
+import io.opentelemetry.api.common.AttributeKey
|
|
|
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
|
|
|
import io.opentelemetry.sdk.logs.data.Severity
|
|
|
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
|
|
|
+import org.jboss.logmanager.MDC
|
|
|
+import org.jboss.logmanager.Level
|
|
|
import org.jboss.logmanager.LogContext
|
|
|
-import spock.lang.Shared
|
|
|
import spock.lang.Unroll
|
|
|
-
|
|
|
-import java.util.logging.Level
|
|
|
+import org.jboss.logmanager.Logger
|
|
|
|
|
|
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")
|
|
|
+class JbossLogmanagerTest extends AgentInstrumentationSpecification {
|
|
|
+ private static final Logger logger = LogContext.getLogContext().getLogger("abc")
|
|
|
+ static {
|
|
|
+ logger.setLevel(Level.INFO)
|
|
|
+ }
|
|
|
|
|
|
@Unroll
|
|
|
- def "test method=#testMethod with testArgs=#testArgs and parent=#parent"() {
|
|
|
+ def "test testMethod=#testMethod, exception=#exception, parent=#parent"(Level testMethod, boolean exception, boolean 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)
|
|
|
+ if (exception) {
|
|
|
+ logger.log(testMethod, "xyz", new IllegalStateException("hello"))
|
|
|
} else {
|
|
|
- logger."$testMethod"("xyz")
|
|
|
+ logger.log(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)
|
|
|
+ if (exception) {
|
|
|
+ logger.log(testMethod, "xyz", new IllegalStateException("hello"))
|
|
|
} else {
|
|
|
- logger."$testMethod"("xyz")
|
|
|
+ logger.log(testMethod, "xyz")
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -55,19 +53,15 @@ class JavaUtilLoggingJBossTest extends AgentInstrumentationSpecification {
|
|
|
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.getBody().asString()).isEqualTo("xyz")
|
|
|
assertThat(log.getInstrumentationLibraryInfo().getName()).isEqualTo("abc")
|
|
|
assertThat(log.getSeverity()).isEqualTo(severity)
|
|
|
assertThat(log.getSeverityText()).isEqualTo(severityText)
|
|
|
- if (testArgs == "exception") {
|
|
|
+ if (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)
|
|
|
+ assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_STACKTRACE)).contains(JbossLogmanagerTest.name)
|
|
|
} else {
|
|
|
assertThat(log.getAttributes().size()).isEqualTo(2)
|
|
|
assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_TYPE)).isNull()
|
|
@@ -83,23 +77,53 @@ class JavaUtilLoggingJBossTest extends AgentInstrumentationSpecification {
|
|
|
}
|
|
|
} else {
|
|
|
Thread.sleep(500) // sleep a bit just to make sure no log is captured
|
|
|
- logs.size() == 0
|
|
|
+ assertThat(logs.size() == 0).isTrue()
|
|
|
}
|
|
|
|
|
|
where:
|
|
|
- [args, testArgs, parent] << [
|
|
|
+ [args, exception, parent] << [
|
|
|
[
|
|
|
- ["fine", null, null],
|
|
|
- ["info", Severity.INFO, "INFO"],
|
|
|
- ["warning", Severity.WARN, "WARNING"],
|
|
|
- ["severe", Severity.ERROR, "SEVERE"]
|
|
|
+ [Level.DEBUG, null, null],
|
|
|
+ [Level.INFO, Severity.INFO, "INFO"],
|
|
|
+ [Level.WARN, Severity.WARN, "WARN"],
|
|
|
+ [Level.ERROR, Severity.ERROR, "ERROR"]
|
|
|
],
|
|
|
- ["none", "exception", "param"],
|
|
|
+ [true, false],
|
|
|
[true, false]
|
|
|
].combinations()
|
|
|
|
|
|
- testMethod = args[0]
|
|
|
+ testMethod = args[0] as Level
|
|
|
severity = args[1]
|
|
|
severityText = args[2]
|
|
|
}
|
|
|
+
|
|
|
+ def "test mdc"() {
|
|
|
+ when:
|
|
|
+ MDC.put("key1", "val1")
|
|
|
+ MDC.put("key2", "val2")
|
|
|
+ try {
|
|
|
+ logger.info("xyz")
|
|
|
+ } finally {
|
|
|
+ MDC.remove("key1")
|
|
|
+ MDC.remove("key2")
|
|
|
+ }
|
|
|
+
|
|
|
+ then:
|
|
|
+
|
|
|
+ await()
|
|
|
+ .untilAsserted(
|
|
|
+ () -> {
|
|
|
+ assertThat(logs).hasSize(1)
|
|
|
+ })
|
|
|
+ def log = logs.get(0)
|
|
|
+ assertThat(log.getBody().asString()).isEqualTo("xyz")
|
|
|
+ assertThat(log.getInstrumentationLibraryInfo().getName()).isEqualTo("abc")
|
|
|
+ assertThat(log.getSeverity()).isEqualTo(Severity.INFO)
|
|
|
+ assertThat(log.getSeverityText()).isEqualTo("INFO")
|
|
|
+ assertThat(log.getAttributes().size()).isEqualTo(4)
|
|
|
+ assertThat(log.getAttributes().get(AttributeKey.stringKey("jboss-logmanager.mdc.key1"))).isEqualTo("val1")
|
|
|
+ assertThat(log.getAttributes().get(AttributeKey.stringKey("jboss-logmanager.mdc.key2"))).isEqualTo("val2")
|
|
|
+ assertThat(log.getAttributes().get(SemanticAttributes.THREAD_NAME)).isEqualTo(Thread.currentThread().getName())
|
|
|
+ assertThat(log.getAttributes().get(SemanticAttributes.THREAD_ID)).isEqualTo(Thread.currentThread().getId())
|
|
|
+ }
|
|
|
}
|