/** * Use for control of how tracing state propagates across threads. */ public Builder(ServerClientAndLocalSpanState state) { this.state = Util.checkNotNull(state, "state must be specified."); this.localEndpoint = state.endpoint(); // the legacy span state doesn't support nested spans per (#166). Only permit nesting on the span // state that has instructions on how to use it properly this.allowNestedLocalSpans = state instanceof InheritableServerClientAndLocalSpanState; }
/** * {@inheritDoc} */ @Override public Span span() { return state().getCurrentClientSpan(); }
@Override public Span span() { return state().getCurrentLocalSpan(); }
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(); }
/** * Start a new span for a new client request that will be bound to current thread. The ClientTracer can decide to return * <code>null</code> in case this request should not be traced (eg sampling). * * @param requestName Request name. Should be lowercase and not <code>null</code> or empty. * @return Span id for new request or <code>null</code> in case we should not trace this new client request. */ public SpanId startNewSpan(String requestName) { Boolean sample = spanAndEndpoint().state().sample(); if (Boolean.FALSE.equals(sample)) { spanAndEndpoint().state().setCurrentClientSpan(null); return null; } SpanId newSpanId = getNewSpanId(); if (sample == null) { // No sample indication is present. if (!traceSampler().isSampled(newSpanId.traceId)) { spanAndEndpoint().state().setCurrentClientSpan(null); return null; } } Span newSpan = newSpanId.toSpan(); newSpan.setName(requestName); spanAndEndpoint().state().setCurrentClientSpan(newSpan); return newSpanId; }
Boolean sample = spanAndEndpoint().state().sample(); if (Boolean.FALSE.equals(sample)) { spanAndEndpoint().state().setCurrentLocalSpan(null); return null; spanAndEndpoint().state().setCurrentLocalSpan(null); return null; newSpan.addToBinary_annotations( BinaryAnnotation.create(LOCAL_COMPONENT, component, spanAndEndpoint().endpoint())); spanAndEndpoint().state().setCurrentLocalSpan(newSpan); return newSpanId;
/** * Sets the 'client received' event for current thread. This will also submit span because setting a client received * event means this span is finished. */ public void setClientReceived() { if (submitEndAnnotation(zipkinCoreConstants.CLIENT_RECV, spanCollector())) { spanAndEndpoint().state().setCurrentClientSpan(null); } }
/** * 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); }
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(); }
@Override public Endpoint endpoint() { return state().endpoint(); } }
/** * {@inheritDoc} */ @Override public Span span() { return state().getCurrentClientSpan(); }
@Override public Span span() { return state().getCurrentLocalSpan(); }
/** * {@inheritDoc} */ @Override public Endpoint endpoint() { return state().endpoint(); } }
/** * {@inheritDoc} */ @Override public Endpoint endpoint() { return state().endpoint(); } }
@Override public Endpoint endpoint() { return state().endpoint(); } }
public final Builder state(ServerClientAndLocalSpanState state) { this.currentLocalSpan = new LocalSpanThreadBinder(state); this.currentServerSpan = new ServerSpanThreadBinder(state); this.currentSpan = new ClientSpanThreadBinder(state); this.localEndpoint = state.endpoint(); return this; }
/** * Builder. * <p> * When using this builder constructor we will try to 'guess' ip address by using java.net.* utility classes. * This might be convenient but not necessary what you want. * It is preferred to use constructor that takes ip, port and service name instead. * </p> * * @param serviceName Name of the local service being traced. Should be lowercase and not <code>null</code> or empty. */ public Builder(String serviceName) { int ipv4 = 127 << 24 | 1; try { ipv4 = toInt(getLocalHostLANAddress()); } catch (UnknownHostException e) { logger.log(Level.WARNING, "Unable to get Inet address", e); } state = new ThreadLocalServerClientAndLocalSpanState(ipv4, 0, serviceName); localEndpoint = state.endpoint(); }