HttpRequestTracingUtils { @SuppressWarnings("ConstantConditions") public static @NotNull <REQ, RES> String generateSafeSpanName( @Nullable REQ request, @Nullable RES response, @NotNull HttpTagAndSpanNamingAdapter<REQ, RES> adapter ) { String httpMethod = (adapter == null) ? null : adapter.getRequestHttpMethod(request); String pathTemplate = (adapter == null) ? null : adapter.getRequestUriPathTemplate(request, response); Integer responseStatusCode = (adapter == null) ? null : adapter.getResponseHttpStatus(response); return generateSafeSpanName(httpMethod, pathTemplate, responseStatusCode); } private  HttpRequestTracingUtils(); static Span fromRequestWithHeaders(RequestWithHeaders request, List<String> userIdHeaderKeys); static boolean hasInvalidSpanIdBecauseCallerDidNotSendOne(Span span); static boolean hasInvalidParentIdBecauseCallerDidNotSendSpanId(Span span); static String getUserIdFromRequestWithHeaders(RequestWithHeaders request, List<String> userIdHeaderKeys); static void propagateTracingHeaders(HttpObjectForPropagation httpObjectForPropagation, Span span); static String convertSampleableBooleanToExpectedB3Value(boolean sampleable); @SuppressWarnings("ConstantConditions") static @NotNull String generateSafeSpanName(
        @Nullable REQ request,
        @Nullable RES response,
        @NotNull HttpTagAndSpanNamingAdapter<REQ, RES> adapter
    ); static @NotNull String generateSafeSpanName(
        @Nullable String requestHttpMethod,
        @Nullable String pathTemplate,
        @Nullable Integer responseStatusCode
    ); static @NotNull String getFallbackSpanNameForHttpRequest(
        @Nullable String prefix,
        @Nullable String httpMethod
    ); static final String UNSPECIFIED_SPAN_NAME; static final String SPAN_FROM_HEADERS_WHERE_CALLER_DID_NOT_SEND_SPAN_ID_TAG_KEY; static final String CHILD_OF_SPAN_FROM_HEADERS_WHERE_CALLER_DID_NOT_SEND_SPAN_ID_TAG_KEY; }
@DataProvider(value = { "someHttpMethod | /some/path/tmplt | null | someHttpMethod /some/path/tmplt", "someHttpMethod | /some/path/tmplt | 299 | someHttpMethod /some/path/tmplt", "someHttpMethod | /some/path/tmplt | 300 | someHttpMethod redirected", "someHttpMethod | /some/path/tmplt | 399 | someHttpMethod redirected", "someHttpMethod | /some/path/tmplt | 400 | someHttpMethod /some/path/tmplt", "someHttpMethod | /some/path/tmplt | 404 | someHttpMethod not_found", "someHttpMethod | /some/path/tmplt | 500 | someHttpMethod /some/path/tmplt", "someHttpMethod | null | null | someHttpMethod", "someHttpMethod | null | 300 | someHttpMethod redirected", "someHttpMethod | null | 404 | someHttpMethod not_found", "someHttpMethod | | null | someHttpMethod", "someHttpMethod | | 300 | someHttpMethod redirected", "someHttpMethod | | 404 | someHttpMethod not_found", "someHttpMethod | [whitespace] | null | someHttpMethod", "someHttpMethod | [whitespace] | 300 | someHttpMethod redirected", "someHttpMethod | [whitespace] | 404 | someHttpMethod not_found", "null | /some/path/tmplt | null | UNKNOWN_HTTP_METHOD /some/path/tmplt", "null | /some/path/tmplt | 300 | UNKNOWN_HTTP_METHOD redirected", "null | /some/path/tmplt | 404 | UNKNOWN_HTTP_METHOD not_found", " | /some/path/tmplt | null | UNKNOWN_HTTP_METHOD /some/path/tmplt", " | /some/path/tmplt | 300 | UNKNOWN_HTTP_METHOD redirected", " | /some/path/tmplt | 404 | UNKNOWN_HTTP_METHOD not_found", "[whitespace] | /some/path/tmplt | null | UNKNOWN_HTTP_METHOD /some/path/tmplt", "[whitespace] | /some/path/tmplt | 300 | UNKNOWN_HTTP_METHOD redirected", "[whitespace] | /some/path/tmplt | 404 | UNKNOWN_HTTP_METHOD not_found", "null | null | null | UNKNOWN_HTTP_METHOD", "null | null | 300 | UNKNOWN_HTTP_METHOD redirected", "null | null | 404 | UNKNOWN_HTTP_METHOD not_found", " | | null | UNKNOWN_HTTP_METHOD", " | | 300 | UNKNOWN_HTTP_METHOD redirected", " | | 404 | UNKNOWN_HTTP_METHOD not_found", "[whitespace] | [whitespace] | null | UNKNOWN_HTTP_METHOD", "[whitespace] | [whitespace] | 300 | UNKNOWN_HTTP_METHOD redirected", "[whitespace] | [whitespace] | 404 | UNKNOWN_HTTP_METHOD not_found" }, splitBy = "\\|") @Test public void generateSafeSpanName_works_as_expected( String httpMethod, String pathTemplate, Integer statusCode, String expectedResult ) { if ("[whitespace]".equals(httpMethod)) { httpMethod = " \r\n\t "; } if ("[whitespace]".equals(pathTemplate)) { pathTemplate = " \r\n\t "; } Object requestMock = mock(Object.class); Object responseMock = mock(Object.class); HttpTagAndSpanNamingAdapter<Object, Object> adapterMock = mock(HttpTagAndSpanNamingAdapter.class); doReturn(httpMethod).when(adapterMock).getRequestHttpMethod(anyObject()); doReturn(pathTemplate).when(adapterMock).getRequestUriPathTemplate(anyObject(), anyObject()); doReturn(statusCode).when(adapterMock).getResponseHttpStatus(anyObject()); String adapterArgsResult = HttpRequestTracingUtils.generateSafeSpanName(requestMock, responseMock, adapterMock); String directArgsResult = HttpRequestTracingUtils.generateSafeSpanName(httpMethod, pathTemplate, statusCode); assertThat(adapterArgsResult).isEqualTo(directArgsResult); assertThat(adapterArgsResult).isEqualTo(expectedResult); assertThat(directArgsResult).isEqualTo(expectedResult); verify(adapterMock).getRequestHttpMethod(requestMock); verify(adapterMock).getRequestUriPathTemplate(requestMock, responseMock); verify(adapterMock).getResponseHttpStatus(responseMock); verifyNoMoreInteractions(adapterMock); verifyZeroInteractions(requestMock, responseMock); }
@Test public void generateSafeSpanName_adapter_args_works_as_expected_if_passed_null_adapter() { Object requestMock = mock(Object.class); Object responseMock = mock(Object.class); String result = HttpRequestTracingUtils.generateSafeSpanName(requestMock, responseMock, null); assertThat(result).isEqualTo("UNKNOWN_HTTP_METHOD"); verifyZeroInteractions(requestMock, responseMock); }