JaxrsAnnotationsInstrumentationTest.groovy 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright The OpenTelemetry Authors
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. import io.opentelemetry.instrumentation.api.instrumenter.http.internal.HttpAttributes
  6. import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
  7. import io.opentelemetry.semconv.SemanticAttributes
  8. import spock.lang.Unroll
  9. import javax.ws.rs.DELETE
  10. import javax.ws.rs.GET
  11. import javax.ws.rs.HEAD
  12. import javax.ws.rs.OPTIONS
  13. import javax.ws.rs.POST
  14. import javax.ws.rs.PUT
  15. import javax.ws.rs.Path
  16. import static io.opentelemetry.api.trace.SpanKind.SERVER
  17. import static io.opentelemetry.instrumentation.test.utils.ClassUtils.getClassName
  18. class JaxrsAnnotationsInstrumentationTest extends AgentInstrumentationSpecification {
  19. @Unroll
  20. def "span named '#paramName' from annotations on class '#className' when is not root span"() {
  21. setup:
  22. runWithHttpServerSpan {
  23. obj.call()
  24. }
  25. expect:
  26. assertTraces(1) {
  27. trace(0, 2) {
  28. span(0) {
  29. name "GET " + paramName
  30. kind SERVER
  31. hasNoParent()
  32. attributes {
  33. "$SemanticAttributes.HTTP_REQUEST_METHOD" "GET"
  34. "$SemanticAttributes.HTTP_ROUTE" paramName
  35. "$HttpAttributes.ERROR_TYPE" "_OTHER"
  36. }
  37. }
  38. span(1) {
  39. name "${className}.call"
  40. childOf span(0)
  41. attributes {
  42. "$SemanticAttributes.CODE_NAMESPACE" obj.getClass().getName()
  43. "$SemanticAttributes.CODE_FUNCTION" "call"
  44. }
  45. }
  46. }
  47. }
  48. when: "multiple calls to the same method"
  49. runWithHttpServerSpan {
  50. (1..10).each {
  51. obj.call()
  52. }
  53. }
  54. then: "doesn't increase the cache size"
  55. where:
  56. paramName | obj
  57. "/a" | new Jax() {
  58. @Path("/a")
  59. void call() {
  60. }
  61. }
  62. "/b" | new Jax() {
  63. @GET
  64. @Path("/b")
  65. void call() {
  66. }
  67. }
  68. "/interface/c" | new InterfaceWithPath() {
  69. @POST
  70. @Path("/c")
  71. void call() {
  72. }
  73. }
  74. "/interface" | new InterfaceWithPath() {
  75. @HEAD
  76. void call() {
  77. }
  78. }
  79. "/abstract/d" | new AbstractClassWithPath() {
  80. @POST
  81. @Path("/d")
  82. void call() {
  83. }
  84. }
  85. "/abstract" | new AbstractClassWithPath() {
  86. @PUT
  87. void call() {
  88. }
  89. }
  90. "/child/e" | new ChildClassWithPath() {
  91. @OPTIONS
  92. @Path("/e")
  93. void call() {
  94. }
  95. }
  96. "/child/call" | new ChildClassWithPath() {
  97. @DELETE
  98. void call() {
  99. }
  100. }
  101. "/child/call" | new ChildClassWithPath()
  102. "/child/call" | new JavaInterfaces.ChildClassOnInterface()
  103. "/child/call" | new JavaInterfaces.DefaultChildClassOnInterface()
  104. className = getClassName(obj.class)
  105. }
  106. def "no annotations has no effect"() {
  107. setup:
  108. runWithHttpServerSpan {
  109. obj.call()
  110. }
  111. expect:
  112. assertTraces(1) {
  113. trace(0, 1) {
  114. span(0) {
  115. name "GET"
  116. kind SERVER
  117. attributes {
  118. "$SemanticAttributes.HTTP_REQUEST_METHOD" "GET"
  119. "$HttpAttributes.ERROR_TYPE" "_OTHER"
  120. }
  121. }
  122. }
  123. }
  124. where:
  125. obj | _
  126. new Jax() {
  127. void call() {
  128. }
  129. } | _
  130. }
  131. interface Jax {
  132. void call()
  133. }
  134. @Path("/interface")
  135. interface InterfaceWithPath extends Jax {
  136. @GET
  137. void call()
  138. }
  139. @Path("/abstract")
  140. static abstract class AbstractClassWithPath implements Jax {
  141. @PUT
  142. abstract void call()
  143. }
  144. @Path("child")
  145. static class ChildClassWithPath extends AbstractClassWithPath {
  146. @Path("call")
  147. @POST
  148. void call() {
  149. }
  150. }
  151. }