static boolean checkHyphen(CharSequence b3, int pos) { if (b3.charAt(pos) == '-') return true; Platform.get().log("Invalid input: expected a hyphen(-) delimiter at offset {0}", pos, null); return false; }
static void parseRemoteAddress(RpcContext rpcContext, Span span) { InetSocketAddress remoteAddress = rpcContext.getRemoteAddress(); if (remoteAddress == null) return; span.remoteIpAndPort(Platform.get().getHostString(remoteAddress), remoteAddress.getPort()); }
boolean invalidIdLength(Object key, int length, int max) { if (length > 1 && length <= max) return false; assert max == 32 || max == 16; Platform.get().log(max == 32 ? "{0} should be a 1 to 32 character lower-hex string with no prefix" : "{0} should be a 1 to 16 character lower-hex string with no prefix", key, null); return true; }
/** * This sets the client IP:port to the {@linkplain Channel#remoteAddress() remote address} if * {@link HttpServerAdapter#parseClientIpAndPort} fails. */ void parseChannelAddress(ChannelHandlerContext ctx, HttpRequest request, Span span) { if (adapter.parseClientIpFromXForwardedFor(request, span)) return; InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress(); if (remoteAddress.getAddress() == null) return; span.remoteIpAndPort(Platform.get().getHostString(remoteAddress), remoteAddress.getPort()); }
private int readVarint(byte b1) { int b2 = buf[pos++]; if ((b2 & 0xf0) != 0) { Platform.get().log("Greater than 14-bit varint at position {0}", pos, null); return -1; } return b1 & 0x7f | b2 << 28; }
boolean isNull(Object key, String maybeNull) { if (maybeNull != null) return false; Platform.get().log("{0} was null", key, null); return true; }
void maybeLogNotLowerHex(String notLowerHex) { Platform.get().log("{0} is not a lower-hex string", notLowerHex, null); }
static void logInvalidSampled(int pos) { Platform.get().log("Invalid input: expected 0, 1 or d for sampled at offset {0}", pos, null); }
@Override public boolean handle(TraceContext context, MutableSpan span) { if (noop.get()) return false; try { return delegate.handle(context, span); } catch (RuntimeException e) { Platform.get().log("error accepting {0}", context, e); return false; } }
/** Returns zero if truncated, malformed, or too big after logging */ static long tryParseParentId(CharSequence b3, int pos, int endIndex) { if (endIndex < pos + 16) { Platform.get().log("Invalid input: truncated", null); return 0L; } long parentId = tryParse16HexCharacters(b3, pos, endIndex); if (parentId == 0L) { Platform.get() .log("Invalid input: expected a 16 lower hex parent ID at offset {0}", pos, null); return 0L; } pos += 16; if (endIndex != pos) { Platform.get().log("Invalid input: parent ID is too long", null); return 0L; } return parentId; }
@Override public Map<String, String> parseBytes(byte[] buf) { if (buf == null) throw new NullPointerException("buf == null"); // programming error if (buf.length == 0) return Collections.emptyMap(); Buffer bytes = new Buffer(buf); byte version = bytes.readByte(); if (version != VERSION) { Platform.get().log("Invalid input: unsupported version {0}", version, null); return null; } Map<String, String> result = new LinkedHashMap<>(); while (bytes.remaining() > 3) { // tag for field ID and two lengths if (bytes.readByte() == TAG_FIELD_ID) { String key = bytes.readLengthPrefixed(); if (key == null) break; String val = bytes.readLengthPrefixed(); if (val == null) break; result.put(key, val); } else { Platform.get().log("Invalid input: expected TAG_FIELD_ID at offset {0}", bytes.pos, null); break; } } return result; }
/** Reports spans orphaned by garbage collection. */ void reportOrphanedSpans() { RealKey contextKey; // This is called on critical path of unrelated traced operations. If we have orphaned spans, be // careful to not penalize the performance of the caller. It is better to cache time when // flushing a span than hurt performance of unrelated operations by calling // currentTimeMicroseconds N times long flushTime = 0L; boolean noop = zipkinHandler == FinishedSpanHandler.NOOP || this.noop.get(); while ((contextKey = (RealKey) poll()) != null) { PendingSpan value = delegate.remove(contextKey); if (noop || value == null || !contextKey.sampled) continue; if (flushTime == 0L) flushTime = clock.currentTimeMicroseconds(); TraceContext context = InternalPropagation.instance.newTraceContext( InternalPropagation.FLAG_SAMPLED_SET | InternalPropagation.FLAG_SAMPLED, contextKey.traceIdHigh, contextKey.traceId, contextKey.localRootId, 0L, contextKey.spanId, Collections.emptyList() ); value.state.annotate(flushTime, "brave.flush"); try { zipkinHandler.handle(context, value.state); } catch (RuntimeException e) { Platform.get().log("error reporting {0}", context, e); } } }
if (bytes.length == 0) return null; if (bytes[0] != VERSION) { Platform.get().log("Invalid input: unsupported version {0}", bytes[0], null); return null; Platform.get().log("Invalid input: truncated", null); return null; pos += 16; } else { Platform.get().log("Invalid input: expected trace ID at offset {0}", pos, null); return null; pos += 8; } else { Platform.get().log("Invalid input: expected span ID at offset {0}", pos, null); return null; pos++; if (bytes.length < pos + 1) { Platform.get().log("Invalid input: truncated", null); return null;
int endIndex) { if (beginIndex == endIndex) { Platform.get().log("Invalid input: empty", null); return null; Platform.get().log("Invalid input: truncated", null); return null; } else if (endIndex > FORMAT_MAX_LENGTH) { Platform.get().log("Invalid input: too long", null); return null; Platform.get().log("Invalid input: expected a 16 or 32 lower hex trace ID at offset 0", null); return null; Platform.get().log("Invalid input: trace ID is too long", null); return null; Platform.get().log("Invalid input: expected a 16 lower hex span ID at offset {0}", pos, null); return null; if (endIndex > pos) { if (isLowerHex(b3.charAt(pos))) { Platform.get().log("Invalid input: span ID is too long", null); return null; Platform.get().log("Invalid input: truncated", null); return null;
traceIdHigh = traceId128Bit ? Platform.get().nextTraceIdHigh() : 0L; traceId = nextId; } else { // child of an existing span. ensure the shared flag is unset
@Override public String generate() { return Long.toHexString(Platform.get().nextTraceIdHigh()); } }
static boolean checkHyphen(CharSequence b3, int pos) { if (b3.charAt(pos) == '-') return true; Platform.get().log("Invalid input: expected a hyphen(-) delimiter at offset {0}", pos, null); return false; }
private int readVarint(byte b1) { int b2 = buf[pos++]; if ((b2 & 0xf0) != 0) { Platform.get().log("Greater than 14-bit varint at position {0}", pos, null); return -1; } return b1 & 0x7f | b2 << 28; }