SpringSchedulingTest.groovy 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright The OpenTelemetry Authors
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. import io.opentelemetry.api.trace.StatusCode
  6. import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
  7. import io.opentelemetry.semconv.SemanticAttributes
  8. import org.springframework.context.annotation.AnnotationConfigApplicationContext
  9. import java.util.concurrent.CountDownLatch
  10. import java.util.concurrent.TimeUnit
  11. class SpringSchedulingTest extends AgentInstrumentationSpecification {
  12. def "schedule one time test"() {
  13. setup:
  14. def context = new AnnotationConfigApplicationContext(OneTimeTaskConfig)
  15. def task = context.getBean(OneTimeTask)
  16. task.blockUntilExecute()
  17. expect:
  18. assert task != null
  19. assertTraces(0) {}
  20. }
  21. def "schedule trigger test according to cron expression"() {
  22. setup:
  23. def context = new AnnotationConfigApplicationContext(TriggerTaskConfig)
  24. def task = context.getBean(TriggerTask)
  25. task.blockUntilExecute()
  26. expect:
  27. assert task != null
  28. assertTraces(1) {
  29. trace(0, 1) {
  30. span(0) {
  31. name "TriggerTask.run"
  32. hasNoParent()
  33. attributes {
  34. "job.system" "spring_scheduling"
  35. "code.namespace" "TriggerTask"
  36. "code.function" "run"
  37. }
  38. }
  39. }
  40. }
  41. }
  42. def "schedule interval test"() {
  43. setup:
  44. def context = new AnnotationConfigApplicationContext(IntervalTaskConfig)
  45. def task = context.getBean(IntervalTask)
  46. task.blockUntilExecute()
  47. expect:
  48. assert task != null
  49. assertTraces(1) {
  50. trace(0, 1) {
  51. span(0) {
  52. name "IntervalTask.run"
  53. hasNoParent()
  54. attributes {
  55. "job.system" "spring_scheduling"
  56. "code.namespace" "IntervalTask"
  57. "code.function" "run"
  58. }
  59. }
  60. }
  61. }
  62. }
  63. def "schedule lambda test"() {
  64. setup:
  65. def context = new AnnotationConfigApplicationContext(LambdaTaskConfig)
  66. def configurer = context.getBean(LambdaTaskConfigurer)
  67. configurer.singleUseLatch.await(2000, TimeUnit.MILLISECONDS)
  68. expect:
  69. assertTraces(1) {
  70. trace(0, 1) {
  71. span(0) {
  72. name "LambdaTaskConfigurer\$\$Lambda.run"
  73. hasNoParent()
  74. attributes {
  75. "job.system" "spring_scheduling"
  76. "code.namespace" { it.startsWith("LambdaTaskConfigurer\$\$Lambda") }
  77. "code.function" "run"
  78. }
  79. }
  80. }
  81. }
  82. cleanup:
  83. context.close()
  84. }
  85. // by putting the scheduled method directly on the TaskConfig, this verifies the case where the
  86. // class is enhanced and so has a different class name, e.g. TaskConfig$$EnhancerByCGLIB$$b910c4a9
  87. def "schedule enhanced class test"() {
  88. setup:
  89. def context = new AnnotationConfigApplicationContext(EnhancedClassTaskConfig)
  90. def latch = context.getBean(CountDownLatch)
  91. latch.await(5, TimeUnit.SECONDS)
  92. expect:
  93. assertTraces(1) {
  94. trace(0, 1) {
  95. span(0) {
  96. name "EnhancedClassTaskConfig.run"
  97. hasNoParent()
  98. attributes {
  99. "job.system" "spring_scheduling"
  100. "code.namespace" "EnhancedClassTaskConfig"
  101. "code.function" "run"
  102. }
  103. }
  104. }
  105. }
  106. }
  107. def "task with error test"() {
  108. setup:
  109. def context = new AnnotationConfigApplicationContext(TaskWithErrorConfig)
  110. def task = context.getBean(TaskWithError)
  111. task.blockUntilExecute()
  112. expect:
  113. assert task != null
  114. assertTraces(1) {
  115. trace(0, 2) {
  116. span(0) {
  117. name "TaskWithError.run"
  118. hasNoParent()
  119. status StatusCode.ERROR
  120. attributes {
  121. "job.system" "spring_scheduling"
  122. "code.namespace" "TaskWithError"
  123. "code.function" "run"
  124. }
  125. event(0) {
  126. eventName "$SemanticAttributes.EXCEPTION_EVENT_NAME"
  127. attributes {
  128. "$SemanticAttributes.EXCEPTION_TYPE" IllegalStateException.getName()
  129. "$SemanticAttributes.EXCEPTION_MESSAGE" "failure"
  130. "$SemanticAttributes.EXCEPTION_STACKTRACE" String
  131. }
  132. }
  133. }
  134. span(1) {
  135. name "error-handler"
  136. childOf(span(0))
  137. }
  138. }
  139. }
  140. }
  141. }