@BeforeMethod public void setup() { when(requestAdapter.getFormat()).thenReturn(Format.Builtin.TEXT_MAP); when(requestAdapter.getCarrier()).thenReturn(carrier); when(requestAdapter.getSpanStoreAdapter()).thenReturn(spanStoreAdapter); when(tracer.buildSpan(anyString(), anyString(), anyBoolean())).thenReturn(spanBuilder); when(spanBuilder.build()).thenReturn(span); when(span.context()).thenReturn(context); }
@Test public void properties() { AsyncClientRequestAdapter<TextMap> adapter = sensor.getAsyncClientRequestAdapter(object, new Object[] { httpExchange }, rsc); assertThat(adapter.getPropagationType(), is(PropagationType.HTTP)); assertThat(adapter.getReferenceType(), is(References.FOLLOWS_FROM)); assertThat(adapter.getFormat(), is(Format.Builtin.HTTP_HEADERS)); verifyZeroInteractions(object, rsc); }
@Test public void methodNull() { when(httpExchange.getMethod()).thenReturn(null); AsyncClientRequestAdapter<TextMap> adapter = sensor.getAsyncClientRequestAdapter(object, new Object[] { httpExchange }, rsc); Map<String, String> tags = adapter.getTags(); assertThat(tags.size(), is(0)); verifyZeroInteractions(object, rsc); }
@Test public void tagsNull() { when(requestAdapter.startClientSpan()).thenReturn(true); when(requestAdapter.getTags()).thenReturn(null); when(requestAdapter.getPropagationType()).thenReturn(PropagationType.HTTP); when(requestAdapter.getReferenceType()).thenReturn(References.FOLLOWS_FROM); SpanImpl result = interceptor.handleAsyncRequest(requestAdapter); assertThat(result, is(span)); verify(tracer).buildSpan(null, References.FOLLOWS_FROM, true); verify(tracer).inject(context, Format.Builtin.TEXT_MAP, carrier); verify(spanBuilder).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT); verify(spanBuilder).withTag(ExtraTags.PROPAGATION_TYPE, PropagationType.HTTP.toString()); verify(spanBuilder).build(); verify(span).context(); ArgumentCaptor<SpanStore> spanStoreCaptor = ArgumentCaptor.forClass(SpanStore.class); verify(spanStoreAdapter).setSpanStore(spanStoreCaptor.capture()); assertThat(spanStoreCaptor.getValue().getSpan(), is(span)); verifyNoMoreInteractions(tracer, spanBuilder, span); verifyZeroInteractions(context); }
@Test public void propagationNull() { when(requestAdapter.startClientSpan()).thenReturn(true); when(requestAdapter.getPropagationType()).thenReturn(null); when(requestAdapter.getReferenceType()).thenReturn(References.FOLLOWS_FROM); SpanImpl result = interceptor.handleAsyncRequest(requestAdapter); assertThat(result, is(span)); verify(tracer).buildSpan(null, References.FOLLOWS_FROM, true); verify(tracer).inject(context, Format.Builtin.TEXT_MAP, carrier); verify(spanBuilder).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT); verify(spanBuilder).build(); verify(span).context(); ArgumentCaptor<SpanStore> spanStoreCaptor = ArgumentCaptor.forClass(SpanStore.class); verify(spanStoreAdapter).setSpanStore(spanStoreCaptor.capture()); assertThat(spanStoreCaptor.getValue().getSpan(), is(span)); verifyNoMoreInteractions(tracer, spanBuilder, span); verifyZeroInteractions(context); } }
@Test public void spanStoreListenerNull() { Proxy proxyListener = new Proxy(); when(httpExchange.getEventListener()).thenReturn(null); when(runtimeLinker.createProxy(eq(JettyEventListenerProxy.class), Mockito.<JettyEventListenerProxy> any(), Mockito.<ClassLoader> any())).thenReturn(proxyListener); AsyncClientRequestAdapter<TextMap> adapter = sensor.getAsyncClientRequestAdapter(object, new Object[] { httpExchange }, rsc); adapter.getSpanStoreAdapter().setSpanStore(spanStore); verify(httpExchange).setEventListener(proxyListener); ArgumentCaptor<JettyEventListenerProxy> proxyCaptor = ArgumentCaptor.forClass(JettyEventListenerProxy.class); verify(runtimeLinker).createProxy(eq(JettyEventListenerProxy.class), proxyCaptor.capture(), eq(httpExchange.getClass().getClassLoader())); assertThat(proxyCaptor.getValue().getOriginalListener(), is(nullValue())); assertThat(proxyCaptor.getValue().getSpanStore(), is(spanStore)); verifyNoMoreInteractions(runtimeLinker); verifyZeroInteractions(object, rsc); }
@Test public void baggageInjection() { String key = "key"; String value = "value"; AsyncClientRequestAdapter<TextMap> adapter = sensor.getAsyncClientRequestAdapter(object, new Object[] { httpExchange }, rsc); adapter.getCarrier().put(key, value); verify(httpExchange).setRequestHeader(key, value); verifyZeroInteractions(object, rsc); }
@Test public void spanShouldNotStart() { when(requestAdapter.startClientSpan()).thenReturn(false); SpanImpl result = interceptor.handleAsyncRequest(requestAdapter); assertThat(result, is(nullValue())); verify(requestAdapter).startClientSpan(); verifyNoMoreInteractions(requestAdapter); verifyZeroInteractions(tracer); }
@Test public void happyPath() { when(requestAdapter.startClientSpan()).thenReturn(true); when(requestAdapter.getTags()).thenReturn(Collections.<String, String> singletonMap(Tags.HTTP_URL.getKey(), "value")); when(requestAdapter.getPropagationType()).thenReturn(PropagationType.HTTP); when(requestAdapter.getReferenceType()).thenReturn(References.FOLLOWS_FROM); SpanImpl result = interceptor.handleAsyncRequest(requestAdapter); assertThat(result, is(span)); verify(tracer).buildSpan(null, References.FOLLOWS_FROM, true); verify(tracer).inject(context, Format.Builtin.TEXT_MAP, carrier); verify(spanBuilder).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT); verify(spanBuilder).withTag(ExtraTags.PROPAGATION_TYPE, PropagationType.HTTP.toString()); verify(spanBuilder).withTag(Tags.HTTP_URL.getKey(), "value"); verify(spanBuilder).build(); verify(span).context(); ArgumentCaptor<SpanStore> spanStoreCaptor = ArgumentCaptor.forClass(SpanStore.class); verify(spanStoreAdapter).setSpanStore(spanStoreCaptor.capture()); assertThat(spanStoreCaptor.getValue().getSpan(), is(span)); verifyNoMoreInteractions(tracer, spanBuilder, span); verifyZeroInteractions(context); }
@Test public void spanStore() { Object listener = new Object(); Proxy proxyListener = new Proxy(); when(httpExchange.getEventListener()).thenReturn(listener); when(runtimeLinker.createProxy(eq(JettyEventListenerProxy.class), Mockito.<JettyEventListenerProxy> any(), Mockito.<ClassLoader> any())).thenReturn(proxyListener); AsyncClientRequestAdapter<TextMap> adapter = sensor.getAsyncClientRequestAdapter(object, new Object[] { httpExchange }, rsc); adapter.getSpanStoreAdapter().setSpanStore(spanStore); verify(httpExchange).setEventListener(proxyListener); ArgumentCaptor<JettyEventListenerProxy> proxyCaptor = ArgumentCaptor.forClass(JettyEventListenerProxy.class); verify(runtimeLinker).createProxy(eq(JettyEventListenerProxy.class), proxyCaptor.capture(), eq(httpExchange.getClass().getClassLoader())); assertThat(proxyCaptor.getValue().getOriginalListener(), is(listener)); assertThat(proxyCaptor.getValue().getSpanStore(), is(spanStore)); verifyNoMoreInteractions(runtimeLinker); verifyZeroInteractions(object, rsc); }
@Test public void spanStarting() { AsyncClientRequestAdapter<TextMap> adapter = sensor.getAsyncClientRequestAdapter(object, new Object[] { httpExchange }, rsc); assertThat(adapter.startClientSpan(), is(true)); }
/** * Handles the {@link ClientRequestAdapter}. This method should be called when new client * request is created. * * @param <C> * type of carrier adapter is providing * @param requestAdapter * {@link ClientRequestAdapter} providing necessary information. * @return Created span */ public <C> SpanImpl handleAsyncRequest(AsyncClientRequestAdapter<C> requestAdapter) { SpanBuilderImpl builder = handleRequestInternal(requestAdapter); // nothing to do if builder was not created if (null == builder) { return null; } // just build the span and store it for later start SpanImpl span = builder.build(); SpanStore spanStore = new SpanStore(); spanStore.storeSpan(span); requestAdapter.getSpanStoreAdapter().setSpanStore(spanStore); // inject here as the context is created when span is started tracer.inject(span.context(), requestAdapter.getFormat(), requestAdapter.getCarrier()); return span; }
@Test public void mustHaveTheProperProperties() { AsyncClientRequestAdapter<TextMap> adapter = sensor.getAsyncClientRequestAdapter(object, new Object[] { null, httpRequest, httpContext, null }, rsc); assertThat("The propagation type must be HTTP.", adapter.getPropagationType(), is(PropagationType.HTTP)); assertThat("The reference type must be FOLLOWS FROM.", adapter.getReferenceType(), is(References.FOLLOWS_FROM)); assertThat("The format must be HTTP HEADER.", adapter.getFormat(), is(Format.Builtin.HTTP_HEADERS)); verifyZeroInteractions(object, rsc); }
@Test public void mustNotHaveAnyTagWhenRequestLineIsNull() { when(httpRequest.getRequestLine()).thenReturn(null); AsyncClientRequestAdapter<TextMap> adapter = sensor.getAsyncClientRequestAdapter(object, new Object[] { null, httpRequest, httpContext, null }, rsc); Map<String, String> tags = adapter.getTags(); assertThat("Tags map size must be 0.", tags.size(), is(0)); verifyZeroInteractions(object, rsc); }
@Test public void mustStartClientProperlyWhenTheRequestDoesNotContainsHeader() { when(httpRequest.containsHeader(anyString())).thenReturn(false); AsyncClientRequestAdapter<TextMap> adapter = sensor.getAsyncClientRequestAdapter(object, new Object[] { null, httpRequest, httpContext, null }, rsc); assertThat("The client must be started.", adapter.startClientSpan(), is(true)); }
@Test public void urlAllNull() { when(httpExchange.getScheme()).thenReturn(null); when(httpExchange.getAddress()).thenReturn(null); when(httpExchange.getRequestURI()).thenReturn(null); AsyncClientRequestAdapter<TextMap> adapter = sensor.getAsyncClientRequestAdapter(object, new Object[] { httpExchange }, rsc); Map<String, String> tags = adapter.getTags(); assertThat(tags.size(), is(0)); verifyZeroInteractions(object, rsc); }
@Test public void mustHaveOneTagWithTheExpectedUri() { String uri = "uri"; when(requestLine.getUri()).thenReturn(uri); AsyncClientRequestAdapter<TextMap> adapter = sensor.getAsyncClientRequestAdapter(object, new Object[] { null, httpRequest, httpContext, null }, rsc); Map<String, String> tags = adapter.getTags(); assertThat("Tags map size must be 1.", tags.size(), is(1)); assertThat("Tags map must have the expected uri.", tags, hasEntry(Tags.HTTP_URL.getKey(), uri)); verifyZeroInteractions(object, rsc); }
@Test public void method() { String method = "get"; when(httpExchange.getMethod()).thenReturn(method); AsyncClientRequestAdapter<TextMap> adapter = sensor.getAsyncClientRequestAdapter(object, new Object[] { httpExchange }, rsc); Map<String, String> tags = adapter.getTags(); assertThat(tags.size(), is(1)); assertThat(tags, hasEntry(Tags.HTTP_METHOD.getKey(), method)); verifyZeroInteractions(object, rsc); }
@Test public void mustHaveOneTagWithTheExpectedMethod() { String methodName = "get"; when(requestLine.getMethod()).thenReturn(methodName); AsyncClientRequestAdapter<TextMap> adapter = sensor.getAsyncClientRequestAdapter(object, new Object[] { null, httpRequest, httpContext, null }, rsc); Map<String, String> tags = adapter.getTags(); assertThat("Tags map size must be 1.", tags.size(), is(1)); assertThat("Tags map must have the expected method name.", tags, hasEntry(Tags.HTTP_METHOD.getKey(), methodName)); verifyZeroInteractions(object, rsc); }
@Test public void urlSchemeNull() { String uri = "/test"; String address = "localhost:8080"; when(httpExchange.getScheme()).thenReturn(null); when(httpExchange.getAddress()).thenReturn(address); when(httpExchange.getRequestURI()).thenReturn(uri); AsyncClientRequestAdapter<TextMap> adapter = sensor.getAsyncClientRequestAdapter(object, new Object[] { httpExchange }, rsc); Map<String, String> tags = adapter.getTags(); assertThat(tags.size(), is(1)); assertThat(tags, hasEntry(Tags.HTTP_URL.getKey(), address + uri)); verifyZeroInteractions(object, rsc); }