/** * @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(); }
@JsonCreator static TraceToken createJson(Map<String, String> map) { return new TraceToken(Maps.filterKeys(map, key -> !key.startsWith("_"))); }
@Override public int hashCode() { int h$ = 1; h$ *= 1000003; h$ ^= token.hashCode(); h$ *= 1000003; h$ ^= oldThreadName.hashCode(); return h$; }
@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(); }
EventJsonSerializer(NodeInfo nodeInfo, @Nullable TraceToken token, EventTypeMetadata<T> eventTypeMetadata) { if (token == null || eventTypeMetadata.getTraceTokenField() != null) { this.token = null; } else if (token.size() == 1) { this.token = token.get("id"); } else { this.token = TRACE_TOKEN_JSON_CODEC.toJson(token); } this.eventTypeMetadata = requireNonNull(eventTypeMetadata, "eventTypeMetadata is null"); if (eventTypeMetadata.getHostField() == null) { hostName = nodeInfo.getInternalHostname(); } else { hostName = null; } }
@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; }
/** * 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)); }