GrizzlyTest.groovy 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.AgentTestTrait
  7. import io.opentelemetry.instrumentation.test.base.HttpServerTest
  8. import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint
  9. import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
  10. import org.glassfish.grizzly.http.server.HttpHandler
  11. import org.glassfish.grizzly.http.server.HttpServer
  12. import org.glassfish.grizzly.http.server.Request
  13. import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory
  14. import org.glassfish.jersey.server.ResourceConfig
  15. import javax.ws.rs.GET
  16. import javax.ws.rs.NotFoundException
  17. import javax.ws.rs.Path
  18. import javax.ws.rs.QueryParam
  19. import javax.ws.rs.core.Response
  20. import javax.ws.rs.ext.ExceptionMapper
  21. import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.ERROR
  22. import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.EXCEPTION
  23. import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.INDEXED_CHILD
  24. import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.QUERY_PARAM
  25. import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT
  26. import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS
  27. class GrizzlyTest extends HttpServerTest<HttpServer> implements AgentTestTrait {
  28. @Override
  29. HttpServer startServer(int port) {
  30. ResourceConfig rc = new ResourceConfig()
  31. rc.register(SimpleExceptionMapper)
  32. rc.register(ServiceResource)
  33. def server = GrizzlyHttpServerFactory.createHttpServer(new URI("http://localhost:$port"), rc, false)
  34. // jersey doesn't propagate exceptions up to the grizzly handler
  35. // so we use a standalone HttpHandler to test exception capture
  36. server.getServerConfiguration().addHttpHandler(new ExceptionHttpHandler(), "/exception")
  37. server.start()
  38. return server
  39. }
  40. @Override
  41. Set<AttributeKey<?>> httpAttributes(ServerEndpoint endpoint) {
  42. def attributes = super.httpAttributes(endpoint)
  43. attributes.remove(SemanticAttributes.HTTP_ROUTE)
  44. attributes.remove(SemanticAttributes.NET_TRANSPORT)
  45. attributes
  46. }
  47. @Override
  48. void stopServer(HttpServer server) {
  49. server.stop()
  50. }
  51. @Override
  52. boolean hasResponseCustomizer(ServerEndpoint endpoint) {
  53. true
  54. }
  55. @Override
  56. boolean testCapturedHttpHeaders() {
  57. false
  58. }
  59. static class SimpleExceptionMapper implements ExceptionMapper<Throwable> {
  60. @Override
  61. Response toResponse(Throwable exception) {
  62. if (exception instanceof NotFoundException) {
  63. return exception.getResponse()
  64. }
  65. Response.status(500).entity(exception.message).build()
  66. }
  67. }
  68. @Path("/")
  69. static class ServiceResource {
  70. @GET
  71. @Path("success")
  72. Response success() {
  73. controller(SUCCESS) {
  74. Response.status(SUCCESS.status).entity(SUCCESS.body).build()
  75. }
  76. }
  77. @GET
  78. @Path("query")
  79. Response query_param(@QueryParam("some") String param) {
  80. controller(QUERY_PARAM) {
  81. Response.status(QUERY_PARAM.status).entity("some=$param".toString()).build()
  82. }
  83. }
  84. @GET
  85. @Path("redirect")
  86. Response redirect() {
  87. controller(REDIRECT) {
  88. Response.status(REDIRECT.status).location(new URI(REDIRECT.body)).build()
  89. }
  90. }
  91. @GET
  92. @Path("error-status")
  93. Response error() {
  94. controller(ERROR) {
  95. Response.status(ERROR.status).entity(ERROR.body).build()
  96. }
  97. }
  98. @GET
  99. @Path("child")
  100. Response exception(@QueryParam("id") String id) {
  101. controller(INDEXED_CHILD) {
  102. INDEXED_CHILD.collectSpanAttributes { it == "id" ? id : null }
  103. Response.status(INDEXED_CHILD.status).entity(INDEXED_CHILD.body).build()
  104. }
  105. }
  106. }
  107. static class ExceptionHttpHandler extends HttpHandler {
  108. @Override
  109. void service(Request request, org.glassfish.grizzly.http.server.Response response) throws Exception {
  110. controller(EXCEPTION) {
  111. throw new Exception(EXCEPTION.body)
  112. }
  113. }
  114. }
  115. }