/** 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); }
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; } };
public static Builder builder() { return new Builder(); }
public static Builder builder() { return new Builder(); }
@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()); }
@Override Span joinSpan(SpanId context) { if (!supportsJoin()) return nextSpan(context); // If the sampled flag was left unset, we need to make the decision here if (context.sampled() == null) { return Brave.toSpan(context.toBuilder() .sampled(sampler().isSampled(context.traceId)) .build()); } else if (context.sampled()) { // We know an instrumented caller initiated the trace if they sampled it return Brave.toSpan(context).setShared(); } else { return Brave.toSpan(context); } } }
public Builder toBuilder() { return new Builder(this); }
public Builder toBuilder() { return new Builder(this); }
private SpanId getNewSpanId() { Span parentSpan = spanAndEndpoint().state().getCurrentLocalSpan(); if (parentSpan == null) { ServerSpan serverSpan = spanAndEndpoint().state().getCurrentServerSpan(); if (serverSpan != null) { parentSpan = serverSpan.getSpan(); } } long newSpanId = randomGenerator().nextLong(); SpanId.Builder builder = SpanId.builder().spanId(newSpanId); if (parentSpan == null) return builder.build(); // new trace return builder.traceId(parentSpan.getTrace_id()).parentId(parentSpan.getId()).build(); }
private SpanId getNewSpanId() { Span parentSpan = spanAndEndpoint().state().getCurrentServerSpan().getSpan(); long newSpanId = randomGenerator().nextLong(); SpanId.Builder builder = SpanId.builder().spanId(newSpanId); if (parentSpan == null) return builder.build(); // new trace return builder.traceId(parentSpan.getTrace_id()).parentId(parentSpan.getId()).build(); }
/** * Creates a new span id. * * @param traceId Trace Id. * @param spanId Span Id. * @param parentSpanId Nullable parent span id. * @deprecated Please use {@link SpanId.Builder} */ @Deprecated public static SpanId create(long traceId, long spanId, @Nullable Long parentSpanId) { return SpanId.builder().traceId(traceId).parentId(parentSpanId).spanId(spanId).build(); }
/** * @deprecated since 3.15 use {@link #setStateCurrentTrace(SpanId, String)} */ @Deprecated public void setStateCurrentTrace(long traceId, long spanId, @Nullable Long parentSpanId, String name) { SpanId context = SpanId.builder().traceId(traceId).spanId(spanId).parentId(parentSpanId).build(); setStateCurrentTrace(context, name); }
/** * Creates a new span id. * * @param traceId Trace Id. * @param spanId Span Id. * @param parentSpanId Nullable parent span id. * @deprecated Please use {@link SpanId.Builder} */ @Deprecated public static SpanId create(long traceId, long spanId, @Nullable Long parentSpanId) { return SpanId.builder().traceId(traceId).parentId(parentSpanId).spanId(spanId).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(); } }