public DTraceStartHandler( List<String> userIdHeaderKeys, @NotNull DistributedTracingConfig<Span> distributedTracingConfig ) { if (distributedTracingConfig == null) { throw new IllegalArgumentException("distributedTracingConfig cannot be null"); } this.userIdHeaderKeys = userIdHeaderKeys; this.spanNamingAndTaggingStrategy = distributedTracingConfig.getServerSpanNamingAndTaggingStrategy(); }
public DTraceStartHandler( List<String> userIdHeaderKeys, @NotNull DistributedTracingConfig<Span> distributedTracingConfig ) { if (distributedTracingConfig == null) { throw new IllegalArgumentException("distributedTracingConfig cannot be null"); } this.userIdHeaderKeys = userIdHeaderKeys; this.spanNamingAndTaggingStrategy = distributedTracingConfig.getServerSpanNamingAndTaggingStrategy(); }
@SuppressWarnings("ConstantConditions") public NonblockingEndpointExecutionHandler( @NotNull Executor longRunningTaskExecutor, long defaultCompletableFutureTimeoutMillis, @NotNull DistributedTracingConfig<Span> distributedTracingConfig ) { if (longRunningTaskExecutor == null) { throw new IllegalArgumentException("longRunningTaskExecutor cannot be null"); } if (distributedTracingConfig == null) { throw new IllegalArgumentException("distributedTracingConfig cannot be null"); } this.longRunningTaskExecutor = longRunningTaskExecutor; this.defaultCompletableFutureTimeoutMillis = defaultCompletableFutureTimeoutMillis; this.spanTaggingStrategy = distributedTracingConfig.getServerSpanNamingAndTaggingStrategy(); }
@SuppressWarnings("ConstantConditions") public NonblockingEndpointExecutionHandler( @NotNull Executor longRunningTaskExecutor, long defaultCompletableFutureTimeoutMillis, @NotNull DistributedTracingConfig<Span> distributedTracingConfig ) { if (longRunningTaskExecutor == null) { throw new IllegalArgumentException("longRunningTaskExecutor cannot be null"); } if (distributedTracingConfig == null) { throw new IllegalArgumentException("distributedTracingConfig cannot be null"); } this.longRunningTaskExecutor = longRunningTaskExecutor; this.defaultCompletableFutureTimeoutMillis = defaultCompletableFutureTimeoutMillis; this.spanTaggingStrategy = distributedTracingConfig.getServerSpanNamingAndTaggingStrategy(); }
@SuppressWarnings("ConstantConditions") public ExceptionHandlingHandler( @NotNull RiposteErrorHandler riposteErrorHandler, @NotNull RiposteUnhandledErrorHandler riposteUnhandledErrorHandler, @NotNull DistributedTracingConfig<Span> distributedTracingConfig ) { if (riposteErrorHandler == null) { throw new IllegalArgumentException("riposteErrorHandler cannot be null"); } if (riposteUnhandledErrorHandler == null) { throw new IllegalArgumentException("riposteUnhandledErrorHandler cannot be null"); } if (distributedTracingConfig == null) { throw new IllegalArgumentException("distributedTracingConfig cannot be null"); } this.riposteErrorHandler = riposteErrorHandler; this.riposteUnhandledErrorHandler = riposteUnhandledErrorHandler; this.spanNamingAndTaggingStrategy = distributedTracingConfig.getServerSpanNamingAndTaggingStrategy(); }
@SuppressWarnings("ConstantConditions") public ExceptionHandlingHandler( @NotNull RiposteErrorHandler riposteErrorHandler, @NotNull RiposteUnhandledErrorHandler riposteUnhandledErrorHandler, @NotNull DistributedTracingConfig<Span> distributedTracingConfig ) { if (riposteErrorHandler == null) { throw new IllegalArgumentException("riposteErrorHandler cannot be null"); } if (riposteUnhandledErrorHandler == null) { throw new IllegalArgumentException("riposteUnhandledErrorHandler cannot be null"); } if (distributedTracingConfig == null) { throw new IllegalArgumentException("distributedTracingConfig cannot be null"); } this.riposteErrorHandler = riposteErrorHandler; this.riposteUnhandledErrorHandler = riposteUnhandledErrorHandler; this.spanNamingAndTaggingStrategy = distributedTracingConfig.getServerSpanNamingAndTaggingStrategy(); }
public ResponseSender( @Nullable ObjectMapper defaultResponseContentSerializer, @Nullable ErrorResponseBodySerializer errorResponseBodySerializer, @NotNull DistributedTracingConfig<Span> distributedTracingConfig ) { if (defaultResponseContentSerializer == null) { logger.info("No defaultResponseContentSerializer specified - using a new no-arg ObjectMapper as the " + "default response serializer"); defaultResponseContentSerializer = new ObjectMapper(); } if (errorResponseBodySerializer == null) { logger.info("No errorResponseBodySerializer specified - using " + "ErrorContractSerializerHelper.SMART_ERROR_SERIALIZER as the default response serializer"); errorResponseBodySerializer = ErrorContractSerializerHelper.SMART_ERROR_SERIALIZER; } if (distributedTracingConfig == null) { throw new IllegalArgumentException("distributedTracingConfig cannot be null"); } this.defaultResponseContentSerializer = defaultResponseContentSerializer; this.errorResponseBodySerializer = errorResponseBodySerializer; this.spanNamingAndTaggingStrategy = distributedTracingConfig.getServerSpanNamingAndTaggingStrategy(); }
public ResponseSender( @Nullable ObjectMapper defaultResponseContentSerializer, @Nullable ErrorResponseBodySerializer errorResponseBodySerializer, @NotNull DistributedTracingConfig<Span> distributedTracingConfig ) { if (defaultResponseContentSerializer == null) { logger.info("No defaultResponseContentSerializer specified - using a new no-arg ObjectMapper as the " + "default response serializer"); defaultResponseContentSerializer = new ObjectMapper(); } if (errorResponseBodySerializer == null) { logger.info("No errorResponseBodySerializer specified - using " + "ErrorContractSerializerHelper.SMART_ERROR_SERIALIZER as the default response serializer"); errorResponseBodySerializer = ErrorContractSerializerHelper.SMART_ERROR_SERIALIZER; } if (distributedTracingConfig == null) { throw new IllegalArgumentException("distributedTracingConfig cannot be null"); } this.defaultResponseContentSerializer = defaultResponseContentSerializer; this.errorResponseBodySerializer = errorResponseBodySerializer; this.spanNamingAndTaggingStrategy = distributedTracingConfig.getServerSpanNamingAndTaggingStrategy(); }
@Before public void beforeMethod() { riposteErrorHandlerMock = mock(RiposteErrorHandler.class); riposteUnhandledErrorHandlerMock = mock(RiposteUnhandledErrorHandler.class); distributedTracingConfigMock = mock(DistributedTracingConfig.class); serverSpanNamingAndTaggingStrategyMock = mock(ServerSpanNamingAndTaggingStrategy.class); doReturn(serverSpanNamingAndTaggingStrategyMock).when(distributedTracingConfigMock).getServerSpanNamingAndTaggingStrategy(); handler = new ExceptionHandlingHandler(riposteErrorHandlerMock, riposteUnhandledErrorHandlerMock, distributedTracingConfigMock ); channelMock = mock(Channel.class); ctxMock = mock(ChannelHandlerContext.class); stateAttributeMock = mock(Attribute.class); proxyRouterStateAttributeMock = mock(Attribute.class); state = new HttpProcessingState(); proxyRouterStateSpy = spy(new ProxyRouterProcessingState()); doReturn(channelMock).when(ctxMock).channel(); doReturn(stateAttributeMock).when(channelMock).attr(ChannelAttributes.HTTP_PROCESSING_STATE_ATTRIBUTE_KEY); doReturn(state).when(stateAttributeMock).get(); doReturn(proxyRouterStateAttributeMock).when(channelMock) .attr(ChannelAttributes.PROXY_ROUTER_PROCESSING_STATE_ATTRIBUTE_KEY); doReturn(proxyRouterStateSpy).when(proxyRouterStateAttributeMock).get(); }
@Before public void beforeMethod() { stateSpy = spy(new HttpProcessingState()); initialSpanNameFromStrategy = new AtomicReference<>("span-name-from-strategy-" + UUID.randomUUID().toString()); strategyInitialSpanNameMethodCalled = new AtomicBoolean(false); strategyRequestTaggingMethodCalled = new AtomicBoolean(false); strategyResponseTaggingAndFinalSpanNameMethodCalled = new AtomicBoolean(false); strategyInitialSpanNameArgs = new AtomicReference<>(null); strategyRequestTaggingArgs = new AtomicReference<>(null); strategyResponseTaggingArgs = new AtomicReference<>(null); wingtipsStrategy = new ArgCapturingHttpTagAndSpanNamingStrategy<>( initialSpanNameFromStrategy, strategyInitialSpanNameMethodCalled, strategyRequestTaggingMethodCalled, strategyResponseTaggingAndFinalSpanNameMethodCalled, strategyInitialSpanNameArgs, strategyRequestTaggingArgs, strategyResponseTaggingArgs ); wingtipsAdapterMock = mock(HttpTagAndSpanNamingAdapter.class); serverTaggingStrategy = new DefaultRiposteServerSpanNamingAndTaggingStrategy(wingtipsStrategy, wingtipsAdapterMock); requestMock = mock(RequestInfo.class); responseMock = mock(ResponseInfo.class); errorMock = mock(Throwable.class); spanMock = mock(Span.class); distributedTracingConfigMock = mock(DistributedTracingConfig.class); doReturn(serverTaggingStrategy).when(distributedTracingConfigMock).getServerSpanNamingAndTaggingStrategy(); }
public void handleTracingResponseTaggingAndFinalSpanNameIfNotAlreadyDone() { if (tracingResponseTaggingAndFinalSpanNameCompleted || distributedTracingConfig == null) { return; } tracingResponseTaggingAndFinalSpanNameCompleted = true; try { Span overallRequestSpan = getOverallRequestSpan(); if (overallRequestSpan != null) { distributedTracingConfig.getServerSpanNamingAndTaggingStrategy().handleResponseTaggingAndFinalSpanName( overallRequestSpan, getRequestInfo(), getResponseInfo(), getErrorThatTriggeredThisResponse() ); } } catch (Throwable t) { logger.error( "Unexpected error occurred while trying to set final span name and response tagging. This " + "exception will be ignored, but should be investigated - it should not happen.", t ); } }
public void handleTracingResponseTaggingAndFinalSpanNameIfNotAlreadyDone() { if (tracingResponseTaggingAndFinalSpanNameCompleted || distributedTracingConfig == null) { return; } tracingResponseTaggingAndFinalSpanNameCompleted = true; try { Span overallRequestSpan = getOverallRequestSpan(); if (overallRequestSpan != null) { distributedTracingConfig.getServerSpanNamingAndTaggingStrategy().handleResponseTaggingAndFinalSpanName( overallRequestSpan, getRequestInfo(), getResponseInfo(), getErrorThatTriggeredThisResponse() ); } } catch (Throwable t) { logger.error( "Unexpected error occurred while trying to set final span name and response tagging. This " + "exception will be ignored, but should be investigated - it should not happen.", t ); } }
@Test public void handleTracingResponseTaggingAndFinalSpanNameIfNotAlreadyDone_does_not_propagate_unexpected_exception() { // given doThrow(new RuntimeException("intentional exception")).when(distributedTracingConfigMock) .getServerSpanNamingAndTaggingStrategy(); stateSpy.setDistributedTracingConfig(distributedTracingConfigMock); Span overallRequestSpanMock = mock(Span.class); doReturn(overallRequestSpanMock).when(stateSpy).getOverallRequestSpan(); stateSpy.setRequestInfo(requestMock); stateSpy.setResponseInfo(responseMock, errorMock); assertThat(stateSpy.isTracingResponseTaggingAndFinalSpanNameCompleted()).isFalse(); // when Throwable ex = catchThrowable(() -> stateSpy.handleTracingResponseTaggingAndFinalSpanNameIfNotAlreadyDone()); // then assertThat(ex).isNull(); verify(distributedTracingConfigMock).getServerSpanNamingAndTaggingStrategy(); assertThat(stateSpy.isTracingResponseTaggingAndFinalSpanNameCompleted()).isTrue(); } }
@Test public void initChannel_adds_DTraceStartHandler_immediately_after_RequestStateCleanerHandler() { // given HttpChannelInitializer hci = basicHttpChannelInitializerNoUtilityHandlers(); DistributedTracingConfig<Span> distributedTracingConfigMock = mock(DistributedTracingConfig.class); ServerSpanNamingAndTaggingStrategy<Span> serverSpanNamingAndTaggingStrategyMock = mock(ServerSpanNamingAndTaggingStrategy.class); Whitebox.setInternalState(hci, "distributedTracingConfig", distributedTracingConfigMock); doReturn(serverSpanNamingAndTaggingStrategyMock) .when(distributedTracingConfigMock).getServerSpanNamingAndTaggingStrategy(); // when hci.initChannel(socketChannelMock); // then ArgumentCaptor<ChannelHandler> channelHandlerArgumentCaptor = ArgumentCaptor.forClass(ChannelHandler.class); verify(channelPipelineMock, atLeastOnce()).addLast(anyString(), channelHandlerArgumentCaptor.capture()); List<ChannelHandler> handlers = channelHandlerArgumentCaptor.getAllValues(); Pair<Integer, RequestStateCleanerHandler> requestStateCleanerHandler = findChannelHandler(handlers, RequestStateCleanerHandler.class); Pair<Integer, DTraceStartHandler> dTraceStartHandler = findChannelHandler(handlers, DTraceStartHandler.class); assertThat(requestStateCleanerHandler, notNullValue()); assertThat(dTraceStartHandler, notNullValue()); assertThat(dTraceStartHandler.getLeft(), is(requestStateCleanerHandler.getLeft() + 1)); assertThat(extractField(dTraceStartHandler.getRight(), "spanNamingAndTaggingStrategy"), is(serverSpanNamingAndTaggingStrategyMock)); }
@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); } }
distributedTracingConfig.getServerSpanNamingAndTaggingStrategy().wireReceiveFinishAnnotationName() );
doReturn(taggingStrategySpy).when(distributedTracingConfigMock).getServerSpanNamingAndTaggingStrategy();
Whitebox.setInternalState(hci, "distributedTracingConfig", distributedTracingConfigMock); doReturn(serverSpanNamingAndTaggingStrategyMock) .when(distributedTracingConfigMock).getServerSpanNamingAndTaggingStrategy();
Whitebox.setInternalState(hci, "distributedTracingConfig", distributedTracingConfigMock); doReturn(expectedServerSpanNamingAndTaggingStrategy) .when(distributedTracingConfigMock).getServerSpanNamingAndTaggingStrategy();
span.getSpanStartTimeEpochMicros() + TimeUnit.NANOSECONDS.toMicros(nanosAfter - nanosBefore); TimestampedAnnotation wireReceiveStartAnnotation = findAnnotationInSpan( span, distributedTracingConfig.getServerSpanNamingAndTaggingStrategy().wireReceiveStartAnnotationName() ); Assertions.assertThat(wireReceiveStartAnnotation.getTimestampEpochMicros())