DynamicBodySourceContentProvider(DynamicBodySource dynamicBodySource, AtomicLong bytesWritten) { this.dynamicBodySource = dynamicBodySource; this.bytesWritten = bytesWritten; traceToken = getCurrentTraceToken(); }
@Override public void close() { registerTraceToken(oldToken); } }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof TraceTokenManager.TokenState) { TraceTokenManager.TokenState that = (TraceTokenManager.TokenState) o; return (this.token.equals(that.getToken())) && (this.oldThreadName.equals(that.getOldThreadName())); } return false; }
@Override protected Runnable wrapTask(Runnable command) { TraceToken token = getCurrentTraceToken(); return () -> { try (TraceTokenScope ignored = registerTraceToken(token)) { command.run(); } }; } }
/** * 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 Request filterRequest(Request request) { requireNonNull(request, "request is null"); TraceToken token = getCurrentTraceToken(); if (token == null) { return request; } String tokenString; if (token.size() == 1) { tokenString = token.toString(); } else { tokenString = TRACE_TOKEN_JSON_CODEC.toJson(token); } return fromRequest(request) .addHeader(TRACETOKEN_HEADER, tokenString) .build(); }
/** * 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))); }
oldThreadName = oldTokenState.getOldThreadName(); TraceTokenManager.token.set(new AutoValue_TraceTokenManager_TokenState(token, oldThreadName)); currentThread().setName(oldThreadName + " " + token); return new TraceTokenScope(null); return new TraceTokenScope(oldTokenState.getToken());
/** * @return The current thread's trace token in string form, or null if no token. * @deprecated Use {@link #getCurrentTraceToken()}. */ @Deprecated @Nullable public static String getCurrentRequestToken() { TokenState tokenState = token.get(); if (tokenState == null) { return null; } return tokenState.getToken().toString(); }
/** * Create and register a new trace token with a random id. * @param properties Additional properties to include in the token. * @return The id of the created token. */ public static String createAndRegisterNewRequestToken(String... properties) { checkArgument((properties.length % 2) == 0, "odd number of elements in properties"); String newToken = UUID.randomUUID().toString(); registerRequestToken(newToken); if (properties.length != 0) { addTraceTokenProperties(properties); } return newToken; }
@Override public Request filterRequest(Request request) { checkNotNull(request, "request is null"); String token = traceTokenManager.getCurrentRequestToken(); if (token == null) { return request; } return fromRequest(request) .addHeader(TRACETOKEN_HEADER, token) .build(); }
private void registerNewRequestToken(HttpServletRequest request) throws UnknownHostException { byte[] randomBytes = new byte[15]; SECURE_RANDOM.get().nextBytes(randomBytes); registerRequestToken(tokenPrefix + encodeAddress(InetAddress.getByName(clientAddressExtractor.clientAddressFor(request))) + BASE64_URL_ENCODER.encodeToString(randomBytes) ); } }
@Override public int hashCode() { int h$ = 1; h$ *= 1000003; h$ ^= token.hashCode(); h$ *= 1000003; h$ ^= oldThreadName.hashCode(); return h$; }
/** * @return The current thread's trace token, or null if no token. */ @Nullable public static TraceToken getCurrentTraceToken() { TokenState tokenState = token.get(); if (tokenState == null) { return null; } return tokenState.getToken(); }
/** * Remove the thread's token. */ public static void clearRequestToken() { TokenState oldToken = TraceTokenManager.token.get(); token.remove(); if (oldToken != null) { currentThread().setName(oldToken.getOldThreadName()); } }
@JsonCreator static TraceToken createJson(Map<String, String> map) { return new TraceToken(Maps.filterKeys(map, key -> !key.startsWith("_"))); }
@Override protected <T> Callable<T> wrapTask(Callable<T> callable) { TraceToken token = getCurrentTraceToken(); return () -> { try (TraceTokenScope ignored = registerTraceToken(token)) { return callable.call(); } }; }
@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); } } } }
JettyResponseFuture(JettyHttpClient jettyHttpClient, Request request, org.eclipse.jetty.client.api.Request jettyRequest, ResponseHandler<T, E> responseHandler, AtomicLong bytesWritten, RequestStats stats) { this.jettyHttpClient = requireNonNull(jettyHttpClient, "jettyHttpClientf is null"); this.request = requireNonNull(request, "request is null"); this.jettyRequest = requireNonNull(jettyRequest, "jettyRequest is null"); this.responseHandler = requireNonNull(responseHandler, "responseHandler is null"); this.bytesWritten = requireNonNull(bytesWritten, "bytesWritten is null"); this.stats = requireNonNull(stats, "stats is null"); traceToken = getCurrentTraceToken(); }
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); } }