@Override public void close() { registerTraceToken(oldToken); } }
@Override public void close() { if (writer instanceof AutoCloseable) { try (TraceTokenScope ignored = registerTraceToken(traceToken)) { ((AutoCloseable)writer).close(); } catch (Exception e) { throwIfUnchecked(e); throw new RuntimeException(e); } } } }
@Override protected Runnable wrapTask(Runnable command) { TraceToken token = getCurrentTraceToken(); return () -> { try (TraceTokenScope ignored = registerTraceToken(token)) { command.run(); } }; } }
@Override protected <T> Callable<T> wrapTask(Callable<T> callable) { TraceToken token = getCurrentTraceToken(); return () -> { try (TraceTokenScope ignored = registerTraceToken(token)) { return callable.call(); } }; }
/** * Associate a given trace token id, with no other properties, with the * current thread. * * @param tokenId The tokenId to associate with the current thread, or null to * remove the thread's token. * @return a {@link TraceTokenScope} which may be used to restore the thread's * previous association. Intended to be used with try-with-resources: * <code> * try (TraceTokenScope ignored = registerRequestToken(traceTokenId)) { * // process request * } * </code> */ public static TraceTokenScope registerRequestToken(@Nullable String tokenId) { if (tokenId == null) { return registerTraceToken(null); } return registerTraceToken(new TraceToken(ImmutableMap.of("id", tokenId))); }
/** * Add properties to the current thread's trace token. If there is * currently no trace token, does nothing. * * @param properties Properties to add or replace. * @return a {@link TraceTokenScope} which may be used to restore the thread's * previous set of properties. */ public static TraceTokenScope addTraceTokenProperties(String... properties) { TokenState tokenState = token.get(); if (tokenState == null) { return new TraceTokenScope(null); } Map<String, String> map = new LinkedHashMap<>(tokenState.getToken()); checkArgument((properties.length % 2) == 0, "odd number of elements in properties"); for (int i = 0; i < properties.length; i += 2) { requireNonNull(properties[i], "property key is null"); requireNonNull(properties[i+1], "property value is null"); map.put(properties[i], properties[i+1]); } return registerTraceToken(new TraceToken(map)); }
@Override public boolean cancel(boolean mayInterruptIfRunning) { try { state.set(JettyAsyncHttpState.CANCELED); jettyRequest.abort(new CancellationException()); return super.cancel(mayInterruptIfRunning); } catch (Throwable e) { try (TraceTokenScope ignored = registerTraceToken(traceToken)) { setException(e); } return true; } }
@Override @SuppressWarnings("ReferenceEquality") // Reference equality to DONE is intentional protected ByteBuffer computeNext() { ByteBuffer chunk = chunks.poll(); if (chunk == null) { try (TraceTokenScope ignored = registerTraceToken(traceToken)) { while (chunk == null) { try { writer.write(); } catch (Exception e) { throwIfUnchecked(e); throw new RuntimeException(e); } chunk = chunks.poll(); } } } if (chunk == DONE) { return endOfData(); } bytesWritten.addAndGet(chunk.position()); ((Buffer)chunk).flip(); return chunk; }
void completed(Response response, InputStream content) { if (state.get() == JettyAsyncHttpState.CANCELED) { return; } try (TraceTokenScope ignored = registerTraceToken(traceToken)) { T value; try { value = processResponse(response, content); } catch (Throwable e) { // this will be an instance of E from the response handler or an Error storeException(e); return; } state.set(JettyAsyncHttpState.DONE); set(value); } }
@Override public Iterator<ByteBuffer> iterator() { final Queue<ByteBuffer> chunks = new BlockingArrayQueue<>(4, 64); Writer writer; try (TraceTokenScope ignored = registerTraceToken(traceToken)) { writer = dynamicBodySource.start(new DynamicBodySourceOutputStream(chunks)); } catch (Exception e) { throwIfUnchecked(e); throw new RuntimeException(e); } return new DynamicBodySourceIterator(chunks, writer, bytesWritten, traceToken); }
@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; String token = request.getHeader("X-Proofpoint-TraceToken"); if (token == null || token.isEmpty()) { registerNewRequestToken(request); } else if (token.charAt(0) == '{') { try { registerTraceToken(TRACE_TOKEN_JSON_CODEC.fromJson(token)); } catch (RuntimeException e) { registerNewRequestToken(request); } } else { registerRequestToken(token); } chain.doFilter(request, response); }
void failed(Throwable throwable) { if (state.get() == JettyAsyncHttpState.CANCELED) { return; } try (TraceTokenScope ignored = registerTraceToken(traceToken)) { // give handler a chance to rewrite the exception or return a value instead if (throwable instanceof Exception) { try { if (throwable instanceof RejectedExecutionException) { jettyHttpClient.maybeLogJettyState(); } T value = responseHandler.handleException(request, (Exception) throwable); // handler returned a value, store it in the future state.set(JettyAsyncHttpState.DONE); set(value); return; } catch (Throwable newThrowable) { throwable = newThrowable; } } // at this point "throwable" will either be an instance of E // from the response handler or not an instance of Exception storeException(throwable); } }
Duration backoff = attemptBackoffPolicy.backoff(previousBackoff); ScheduledFuture<?> scheduledFuture = retryExecutor.schedule(() -> { try (TraceTokenScope scope = registerTraceToken(traceToken)){ synchronized (subFutureLock) { HttpServiceAttempt nextAttempt;