@Override public void run() { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = linkTracingAndMdcToCurrentThread(distributedTraceStackForExecution, mdcContextMapForExecution); origRunnable.run(); } finally { unlinkTracingAndMdcFromCurrentThread(originalThreadInfo); } } }
/** * Helper method for creating a `CompletableFuture` that is using the tracing helpers. * <p> * <pre> * AsyncNettyHelper.supplyAsync(() -> { * //do some work in a background thread * return VOID; * }, executor, ctx); * </pre> */ public static <U> CompletableFuture<U> supplyAsync(Supplier<U> f, Executor executor, ChannelHandlerContext ctx) { return CompletableFuture.supplyAsync(supplierWithTracingAndMdc(f, ctx), executor); }
private ChannelFutureListener logOnWriteErrorOperationListener(ChannelHandlerContext ctx) { Consumer<ChannelFuture> errorLoggerConsumerWithTracingAndMdc = consumerWithTracingAndMdc( logOnWriteErrorConsumer, ctx ); return channelFuture -> { if (!channelFuture.isSuccess()) errorLoggerConsumerWithTracingAndMdc.accept(channelFuture); }; }
executeOnlyIfChannelIsActive(ctx, "StreamingCallbackForCtx-unrecoverableErrorOccurred", () -> { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = AsyncNettyHelper.linkTracingAndMdcToCurrentThread(ctx); AsyncNettyHelper.unlinkTracingAndMdcFromCurrentThread(originalThreadInfo);
/** * Contructor that extracts the trace and MDC info from the given {@link ChannelHandlerContext}'s {@link * com.nike.riposte.server.http.HttpProcessingState} so that it can be associated with the thread when the given * operation is executed. * <p/> * The operation you pass in cannot be null (an {@link IllegalArgumentException} will be thrown if you pass in null * for the operation). * <p/> * The {@link ChannelHandlerContext} can be null (or the state inside can be null) but if you pass null it means * there won't be any trace or MDC info associated with the thread when the given operation is executed. */ public RunnableWithTracingAndMdcSupport(Runnable origRunnable, ChannelHandlerContext ctx) { this(origRunnable, AsyncNettyHelper.extractTracingAndMdcInfoFromChannelHandlerContext(ctx)); }
protected void logErrorWithTracing( String msg, Throwable ex, Deque<Span> distributedTraceStackToLink, Map<String, String> mdcContextMapToLink ) { runnableWithTracingAndMdc( () -> logger.error(msg, ex), distributedTraceStackToLink, mdcContextMapToLink ).run(); }
runnableWithTracingAndMdc( () -> logger.error( "Unrecoverable error occurred and somehow the StreamingCallback was " ctx ).run(); executeOnlyIfChannelIsActive( ctx, "ProxyRouterEndpointExecutionHandler-streamchunk-writefuture-unsuccessful", runnableWithTracingAndMdc( () -> logger.error("What? This should never happen. Swallowing.", t), ctx runnableWithTracingAndMdc( () -> logger.error( "Unrecoverable error occurred and somehow the StreamingCallback was not " ctx ).run(); executeOnlyIfChannelIsActive( ctx, "ProxyRouterEndpointExecutionHandler-streamchunk-unsuccessful", () -> ctx.fireExceptionCaught(errorToFire) Throwable actualCause = unwrapAsyncExceptions(cause); if (!(actualCause instanceof WrapperException)) { runnableWithTracingAndMdc( () -> logger.error( "Unable to extract StreamingChannel during error handling and the error that "
/** * Helper method for creating a `CompletableFuture` that is using the tracing helpers and has the CompletableFuture * wrapped in a `CircuitBreaker` * <p> * <pre> * AsyncNettyHelper.supplyAsync(() -> { * //do some work in a background thread * return VOID; * }, circuitBreaker, executor, ctx); * </pre> */ public static <U> CompletableFuture<U> supplyAsync(Supplier<U> f, CircuitBreaker<U> circuitBreaker, Executor executor, ChannelHandlerContext ctx) { return circuitBreaker.executeAsyncCall(() -> supplyAsync(f, executor, ctx)); }
.thenCompose(functionWithTracingAndMdc( aVoid -> endpointProxyRouter .getDownstreamRequestFirstChunkInfo(requestInfo, longRunningTaskExecutor, ctx), + "downstream call."; Exception channelClosedException = new RuntimeException(errorMsg); runnableWithTracingAndMdc( () -> logger.warn(errorMsg), ctx
protected void asyncErrorCallback(ChannelHandlerContext ctx, Throwable error) { executeOnlyIfChannelIsActive( ctx, "NonblockingEndpointExecutionHandler-asyncErrorCallback", () -> ctx.fireExceptionCaught(error) ); } }
ChannelHandlerContext ctx ) { return functionWithTracingAndMdc( aVoid -> {
protected Pair<Deque<Span>, Map<String, String>> linkTracingAndMdcToCurrentThread(ChannelHandlerContext ctx) { return AsyncNettyHelper.linkTracingAndMdcToCurrentThread(ctx); }
/** * Helper method for calling {@link #unlinkTracingAndMdcFromCurrentThread(Deque, Map)} that * gracefully handles the case where the pair you pass in is null - if the pair you pass in is null then {@link * #unlinkTracingAndMdcFromCurrentThread(Deque, Map)} will be called with both arguments null. */ public static void unlinkTracingAndMdcFromCurrentThread( Pair<Deque<Span>, Map<String, String>> threadInfoToResetFor) { Deque<Span> traceStackToResetFor = (threadInfoToResetFor == null) ? null : threadInfoToResetFor.getLeft(); Map<String, String> mdcContextMapToResetFor = (threadInfoToResetFor == null) ? null : threadInfoToResetFor.getRight(); unlinkTracingAndMdcFromCurrentThread(traceStackToResetFor, mdcContextMapToResetFor); }
executeOnlyIfChannelIsActive(ctx, "StreamingCallbackForCtx-unrecoverableErrorOccurred", () -> { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = AsyncNettyHelper.linkTracingAndMdcToCurrentThread(ctx); AsyncNettyHelper.unlinkTracingAndMdcFromCurrentThread(originalThreadInfo);
/** * Contructor that extracts the trace and MDC info from the given {@link ChannelHandlerContext}'s {@link * com.nike.riposte.server.http.HttpProcessingState} so that it can be associated with the thread when the given * operation is executed. * <p/> * The operation you pass in cannot be null (an {@link IllegalArgumentException} will be thrown if you pass in null * for the operation). * <p/> * The {@link ChannelHandlerContext} can be null (or the state inside can be null) but if you pass null it means * there won't be any trace or MDC info associated with the thread when the given operation is executed. */ public FunctionWithTracingAndMdcSupport(Function<T, U> origFunction, ChannelHandlerContext ctx) { this(origFunction, AsyncNettyHelper.extractTracingAndMdcInfoFromChannelHandlerContext(ctx)); }
protected void logErrorWithTracing( String msg, Throwable ex, Deque<Span> distributedTraceStackToLink, Map<String, String> mdcContextMapToLink ) { runnableWithTracingAndMdc( () -> logger.error(msg, ex), distributedTraceStackToLink, mdcContextMapToLink ).run(); }
channelIsActive = executeOnlyIfChannelIsActive(ctx, "StreamingCallbackForCtx-messageReceived", () -> { HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get(); runnableWithTracingAndMdc(() -> logger.error(errorMsg), ctx).run(); throw new IllegalArgumentException(errorMsg); runnableWithTracingAndMdc( () -> { HttpResponse httpResponse = (HttpResponse) msg; String errorMsg = "Expected msg to be a HttpResponse or HttpContent, instead received: " + msg.getClass().getName(); runnableWithTracingAndMdc(() -> logger.error(errorMsg), ctx).run(); throw new IllegalArgumentException(errorMsg);
/** * Helper method for creating a `CompletableFuture` that is using the tracing helpers and has the CompletableFuture * wrapped in a `CircuitBreaker` * <p> * <pre> * AsyncNettyHelper.supplyAsync(() -> { * //do some work in a background thread * return VOID; * }, circuitBreaker, executor, ctx); * </pre> */ public static <U> CompletableFuture<U> supplyAsync(Supplier<U> f, CircuitBreaker<U> circuitBreaker, Executor executor, ChannelHandlerContext ctx) { return circuitBreaker.executeAsyncCall(() -> supplyAsync(f, executor, ctx)); }
.thenCompose(functionWithTracingAndMdc( aVoid -> endpointProxyRouter .getDownstreamRequestFirstChunkInfo(requestInfo, longRunningTaskExecutor, ctx), + "downstream call."; Exception channelClosedException = new RuntimeException(errorMsg); runnableWithTracingAndMdc( () -> logger.warn(errorMsg), ctx
protected void asyncErrorCallback(ChannelHandlerContext ctx, Throwable error) { executeOnlyIfChannelIsActive( ctx, "NonblockingEndpointExecutionHandler-asyncErrorCallback", () -> ctx.fireExceptionCaught(error) ); } }