HystrixTest.groovy 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright The OpenTelemetry Authors
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. import static com.netflix.hystrix.HystrixCommandGroupKey.Factory.asKey
  6. import static io.opentelemetry.api.trace.StatusCode.ERROR
  7. import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runInternalSpan
  8. import com.netflix.hystrix.HystrixCommand
  9. import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
  10. import java.util.concurrent.BlockingQueue
  11. import java.util.concurrent.LinkedBlockingQueue
  12. class HystrixTest extends AgentInstrumentationSpecification {
  13. def "test command #action"() {
  14. setup:
  15. def command = new HystrixCommand<String>(asKey("ExampleGroup")) {
  16. @Override
  17. protected String run() throws Exception {
  18. return tracedMethod()
  19. }
  20. private String tracedMethod() {
  21. runInternalSpan("tracedMethod")
  22. return "Hello!"
  23. }
  24. }
  25. def result = runWithSpan("parent") {
  26. operation(command)
  27. }
  28. expect:
  29. result == "Hello!"
  30. assertTraces(1) {
  31. trace(0, 3) {
  32. span(0) {
  33. name "parent"
  34. hasNoParent()
  35. attributes {
  36. }
  37. }
  38. span(1) {
  39. name "ExampleGroup.HystrixTest\$1.execute"
  40. childOf span(0)
  41. attributes {
  42. "hystrix.command" "HystrixTest\$1"
  43. "hystrix.group" "ExampleGroup"
  44. "hystrix.circuit_open" false
  45. }
  46. }
  47. span(2) {
  48. name "tracedMethod"
  49. childOf span(1)
  50. attributes {
  51. }
  52. }
  53. }
  54. }
  55. where:
  56. action | operation
  57. "execute" | { HystrixCommand cmd -> cmd.execute() }
  58. "queue" | { HystrixCommand cmd -> cmd.queue().get() }
  59. "toObservable" | { HystrixCommand cmd -> cmd.toObservable().toBlocking().first() }
  60. "observe" | { HystrixCommand cmd -> cmd.observe().toBlocking().first() }
  61. "observe block" | { HystrixCommand cmd ->
  62. BlockingQueue queue = new LinkedBlockingQueue()
  63. cmd.observe().subscribe { next ->
  64. queue.put(next)
  65. }
  66. queue.take()
  67. }
  68. }
  69. def "test command #action fallback"() {
  70. setup:
  71. def command = new HystrixCommand<String>(asKey("ExampleGroup")) {
  72. @Override
  73. protected String run() throws Exception {
  74. throw new IllegalArgumentException()
  75. }
  76. protected String getFallback() {
  77. return "Fallback!"
  78. }
  79. }
  80. def result = runWithSpan("parent") {
  81. operation(command)
  82. }
  83. expect:
  84. result == "Fallback!"
  85. assertTraces(1) {
  86. trace(0, 3) {
  87. span(0) {
  88. name "parent"
  89. hasNoParent()
  90. attributes {
  91. }
  92. }
  93. span(1) {
  94. name "ExampleGroup.HystrixTest\$2.execute"
  95. childOf span(0)
  96. status ERROR
  97. errorEvent(IllegalArgumentException)
  98. attributes {
  99. "hystrix.command" "HystrixTest\$2"
  100. "hystrix.group" "ExampleGroup"
  101. "hystrix.circuit_open" false
  102. }
  103. }
  104. span(2) {
  105. name "ExampleGroup.HystrixTest\$2.fallback"
  106. childOf span(1)
  107. attributes {
  108. "hystrix.command" "HystrixTest\$2"
  109. "hystrix.group" "ExampleGroup"
  110. "hystrix.circuit_open" false
  111. }
  112. }
  113. }
  114. }
  115. where:
  116. action | operation
  117. "execute" | { HystrixCommand cmd -> cmd.execute() }
  118. "queue" | { HystrixCommand cmd -> cmd.queue().get() }
  119. "toObservable" | { HystrixCommand cmd -> cmd.toObservable().toBlocking().first() }
  120. "observe" | { HystrixCommand cmd -> cmd.observe().toBlocking().first() }
  121. "observe block" | { HystrixCommand cmd ->
  122. BlockingQueue queue = new LinkedBlockingQueue()
  123. cmd.observe().subscribe { next ->
  124. queue.put(next)
  125. }
  126. queue.take()
  127. }
  128. }
  129. }