/** * Creates a new instance that uses {@link ZipkinHttpTagStrategy#getDefaultInstance()} and {@link * RiposteWingtipsServerTagAdapter#getDefaultInstance()} to do the work of span naming and tagging. */ public DefaultRiposteServerSpanNamingAndTaggingStrategy() { this(ZipkinHttpTagStrategy.getDefaultInstance(), RiposteWingtipsServerTagAdapter.getDefaultInstance()); }
/** * Since this class represents server requests/responses (not clients), we only want to consider HTTP status codes * greater than or equal to 500 to be an error. From a server's perspective, a 4xx response is the correct * response to a bad request, and should therefore not be considered an error (again, from the server's * perspective - the client may feel differently). * * @param response The response object. * @return The value of {@link #getResponseHttpStatus(ResponseInfo)} if it is greater than or equal to 500, * or null otherwise. */ @Override public @Nullable String getErrorResponseTagValue(@Nullable ResponseInfo<?> response) { Integer statusCode = getResponseHttpStatus(response); if (statusCode != null && statusCode >= 500) { return statusCode.toString(); } // Status code does not indicate an error, so return null. return null; }
@Test public void getHeaderSingleValue_works_as_expected() { // given String headerKey = UUID.randomUUID().toString(); String expectedResult = UUID.randomUUID().toString(); doReturn(expectedResult).when(requestHeadersMock).get(anyString()); // when String result = adapterSpy.getHeaderSingleValue(requestMock, headerKey); // then assertThat(result).isEqualTo(expectedResult); verify(requestHeadersMock).get(headerKey); }
@DataProvider(value = { "null | null", "200 | null", "300 | null", "400 | null", "499 | null", "500 | 500", "599 | 599", "999 | 999" }, splitBy = "\\|") @Test public void getErrorResponseTagValue_works_as_expected(Integer statusCode, String expectedTagValue) { // given doReturn(statusCode).when(adapterSpy).getResponseHttpStatus(any(ResponseInfo.class)); // when String result = adapterSpy.getErrorResponseTagValue(responseMock); // then assertThat(result).isEqualTo(expectedTagValue); verify(adapterSpy).getErrorResponseTagValue(responseMock); verify(adapterSpy).getResponseHttpStatus(responseMock); verifyNoMoreInteractions(adapterSpy); }
@Test public void getRequestHttpMethod_returns_null_if_passed_null() { // expect assertThat(adapterSpy.getRequestHttpMethod(null)).isNull(); }
@Test public void getRequestUriPathTemplate_returns_null_when_passed_null_request() { // when String result = adapterSpy.getRequestUriPathTemplate(null, responseMock); // then assertThat(result).isNull(); }
@Test public void getRequestPath_works_as_expected() { // given String expectedResult = UUID.randomUUID().toString(); doReturn(expectedResult).when(requestMock).getPath(); // when String result = adapterSpy.getRequestPath(requestMock); // then assertThat(result).isEqualTo(expectedResult); }
@Test public void getHeaderMultipleValue_works_as_expected() { // given String headerKey = UUID.randomUUID().toString(); List<String> expectedResult = Arrays.asList(UUID.randomUUID().toString(), UUID.randomUUID().toString()); doReturn(expectedResult).when(requestHeadersMock).getAll(anyString()); // when List<String> result = adapterSpy.getHeaderMultipleValue(requestMock, headerKey); // then assertThat(result).isEqualTo(expectedResult); verify(requestHeadersMock).getAll(headerKey); }
@Before public void setup() { adapterSpy = spy(new RiposteWingtipsServerTagAdapter()); requestMock = mock(RequestInfo.class); responseMock = mock(ResponseInfo.class); requestHeadersMock = mock(HttpHeaders.class); doReturn(requestHeadersMock).when(requestMock).getHeaders(); }
@Test public void getRequestHttpMethod_works_as_expected() { // given HttpMethod expectedResult = HttpMethod.valueOf(UUID.randomUUID().toString()); doReturn(expectedResult).when(requestMock).getMethod(); // when String result = adapterSpy.getRequestHttpMethod(requestMock); // then assertThat(result).isEqualTo(expectedResult.name()); }
@DataProvider(value = { "/some/path/{tmplt} | /some/path/{tmplt}", "null | null", " | null", "[whitespace] | null", }, splitBy = "\\|") @Test public void getRequestUriPathTemplate_works_as_expected( String requestPathTemplate, String expectedResult ) { // given if ("[whitespace]".equals(requestPathTemplate)) { requestPathTemplate = " \t\r\n "; } doReturn(requestPathTemplate).when(requestMock).getPathTemplate(); // when String result = adapterSpy.getRequestUriPathTemplate(requestMock, responseMock); // then assertThat(result).isEqualTo(expectedResult); }
@Test public void getRequestPath_returns_null_if_passed_null() { // expect assertThat(adapterSpy.getRequestPath(null)).isNull(); }
@Test public void getHeaderMultipleValue_returns_null_if_passed_null_request() { // expect assertThat(adapterSpy.getHeaderMultipleValue(null, "foo")).isNull(); }
/** * Creates a new instance that uses {@link ZipkinHttpTagStrategy#getDefaultInstance()} and {@link * RiposteWingtipsServerTagAdapter#getDefaultInstance()} to do the work of span naming and tagging. */ public DefaultRiposteServerSpanNamingAndTaggingStrategy() { this(ZipkinHttpTagStrategy.getDefaultInstance(), RiposteWingtipsServerTagAdapter.getDefaultInstance()); }
/** * Since this class represents server requests/responses (not clients), we only want to consider HTTP status codes * greater than or equal to 500 to be an error. From a server's perspective, a 4xx response is the correct * response to a bad request, and should therefore not be considered an error (again, from the server's * perspective - the client may feel differently). * * @param response The response object. * @return The value of {@link #getResponseHttpStatus(ResponseInfo)} if it is greater than or equal to 500, * or null otherwise. */ @Override public @Nullable String getErrorResponseTagValue(@Nullable ResponseInfo<?> response) { Integer statusCode = getResponseHttpStatus(response); if (statusCode != null && statusCode >= 500) { return statusCode.toString(); } // Status code does not indicate an error, so return null. return null; }
@Test public void getHeaderSingleValue_returns_null_if_passed_null_request() { // expect assertThat(adapterSpy.getHeaderSingleValue(null, "foo")).isNull(); }
@Test public void getDefaultInstance_returns_DEFAULT_INSTANCE() { // expect assertThat(RiposteWingtipsServerTagAdapter.getDefaultInstance()) .isSameAs(RiposteWingtipsServerTagAdapter.DEFAULT_INSTANCE); }
@Test public void getResponseHttpStatus_works_as_expected() { // given Integer expectedResult = 42; doReturn(expectedResult).when(responseMock).getHttpStatusCode(); // when Integer result = adapterSpy.getResponseHttpStatus(responseMock); // then assertThat(result).isEqualTo(expectedResult); }
@Test public void getDefaultInstance_returns_DEFAULT_INSTANCE() { // when DefaultRiposteServerSpanNamingAndTaggingStrategy instance = DefaultRiposteServerSpanNamingAndTaggingStrategy.getDefaultInstance(); // then assertThat(instance) .isSameAs(DefaultRiposteServerSpanNamingAndTaggingStrategy.DEFAULT_INSTANCE); assertThat(instance.tagAndNamingStrategy).isSameAs(ZipkinHttpTagStrategy.getDefaultInstance()); assertThat(instance.tagAndNamingAdapter).isSameAs(RiposteWingtipsServerTagAdapter.getDefaultInstance()); }
@Test public void getResponseHttpStatus_returns_null_if_passed_null() { // expect assertThat(adapterSpy.getResponseHttpStatus(null)).isNull(); }