/** Same as {@link Span.Builder#parentId(String)} */ public Builder parentId(String parentId) { this.parentId = parentId != null ? lowerHexToUnsignedLong(parentId) : 0L; return this; }
/** * Parses a 1 to 32 character lower-hex string with no prefix into an unsigned long, tossing any * bits higher than 64. */ public static long lowerHexToUnsignedLong(String lowerHex) { int length = lowerHex.length(); if (length < 1 || length > 32) throw isntLowerHexLong(lowerHex); // trim off any high bits int beginIndex = length > 16 ? length - 16 : 0; return lowerHexToUnsignedLong(lowerHex, beginIndex); }
/** * Parses a 16 character lower-hex string with no prefix into an unsigned long, starting at the * spe index. */ public static long lowerHexToUnsignedLong(String lowerHex, int index) { long result = 0; for (int endIndex = Math.min(index + 16, lowerHex.length()); index < endIndex; index++) { char c = lowerHex.charAt(index); result <<= 4; if (c >= '0' && c <= '9') { result |= c - '0'; } else if (c >= 'a' && c <= 'f') { result |= c - 'a' + 10; } else { throw isntLowerHexLong(lowerHex); } } return result; }
/** Same as {@link Span.Builder#traceId(String)} */ public Builder traceId(String traceId) { if (traceId == null) throw new NullPointerException("traceId == null"); if (traceId.length() == 32) { traceIdHigh = lowerHexToUnsignedLong(traceId, 0); } this.traceId = lowerHexToUnsignedLong(traceId); return this; }
/** Same as {@link Span.Builder#id(String)} */ public Builder id(String id) { if (id == null) throw new NullPointerException("id == null"); this.id = lowerHexToUnsignedLong(id); return this; }
/** * Returns true if spans with this trace ID should be recorded to storage. * * <p>Zipkin v1 allows storage-layer sampling, which can help prevent spikes in traffic from * overloading the system. Debug spans are always stored. * * <p>This uses only the lower 64 bits of the trace ID as instrumentation still send mixed trace * ID width. * * @param hexTraceId the lower 64 bits of the span's trace ID are checked against the boundary * @param debug when true, always passes sampling */ public boolean isSampled(String hexTraceId, boolean debug) { if (Boolean.TRUE.equals(debug)) return true; long traceId = HexCodec.lowerHexToUnsignedLong(hexTraceId); // The absolute value of Long.MIN_VALUE is larger than a long, so Math.abs returns identity. // This converts to MAX_VALUE to avoid always dropping when traceId == Long.MIN_VALUE long t = traceId == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(traceId); return t <= boundary(); }
Call<List<Span>> newCall(String hexTraceId) { long traceId = HexCodec.lowerHexToUnsignedLong(hexTraceId); Call<List<Span>> result = new SelectFromTraces(this, Collections.singleton(traceId), maxTraceCols) .flatMap(accumulateSpans); return strictTraceId ? result.map(StrictTraceId.filterSpans(hexTraceId)) : result; }
containerPack.gxid = HexCodec.lowerHexToUnsignedLong(entry.getKey()); containerPack.spanCount = entry.getValue().size();
private static SpanPack makeSpanPack(Span span) { SpanPack pack = new SpanPack(); pack.gxid = HexCodec.lowerHexToUnsignedLong(span.traceId()); pack.txid = HexCodec.lowerHexToUnsignedLong(span.id()); pack.caller = span.parentId() != null ? HexCodec.lowerHexToUnsignedLong(span.parentId()) : 0L;