@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 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 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 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 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); }
/** * 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; }