@Override public void run() { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = linkTracingAndMdcToCurrentThread(distributedTraceStackForExecution, mdcContextMapForExecution); origRunnable.run(); } finally { unlinkTracingAndMdcFromCurrentThread(originalThreadInfo); } } }
@Override public R apply(T t, U u) { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = linkTracingAndMdcToCurrentThread(distributedTraceStackForExecution, mdcContextMapForExecution); return origBiFunction.apply(t, u); } finally { unlinkTracingAndMdcFromCurrentThread(originalThreadInfo); } } }
@Override public void run() { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = linkTracingAndMdcToCurrentThread(distributedTraceStackForExecution, mdcContextMapForExecution); origRunnable.run(); } finally { unlinkTracingAndMdcFromCurrentThread(originalThreadInfo); } } }
@Override public U get() { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = linkTracingAndMdcToCurrentThread(distributedTraceStackForExecution, mdcContextMapForExecution); return origSupplier.get(); } finally { unlinkTracingAndMdcFromCurrentThread(originalThreadInfo); } } }
@Override public U apply(T t) { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = linkTracingAndMdcToCurrentThread(distributedTraceStackForExecution, mdcContextMapForExecution); return origFunction.apply(t); } finally { unlinkTracingAndMdcFromCurrentThread(originalThreadInfo); } } }
@Override public void accept(T t) { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = linkTracingAndMdcToCurrentThread(distributedTraceStackForExecution, mdcContextMapForExecution); origConsumer.accept(t); } finally { unlinkTracingAndMdcFromCurrentThread(originalThreadInfo); } } }
@Override public void operationComplete(ChannelFuture future) throws Exception { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = linkTracingAndMdcToCurrentThread(distributedTraceStackForExecution, mdcContextMapForExecution); postCompleteOperation.accept(future); } finally { unlinkTracingAndMdcFromCurrentThread(originalThreadInfo); } } }
@Override public void accept(T t, U u) { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = linkTracingAndMdcToCurrentThread(distributedTraceStackForExecution, mdcContextMapForExecution); origBiConsumer.accept(t, u); } finally { unlinkTracingAndMdcFromCurrentThread(originalThreadInfo); } } }
@Override public void accept(T t) { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = linkTracingAndMdcToCurrentThread(distributedTraceStackForExecution, mdcContextMapForExecution); origConsumer.accept(t); } finally { unlinkTracingAndMdcFromCurrentThread(originalThreadInfo); } } }
@Override public U call() throws Exception { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = linkTracingAndMdcToCurrentThread(distributedTraceStackForExecution, mdcContextMapForExecution); return origCallable.call(); } finally { unlinkTracingAndMdcFromCurrentThread(originalThreadInfo); } } }
@Override public U get() { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = linkTracingAndMdcToCurrentThread(distributedTraceStackForExecution, mdcContextMapForExecution); return origSupplier.get(); } finally { unlinkTracingAndMdcFromCurrentThread(originalThreadInfo); } } }
@Override public void accept(T t, U u) { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = linkTracingAndMdcToCurrentThread(distributedTraceStackForExecution, mdcContextMapForExecution); origBiConsumer.accept(t, u); } finally { unlinkTracingAndMdcFromCurrentThread(originalThreadInfo); } } }
@Override public U call() throws Exception { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = linkTracingAndMdcToCurrentThread(distributedTraceStackForExecution, mdcContextMapForExecution); return origCallable.call(); } finally { unlinkTracingAndMdcFromCurrentThread(originalThreadInfo); } } }
@Override public void operationComplete(ChannelFuture future) throws Exception { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = linkTracingAndMdcToCurrentThread(distributedTraceStackForExecution, mdcContextMapForExecution); postCompleteOperation.accept(future); } finally { unlinkTracingAndMdcFromCurrentThread(originalThreadInfo); } } }
@Override public R apply(T t, U u) { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = linkTracingAndMdcToCurrentThread(distributedTraceStackForExecution, mdcContextMapForExecution); return origBiFunction.apply(t, u); } finally { unlinkTracingAndMdcFromCurrentThread(originalThreadInfo); } } }
@Override public U apply(T t) { Pair<Deque<Span>, Map<String, String>> originalThreadInfo = null; try { originalThreadInfo = linkTracingAndMdcToCurrentThread(distributedTraceStackForExecution, mdcContextMapForExecution); return origFunction.apply(t); } finally { unlinkTracingAndMdcFromCurrentThread(originalThreadInfo); } } }
/** * 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); }
/** * 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); }
protected void unlinkTracingAndMdcFromCurrentThread(ChannelHandlerContext ctx, Pair<Deque<Span>, Map<String, String>> origThreadInfo) { // Update the state (if we have any) with the current values of the MDC and tracer data HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get(); if (state != null) { // Get references to the *current* MDC and tracer data for storing in our ctx // and set them on the state object Map<String, String> currentMdcContextMapForState = MDC.getCopyOfContextMap(); Deque<Span> currentTraceStackForState = Tracer.getInstance().unregisterFromThread(); state.setLoggerMdcContextMap(currentMdcContextMapForState); state.setDistributedTraceStack(currentTraceStackForState); } // Reset the thread to the way it was before linkTracingAndMdcToCurrentThread was called AsyncNettyHelper.unlinkTracingAndMdcFromCurrentThread(origThreadInfo); }
protected void unlinkTracingAndMdcFromCurrentThread(ChannelHandlerContext ctx, Pair<Deque<Span>, Map<String, String>> origThreadInfo) { // Update the state (if we have any) with the current values of the MDC and tracer data HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get(); if (state != null) { // Get references to the *current* MDC and tracer data for storing in our ctx // and set them on the state object Map<String, String> currentMdcContextMapForState = MDC.getCopyOfContextMap(); Deque<Span> currentTraceStackForState = Tracer.getInstance().unregisterFromThread(); state.setLoggerMdcContextMap(currentMdcContextMapForState); state.setDistributedTraceStack(currentTraceStackForState); } // Reset the thread to the way it was before linkTracingAndMdcToCurrentThread was called AsyncNettyHelper.unlinkTracingAndMdcFromCurrentThread(origThreadInfo); }