/** * 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, 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); }