static int parseFlags(String b3, int pos) { int flags; char sampledChar = b3.charAt(pos); if (sampledChar == 'd') { flags = FLAG_SAMPLED_SET | FLAG_SAMPLED | FLAG_DEBUG; } else if (sampledChar == '1') { flags = FLAG_SAMPLED_SET | FLAG_SAMPLED; } else if (sampledChar == '0') { flags = FLAG_SAMPLED_SET; } else { logInvalidSampled(pos); flags = 0; } return flags; }
static TraceData tryParseSamplingFlags(String b3, int pos) { int flags = parseFlags(b3, pos); if (flags == 0) return null; if ((flags & FLAG_DEBUG) == FLAG_DEBUG // brave 3 doesn't support debug || (flags & FLAG_SAMPLED) == FLAG_SAMPLED) { return TraceData.SAMPLED; } return TraceData.NOT_SAMPLED; }
return tryParseSamplingFlags(b3, pos); traceIdHigh = tryParse16HexCharacters(b3, pos, length); pos += 16; // upper 64 bits of the trace ID traceId = tryParse16HexCharacters(b3, pos, length); } else { traceIdHigh = 0L; traceId = tryParse16HexCharacters(b3, pos, length); if (!checkHyphen(b3, pos++)) return null; long spanId = tryParse16HexCharacters(b3, pos, length); if (spanId == 0L) { LOG.log(Level.FINE, "Invalid input: expected a 16 lower hex span ID at offset {0}", pos); if (!checkHyphen(b3, pos++)) return null; if (length == pos + 1 || delimiterFollowsPos(b3, pos, length)) { flags = parseFlags(b3, pos); if (flags == 0) return null; if (!checkHyphen(b3, pos++)) return null; parentId = tryParse16HexCharacters(b3, pos, length); if (parentId == 0L) { LOG.log(Level.FINE, .parentId(parentId != 0L ? parentId : null) .spanId(spanId) .sampled(debug ? Boolean.TRUE : sampled(flags))
@Override public TraceData getTraceData() { // try to extract single-header format String b3String = request.getHttpHeaderValue("b3"); if (b3String != null) { TraceData extracted = B3SingleFormat.parseB3SingleFormat(b3String); if (extracted != null) return extracted; } String sampled = request.getHttpHeaderValue(BraveHttpHeaders.Sampled.getName()); String parentSpanId = request.getHttpHeaderValue(BraveHttpHeaders.ParentSpanId.getName()); String traceId = request.getHttpHeaderValue(BraveHttpHeaders.TraceId.getName()); String spanId = request.getHttpHeaderValue(BraveHttpHeaders.SpanId.getName()); // Official sampled value is 1, though some old instrumentation send true Boolean parsedSampled = sampled != null ? sampled.equals("1") || sampled.equalsIgnoreCase("true") : null; if (traceId != null && spanId != null) { return TraceData.create(getSpanId(traceId, spanId, parentSpanId, parsedSampled)); } else if (parsedSampled == null) { return TraceData.EMPTY; } else if (parsedSampled) { return TraceData.SAMPLED; } else { return TraceData.NOT_SAMPLED; } }