/** Deserializes this from a big-endian byte array */ public static SpanId fromBytes(byte[] bytes) { checkNotNull(bytes, "bytes"); if (bytes.length != 32 && bytes.length != 40) { throw new IllegalArgumentException("bytes.length " + bytes.length + " != 32 or 40"); } ByteBuffer buffer = ByteBuffer.wrap(bytes); Builder builder = new Builder(); builder.spanId(buffer.getLong(0)); builder.parentId(buffer.getLong(8)); if (bytes.length == 32) { builder.traceId(buffer.getLong(16)); builder.flags(buffer.getLong(24)); } else { builder.traceIdHigh(buffer.getLong(16)); builder.traceId(buffer.getLong(24)); builder.flags(buffer.getLong(32)); } return new SpanId(builder); }
.traceIdHigh(traceIdHigh) .traceId(traceId) .parentId(parentId != 0L ? parentId : null)
static SpanId getSpanId(String traceId, String spanId, String parentSpanId, Boolean sampled) { return SpanId.builder() .traceIdHigh(traceId.length() == 32 ? convertToLong(traceId, 0) : 0) .traceId(convertToLong(traceId)) .spanId(convertToLong(spanId)) .sampled(sampled) .parentId(parentSpanId == null ? null : convertToLong(parentSpanId)).build(); } }
public static Span fromZipkin(zipkin2.Span v2) { V1Span in = V2SpanConverter.create().convert(v2); Span result = newSpan(SpanId.builder() .traceIdHigh(in.traceIdHigh()) .traceId(in.traceId()) .spanId(in.id()) .parentId(in.parentId() != 0L ? in.parentId() : null) .debug(in.debug() != null ? in.debug() : false).build() ); result.setName(in.name()); result.setTimestamp(in.timestamp() != 0L ? in.timestamp() : null); result.setDuration(in.duration() != 0L ? in.duration() : null); for (zipkin2.v1.V1Annotation a : in.annotations()) { result.addToAnnotations(Annotation.create( a.timestamp(), a.value(), to(a.endpoint()))); } for (zipkin2.v1.V1BinaryAnnotation a : in.binaryAnnotations()) { if (a.type() != 0 && a.type() != 6) continue;; result.addToBinary_annotations(BinaryAnnotation.create( a.key(), a.type() == 0 ? new byte[]{1}: a.stringValue().getBytes(UTF_8), a.type() == 0 ? AnnotationType.BOOL : AnnotationType.STRING, to(a.endpoint()))); } return result; }
@Override public SpanId context(Span span) { if (span.context != null) return span.context; // If we got here, some implementation of state passed a deprecated span // If no ids were set, it is junk, so return null if (span.trace_id == 0 && span.parent_id == null && span.id == 0) return null; // Attempt to backfill the span context synchronized (span) { if (span.context != null) return span.context; span.context = SpanId.builder() .traceIdHigh(span.getTrace_id_high()) .traceId(span.getTrace_id()) .parentId(span.getParent_id()) .spanId(span.getId()) .debug(span.debug != null ? span.debug : false) .build(); } return span.context; } };
@Override Span nextSpan(@Nullable SpanId maybeParent) { // Generates a new 64-bit ID, taking care to dodge zero which can be confused with absent long newSpanId = randomGenerator().nextLong(); while (newSpanId == 0L) { newSpanId = randomGenerator().nextLong(); } if (maybeParent == null) { // new trace return Brave.toSpan(SpanId.builder() .traceIdHigh(traceId128Bit() ? nextTraceIdHigh(randomGenerator()) : 0L) .traceId(newSpanId) .spanId(newSpanId) .sampled(sampler().isSampled(newSpanId)) .build()); } return Brave.toSpan(maybeParent.toBuilder() .parentId(maybeParent.spanId) .spanId(newSpanId) .build()); }
static SpanId getSpanId(String traceId, String spanId, String parentSpanId, Boolean sampled) { return SpanId.builder() .traceIdHigh(traceId.length() == 32 ? convertToLong(traceId, 0) : 0) .traceId(convertToLong(traceId)) .spanId(convertToLong(spanId)) .sampled(sampled) .parentId(parentSpanId == null ? null : convertToLong(parentSpanId)).build(); } }
static SpanId toSpanId(TraceContext context) { return SpanId.builder() .traceIdHigh(context.traceIdHigh()) .traceId(context.traceId()) .parentId(context.parentId()) .spanId(context.spanId()) .debug(context.debug()) .sampled(context.sampled()).build(); } }