/** * @param surroundCallsWithSubspan Pass in true to have the returned {@link RestTemplate} surround all calls with * a subspan and propagate the subspan's tracing info, or false to have only the current span propagated at the * time of the call (no subspan). * @return A new {@link RestTemplate} instance with a {@link WingtipsClientHttpRequestInterceptor} already added * and with the subspan option on or off depending on the value of the {@code surroundCallsWithSubspan} argument, * and using the default {@link HttpTagAndSpanNamingStrategy} and {@link HttpTagAndSpanNamingAdapter} * ({@link ZipkinHttpTagStrategy} and {@link SpringHttpClientTagAdapter}). */ public static RestTemplate createTracingEnabledRestTemplate(boolean surroundCallsWithSubspan) { RestTemplate restTemplate = new RestTemplate(); restTemplate.getInterceptors().add( new WingtipsClientHttpRequestInterceptor(surroundCallsWithSubspan) ); return restTemplate; }
/** * @param surroundCallsWithSubspan Pass in true to have the returned {@link RestTemplate} surround all calls with * a subspan and propagate the subspan's tracing info, or false to have only the current span propagated at the * time of the call (no subspan). * @return A new {@link RestTemplate} instance with a {@link WingtipsClientHttpRequestInterceptor} already added * and with the subspan option on or off depending on the value of the {@code surroundCallsWithSubspan} argument, * and using the default {@link HttpTagAndSpanNamingStrategy} and {@link HttpTagAndSpanNamingAdapter} * ({@link ZipkinHttpTagStrategy} and {@link SpringHttpClientTagAdapter}). */ public static RestTemplate createTracingEnabledRestTemplate(boolean surroundCallsWithSubspan) { RestTemplate restTemplate = new RestTemplate(); restTemplate.getInterceptors().add( new WingtipsClientHttpRequestInterceptor(surroundCallsWithSubspan) ); return restTemplate; }
/** * @param tagAndNamingStrategy The span tag and naming strategy to use - cannot be null. If you really want no * tag and naming strategy, then pass in {@link NoOpHttpTagStrategy#getDefaultInstance()}. * @param tagAndNamingAdapter The tag and naming adapter to use - cannot be null. If you really want no tag and * naming adapter, then pass in {@link NoOpHttpTagAdapter#getDefaultInstance()}. * @return A new {@link RestTemplate} instance with a {@link WingtipsClientHttpRequestInterceptor} * already added, and with the subspan option and tag/naming strategy and adapter set to the given arguments. */ public static RestTemplate createTracingEnabledRestTemplate( boolean surroundCallsWithSubspan, HttpTagAndSpanNamingStrategy<HttpRequest, ClientHttpResponse> tagAndNamingStrategy, HttpTagAndSpanNamingAdapter<HttpRequest, ClientHttpResponse> tagAndNamingAdapter ) { RestTemplate restTemplate = new RestTemplate(); restTemplate.getInterceptors().add( new WingtipsClientHttpRequestInterceptor( surroundCallsWithSubspan, tagAndNamingStrategy, tagAndNamingAdapter ) ); return restTemplate; }
/** * @param tagAndNamingStrategy The span tag and naming strategy to use - cannot be null. If you really want no * tag and naming strategy, then pass in {@link NoOpHttpTagStrategy#getDefaultInstance()}. * @param tagAndNamingAdapter The tag and naming adapter to use - cannot be null. If you really want no tag and * naming adapter, then pass in {@link NoOpHttpTagAdapter#getDefaultInstance()}. * @return A new {@link RestTemplate} instance with a {@link WingtipsClientHttpRequestInterceptor} * already added, and with the subspan option and tag/naming strategy and adapter set to the given arguments. */ public static RestTemplate createTracingEnabledRestTemplate( boolean surroundCallsWithSubspan, HttpTagAndSpanNamingStrategy<HttpRequest, ClientHttpResponse> tagAndNamingStrategy, HttpTagAndSpanNamingAdapter<HttpRequest, ClientHttpResponse> tagAndNamingAdapter ) { RestTemplate restTemplate = new RestTemplate(); restTemplate.getInterceptors().add( new WingtipsClientHttpRequestInterceptor( surroundCallsWithSubspan, tagAndNamingStrategy, tagAndNamingAdapter ) ); return restTemplate; }
@DataProvider(value = { "NULL_STRATEGY_ARG", "NULL_ADAPTER_ARG" }) @Test public void constructor_with_tag_and_span_naming_args_throws_IllegalArgumentException_if_passed_null_args( NullConstructorArgsScenario scenario ) { // when Throwable ex = catchThrowable( () -> new WingtipsClientHttpRequestInterceptor(true, scenario.strategy, scenario.adapter) ); // then assertThat(ex) .isInstanceOf(IllegalArgumentException.class) .hasMessage(scenario.expectedExceptionMessage); }
@DataProvider(value = { "true", "false" }) @Test public void constructor_with_tag_and_span_naming_args_sets_fields_as_expected(boolean subspanOptionOn) { // when WingtipsClientHttpRequestInterceptor interceptor = new WingtipsClientHttpRequestInterceptor( subspanOptionOn, tagAndNamingStrategy, tagAndNamingAdapterMock ); // then assertThat(interceptor.surroundCallsWithSubspan).isEqualTo(subspanOptionOn); assertThat(interceptor.tagAndNamingStrategy).isSameAs(tagAndNamingStrategy); assertThat(interceptor.tagAndNamingAdapter).isSameAs(tagAndNamingAdapterMock); }
@Test public void default_constructor_creates_instance_with_subspan_option_on() { // when WingtipsClientHttpRequestInterceptor interceptor = new WingtipsClientHttpRequestInterceptor(); // then assertThat(interceptor.surroundCallsWithSubspan).isTrue(); assertThat(interceptor.tagAndNamingStrategy).isSameAs(ZipkinHttpTagStrategy.getDefaultInstance()); assertThat(interceptor.tagAndNamingAdapter).isSameAs(SpringHttpClientTagAdapter.getDefaultInstance()); }
@DataProvider(value = { "spanNameFromStrategy | PATCH | spanNameFromStrategy", "null | PATCH | resttemplate_downstream_call-PATCH", " | PATCH | resttemplate_downstream_call-PATCH", "[whitespace] | PATCH | resttemplate_downstream_call-PATCH", "null | null | resttemplate_downstream_call-UNKNOWN_HTTP_METHOD", }, splitBy = "\\|") @Test public void getSubspanSpanName_works_as_expected( String strategyResult, HttpMethod httpMethod, String expectedResult ) { // given if ("[whitespace]".equals(strategyResult)) { strategyResult = " \n\r\t "; } initialSpanNameFromStrategy.set(strategyResult); doReturn(httpMethod).when(requestMock).getMethod(); WingtipsClientHttpRequestInterceptor interceptor = new WingtipsClientHttpRequestInterceptor( true, tagAndNamingStrategy, tagAndNamingAdapterMock ); // when String result = interceptor.getSubspanSpanName(requestMock, tagAndNamingStrategy, tagAndNamingAdapterMock); // then assertThat(result).isEqualTo(expectedResult); }
@DataProvider(value = { "true", "false" }) @Test public void single_arg_constructor_creates_instance_with_subspan_option_set_to_desired_value( boolean subspanOptionOn ) { // when WingtipsClientHttpRequestInterceptor interceptor = new WingtipsClientHttpRequestInterceptor(subspanOptionOn); // then assertThat(interceptor.surroundCallsWithSubspan).isEqualTo(subspanOptionOn); assertThat(interceptor.tagAndNamingStrategy).isSameAs(ZipkinHttpTagStrategy.getDefaultInstance()); assertThat(interceptor.tagAndNamingAdapter).isSameAs(SpringHttpClientTagAdapter.getDefaultInstance()); }
) throws IOException { WingtipsClientHttpRequestInterceptor interceptor = new WingtipsClientHttpRequestInterceptor( subspanOptionOn, tagAndNamingStrategy, tagAndNamingAdapterMock );