|
| 1 | +package server; |
| 2 | + |
| 3 | +import static org.mockito.ArgumentMatchers.any; |
| 4 | +import static org.mockito.ArgumentMatchers.eq; |
| 5 | +import static org.mockito.Mockito.mock; |
| 6 | +import static org.mockito.Mockito.never; |
| 7 | +import static org.mockito.Mockito.times; |
| 8 | +import static org.mockito.Mockito.verify; |
| 9 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 10 | +import static org.mockito.Mockito.when; |
| 11 | + |
| 12 | +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
| 13 | +import datadog.trace.bootstrap.instrumentation.api.ResourceNamePriorities; |
| 14 | +import datadog.trace.bootstrap.instrumentation.api.Tags; |
| 15 | +import datadog.trace.instrumentation.vertx_3_4.server.RouteUpdateHelper; |
| 16 | +import io.vertx.core.http.HttpServerRequest; |
| 17 | +import io.vertx.ext.web.Route; |
| 18 | +import io.vertx.ext.web.RoutingContext; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +class RouteHandlerWrapperTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + void updateRouteWritesRouteToBothSpans() { |
| 25 | + RoutingContext context = mock(RoutingContext.class); |
| 26 | + HttpServerRequest request = mock(HttpServerRequest.class); |
| 27 | + Route route = mock(Route.class); |
| 28 | + AgentSpan parentSpan = mock(AgentSpan.class); |
| 29 | + AgentSpan handlerSpan = mock(AgentSpan.class); |
| 30 | + when(route.getPath()).thenReturn("/items/:id"); |
| 31 | + when(context.mountPoint()).thenReturn(null); |
| 32 | + when(context.request()).thenReturn(request); |
| 33 | + when(request.path()).thenReturn("/items/123"); |
| 34 | + when(request.rawMethod()).thenReturn("GET"); |
| 35 | + when(context.get("dd." + Tags.HTTP_ROUTE)).thenReturn(null); |
| 36 | + when(handlerSpan.getSpanName()).thenReturn("vertx.route-handler"); |
| 37 | + when(handlerSpan.getResourceNamePriority()).thenReturn(Byte.MIN_VALUE); |
| 38 | + |
| 39 | + RouteUpdateHelper.updateRouteFromMatchedRoute(context, route, parentSpan, handlerSpan); |
| 40 | + |
| 41 | + verify(route).getPath(); |
| 42 | + verify(context).mountPoint(); |
| 43 | + verify(context, times(2)).request(); |
| 44 | + verify(request).path(); |
| 45 | + verify(request).rawMethod(); |
| 46 | + verify(context).get("dd." + Tags.HTTP_ROUTE); |
| 47 | + verify(context).put("dd.vertx.matched_route", "/items/:id"); |
| 48 | + verify(context).put("dd." + Tags.HTTP_ROUTE, "/items/:id"); |
| 49 | + verify(parentSpan).setTag(Tags.HTTP_ROUTE, (CharSequence) "/items/:id"); |
| 50 | + verify(parentSpan) |
| 51 | + .setResourceName(any(CharSequence.class), eq(ResourceNamePriorities.HTTP_FRAMEWORK_ROUTE)); |
| 52 | + verify(handlerSpan).getSpanName(); |
| 53 | + verify(handlerSpan).getResourceNamePriority(); |
| 54 | + verify(handlerSpan).setTag(Tags.HTTP_ROUTE, (CharSequence) "/items/:id"); |
| 55 | + verify(handlerSpan) |
| 56 | + .setResourceName(any(CharSequence.class), eq(ResourceNamePriorities.HTTP_FRAMEWORK_ROUTE)); |
| 57 | + verifyNoMoreInteractions(context, request, route, parentSpan, handlerSpan); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void updateRouteDoesNotWriteRouteToNonVertxHandlerSpan() { |
| 62 | + RoutingContext context = mock(RoutingContext.class); |
| 63 | + HttpServerRequest request = mock(HttpServerRequest.class); |
| 64 | + Route route = mock(Route.class); |
| 65 | + AgentSpan parentSpan = mock(AgentSpan.class); |
| 66 | + AgentSpan handlerSpan = mock(AgentSpan.class); |
| 67 | + when(route.getPath()).thenReturn("/items/:id"); |
| 68 | + when(context.mountPoint()).thenReturn(null); |
| 69 | + when(context.request()).thenReturn(request); |
| 70 | + when(request.path()).thenReturn("/items/123"); |
| 71 | + when(request.rawMethod()).thenReturn("GET"); |
| 72 | + when(context.get("dd." + Tags.HTTP_ROUTE)).thenReturn(null); |
| 73 | + when(handlerSpan.getSpanName()).thenReturn("some.other.span"); |
| 74 | + |
| 75 | + RouteUpdateHelper.updateRouteFromMatchedRoute(context, route, parentSpan, handlerSpan); |
| 76 | + |
| 77 | + verify(route).getPath(); |
| 78 | + verify(context).mountPoint(); |
| 79 | + verify(context, times(2)).request(); |
| 80 | + verify(request).path(); |
| 81 | + verify(request).rawMethod(); |
| 82 | + verify(context).get("dd." + Tags.HTTP_ROUTE); |
| 83 | + verify(context).put("dd.vertx.matched_route", "/items/:id"); |
| 84 | + verify(context).put("dd." + Tags.HTTP_ROUTE, "/items/:id"); |
| 85 | + verify(parentSpan).setTag(Tags.HTTP_ROUTE, (CharSequence) "/items/:id"); |
| 86 | + verify(parentSpan) |
| 87 | + .setResourceName(any(CharSequence.class), eq(ResourceNamePriorities.HTTP_FRAMEWORK_ROUTE)); |
| 88 | + verify(handlerSpan).getSpanName(); |
| 89 | + verify(handlerSpan, never()).setTag(any(String.class), any(CharSequence.class)); |
| 90 | + verifyNoMoreInteractions(context, request, route, parentSpan, handlerSpan); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void updateRouteDoesNotReplaceRootRouteWhenOneExists() { |
| 95 | + RoutingContext context = mock(RoutingContext.class); |
| 96 | + HttpServerRequest request = mock(HttpServerRequest.class); |
| 97 | + Route route = mock(Route.class); |
| 98 | + AgentSpan parentSpan = mock(AgentSpan.class); |
| 99 | + AgentSpan handlerSpan = mock(AgentSpan.class); |
| 100 | + when(route.getPath()).thenReturn("/"); |
| 101 | + when(context.mountPoint()).thenReturn(null); |
| 102 | + when(context.request()).thenReturn(request); |
| 103 | + when(request.path()).thenReturn("/"); |
| 104 | + when(request.rawMethod()).thenReturn("GET"); |
| 105 | + when(context.get("dd." + Tags.HTTP_ROUTE)).thenReturn(null); |
| 106 | + when(parentSpan.getTag(Tags.HTTP_ROUTE)).thenReturn("/existing"); |
| 107 | + |
| 108 | + RouteUpdateHelper.updateRouteFromMatchedRoute(context, route, parentSpan, handlerSpan); |
| 109 | + |
| 110 | + verify(route).getPath(); |
| 111 | + verify(context).mountPoint(); |
| 112 | + verify(context, times(2)).request(); |
| 113 | + verify(request).path(); |
| 114 | + verify(request).rawMethod(); |
| 115 | + verify(context).get("dd." + Tags.HTTP_ROUTE); |
| 116 | + verify(context).put("dd.vertx.matched_route", "/"); |
| 117 | + verify(parentSpan).getTag(Tags.HTTP_ROUTE); |
| 118 | + verify(context, never()).put("dd." + Tags.HTTP_ROUTE, "/"); |
| 119 | + verify(parentSpan, never()).setTag(any(String.class), any(CharSequence.class)); |
| 120 | + verify(handlerSpan, never()).setTag(any(String.class), any(CharSequence.class)); |
| 121 | + verifyNoMoreInteractions(context, request, route, parentSpan, handlerSpan); |
| 122 | + } |
| 123 | +} |
0 commit comments