/** * Returns the name that should be used for the span surrounding the request. Defaults to whatever {@link * ServerSpanNamingAndTaggingStrategy#getInitialSpanName(Object)} returns, with a fallback * of {@link HttpRequestTracingUtils#getFallbackSpanNameForHttpRequest(String, String)} if the naming strategy * returned null or blank string. * * @param nettyRequest The Netty {@link HttpRequest}. * @param riposteRequestInfo The Riposte {@link RequestInfo} (if this is null, then the tag strategy and adapter * will be ignored, and we'll fallback to basic behavior using the {@code nettyRequest}). * @param namingStrategy The {@link ServerSpanNamingAndTaggingStrategy} being used. * @return The name that should be used for the span surrounding the request. */ protected @NotNull String getSpanName( @NotNull HttpRequest nettyRequest, @Nullable RequestInfo<?> riposteRequestInfo, @NotNull ServerSpanNamingAndTaggingStrategy<Span> namingStrategy ) { // Immediately go to the fallback span name if the Riposte RequestInfo is null. if (riposteRequestInfo == null) { return getFallbackSpanName(nettyRequest); } // We have a Riposte RequestInfo. Try the naming strategy first. String spanNameFromStrategy = namingStrategy.getInitialSpanName(riposteRequestInfo); if (StringUtils.isNotBlank(spanNameFromStrategy)) { return spanNameFromStrategy; } // The naming strategy didn't have anything for us. Fall back to something reasonable. return getFallbackSpanName(nettyRequest); }
/** * Returns the name that should be used for the span surrounding the request. Defaults to whatever {@link * ServerSpanNamingAndTaggingStrategy#getInitialSpanName(Object)} returns, with a fallback * of {@link HttpRequestTracingUtils#getFallbackSpanNameForHttpRequest(String, String)} if the naming strategy * returned null or blank string. * * @param nettyRequest The Netty {@link HttpRequest}. * @param riposteRequestInfo The Riposte {@link RequestInfo} (if this is null, then the tag strategy and adapter * will be ignored, and we'll fallback to basic behavior using the {@code nettyRequest}). * @param namingStrategy The {@link ServerSpanNamingAndTaggingStrategy} being used. * @return The name that should be used for the span surrounding the request. */ protected @NotNull String getSpanName( @NotNull HttpRequest nettyRequest, @Nullable RequestInfo<?> riposteRequestInfo, @NotNull ServerSpanNamingAndTaggingStrategy<Span> namingStrategy ) { // Immediately go to the fallback span name if the Riposte RequestInfo is null. if (riposteRequestInfo == null) { return getFallbackSpanName(nettyRequest); } // We have a Riposte RequestInfo. Try the naming strategy first. String spanNameFromStrategy = namingStrategy.getInitialSpanName(riposteRequestInfo); if (StringUtils.isNotBlank(spanNameFromStrategy)) { return spanNameFromStrategy; } // The naming strategy didn't have anything for us. Fall back to something reasonable. return getFallbackSpanName(nettyRequest); }
@Test public void getFallbackSpanName_returns_UNKNOWN_HTTP_METHOD_if_unexpected_exception_occurs() { // given HttpRequest nettyHttpRequestMock = mock(HttpRequest.class); doThrow(new RuntimeException("intentional exception")).when(nettyHttpRequestMock).method(); // when String result = handler.getFallbackSpanName(nettyHttpRequestMock); // then Assertions.assertThat(result).isEqualTo("UNKNOWN_HTTP_METHOD"); verify(nettyHttpRequestMock).method(); } }
@UseDataProvider("getSpanNameScenarioDataProvider") @Test public void getSpanName_works_as_expected(GetSpanNameScenario scenario) { // given DTraceStartHandler handlerSpy = spy(handler); initialSpanNameFromStrategy.set(scenario.strategySpanName); doReturn(scenario.fallbackSpanName).when(handlerSpy).getFallbackSpanName(any()); if (scenario.riposteRequestInfoIsNull) { requestInfoMock = null; } String expectedResult = (scenario.expectStrategyResult) ? scenario.strategySpanName : scenario.fallbackSpanName; // when String result = handlerSpy.getSpanName( httpRequest, requestInfoMock, distributedTracingConfig.getServerSpanNamingAndTaggingStrategy() ); // then Assertions.assertThat(result).isEqualTo(expectedResult); if (!scenario.riposteRequestInfoIsNull) { strategyInitialSpanNameArgs.get().verifyArgs(requestInfoMock, tagAndNamingAdapterMock); } if (!scenario.expectStrategyResult) { verify(handlerSpy).getFallbackSpanName(httpRequest); } }
@DataProvider(value = { "foo | foo", "null | UNKNOWN_HTTP_METHOD", " | UNKNOWN_HTTP_METHOD", "[whitespace] | UNKNOWN_HTTP_METHOD", }, splitBy = "\\|") @Test public void getFallbackSpanName_works_as_expected(String httpMethodStr, String expectedResult) { // given if ("[whitespace]".equals(httpMethodStr)) { httpMethodStr = " \r\n\t "; } HttpMethod httpMethodMock = (httpMethodStr == null) ? null : mock(HttpMethod.class); HttpRequest nettyHttpRequestMock = mock(HttpRequest.class); doReturn(httpMethodMock).when(nettyHttpRequestMock).method(); if (httpMethodMock != null) { doReturn(httpMethodStr).when(httpMethodMock).name(); } // when String result = handler.getFallbackSpanName(nettyHttpRequestMock); // then Assertions.assertThat(result).isEqualTo(expectedResult); }
newSpan = tracer.startRequestWithChildSpan( parentSpan, getFallbackSpanName(nettyRequest) ); getFallbackSpanName(nettyRequest), HttpRequestTracingUtils.getUserIdFromRequestWithHeaders(requestWrapper, userIdHeaderKeys) );
newSpan = tracer.startRequestWithChildSpan( parentSpan, getFallbackSpanName(nettyRequest) ); getFallbackSpanName(nettyRequest), HttpRequestTracingUtils.getUserIdFromRequestWithHeaders(requestWrapper, userIdHeaderKeys) );
doReturn(fallbackSpanName).when(handlerSpy).getFallbackSpanName(any());