/** * Request a new local span, which starts now. * * @param component {@link zipkinCoreConstants#LOCAL_COMPONENT component} responsible for the operation * @param operation name of the operation that's begun * @return metadata about the new span or null if one wasn't started due to sampling policy. * @see zipkinCoreConstants#LOCAL_COMPONENT */ public SpanId startNewSpan(String component, String operation) { SpanId spanId = startNewSpan(component, operation, currentTimeMicroseconds()); if (spanId == null) return null; spanAndEndpoint().span().startTick = System.nanoTime(); // embezzle start tick into an internal field. return spanId; }
/** * Completes the span, which took {@code duration} microseconds. */ public void finishSpan(long duration) { Span span = spanAndEndpoint().span(); if (span == null) return; synchronized (span) { span.setDuration(duration); spanCollector().collect(span); } spanAndEndpoint().state().setCurrentLocalSpan(null); }
/** * Completes the span, assigning the most precise duration possible. */ public void finishSpan() { long endTick = System.nanoTime(); Span span = spanAndEndpoint().span(); if (span == null) return; Long startTick = span.startTick; final long duration; if (startTick != null) { duration = (endTick - startTick) / 1000; } else { duration = currentTimeMicroseconds() - span.getTimestamp(); } finishSpan(duration); }
Boolean sample = spanAndEndpoint().state().sample(); if (Boolean.FALSE.equals(sample)) { spanAndEndpoint().state().setCurrentLocalSpan(null); return null; spanAndEndpoint().state().setCurrentLocalSpan(null); return null; newSpan.setTimestamp(timestamp); newSpan.addToBinary_annotations( BinaryAnnotation.create(LOCAL_COMPONENT, component, spanAndEndpoint().endpoint())); spanAndEndpoint().state().setCurrentLocalSpan(newSpan); return newSpanId;
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(); }