JbossLogmanagerTest.groovy 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright The OpenTelemetry Authors
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. import io.opentelemetry.api.common.AttributeKey
  6. import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
  7. import io.opentelemetry.api.logs.Severity
  8. import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
  9. import org.jboss.logmanager.MDC
  10. import org.jboss.logmanager.Level
  11. import org.jboss.logmanager.LogContext
  12. import spock.lang.Unroll
  13. import org.jboss.logmanager.Logger
  14. import static org.assertj.core.api.Assertions.assertThat
  15. import static org.awaitility.Awaitility.await
  16. class JbossLogmanagerTest extends AgentInstrumentationSpecification {
  17. private static final Logger logger = LogContext.getLogContext().getLogger("abc")
  18. static {
  19. logger.setLevel(Level.INFO)
  20. }
  21. @Unroll
  22. def "test testMethod=#testMethod, exception=#exception, parent=#parent"(Level testMethod, boolean exception, boolean parent) {
  23. when:
  24. if (parent) {
  25. runWithSpan("parent") {
  26. if (exception) {
  27. logger.log(testMethod, "xyz", new IllegalStateException("hello"))
  28. } else {
  29. logger.log(testMethod, "xyz")
  30. }
  31. }
  32. } else {
  33. if (exception) {
  34. logger.log(testMethod, "xyz", new IllegalStateException("hello"))
  35. } else {
  36. logger.log(testMethod, "xyz")
  37. }
  38. }
  39. then:
  40. if (parent) {
  41. waitForTraces(1)
  42. }
  43. if (severity != null) {
  44. await()
  45. .untilAsserted(
  46. () -> {
  47. assertThat(logRecords).hasSize(1)
  48. })
  49. def log = logRecords.get(0)
  50. assertThat(log.getBody().asString()).isEqualTo("xyz")
  51. assertThat(log.getInstrumentationScopeInfo().getName()).isEqualTo("abc")
  52. assertThat(log.getSeverity()).isEqualTo(severity)
  53. assertThat(log.getSeverityText()).isEqualTo(severityText)
  54. if (exception) {
  55. assertThat(log.getAttributes().size()).isEqualTo(5)
  56. assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_TYPE)).isEqualTo(IllegalStateException.getName())
  57. assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_MESSAGE)).isEqualTo("hello")
  58. assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_STACKTRACE)).contains(JbossLogmanagerTest.name)
  59. } else {
  60. assertThat(log.getAttributes().size()).isEqualTo(2)
  61. assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_TYPE)).isNull()
  62. assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_MESSAGE)).isNull()
  63. assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_STACKTRACE)).isNull()
  64. }
  65. assertThat(log.getAttributes().get(SemanticAttributes.THREAD_NAME)).isEqualTo(Thread.currentThread().getName())
  66. assertThat(log.getAttributes().get(SemanticAttributes.THREAD_ID)).isEqualTo(Thread.currentThread().getId())
  67. if (parent) {
  68. assertThat(log.getSpanContext()).isEqualTo(traces.get(0).get(0).getSpanContext())
  69. } else {
  70. assertThat(log.getSpanContext().isValid()).isFalse()
  71. }
  72. } else {
  73. Thread.sleep(500) // sleep a bit just to make sure no log is captured
  74. assertThat(logRecords.size() == 0).isTrue()
  75. }
  76. where:
  77. [args, exception, parent] << [
  78. [
  79. [Level.DEBUG, null, null],
  80. [Level.INFO, Severity.INFO, "INFO"],
  81. [Level.WARN, Severity.WARN, "WARN"],
  82. [Level.ERROR, Severity.ERROR, "ERROR"]
  83. ],
  84. [true, false],
  85. [true, false]
  86. ].combinations()
  87. testMethod = args[0] as Level
  88. severity = args[1]
  89. severityText = args[2]
  90. }
  91. def "test mdc"() {
  92. when:
  93. MDC.put("key1", "val1")
  94. MDC.put("key2", "val2")
  95. try {
  96. logger.info("xyz")
  97. } finally {
  98. MDC.remove("key1")
  99. MDC.remove("key2")
  100. }
  101. then:
  102. await()
  103. .untilAsserted(
  104. () -> {
  105. assertThat(logRecords).hasSize(1)
  106. })
  107. def log = logRecords.get(0)
  108. assertThat(log.getBody().asString()).isEqualTo("xyz")
  109. assertThat(log.getInstrumentationScopeInfo().getName()).isEqualTo("abc")
  110. assertThat(log.getSeverity()).isEqualTo(Severity.INFO)
  111. assertThat(log.getSeverityText()).isEqualTo("INFO")
  112. assertThat(log.getAttributes().size()).isEqualTo(4)
  113. assertThat(log.getAttributes().get(AttributeKey.stringKey("jboss-logmanager.mdc.key1"))).isEqualTo("val1")
  114. assertThat(log.getAttributes().get(AttributeKey.stringKey("jboss-logmanager.mdc.key2"))).isEqualTo("val2")
  115. assertThat(log.getAttributes().get(SemanticAttributes.THREAD_NAME)).isEqualTo(Thread.currentThread().getName())
  116. assertThat(log.getAttributes().get(SemanticAttributes.THREAD_ID)).isEqualTo(Thread.currentThread().getId())
  117. }
  118. }