@Override public boolean isRoot(Span span) { // TODO replace with Span#unwrap once https://github.com/opentracing/opentracing-java/pull/211 is merged if (span instanceof SpanWrapper) { span = ((SpanWrapper) span).getDelegate(); } if (span instanceof BraveSpan) { final BraveSpan braveSpan = (BraveSpan) span; return braveSpan.unwrap().context().parentId() == null; } return false; }
@Override public BraveSpan log(Map<String, ?> fields) { if (finishCalled) return this; if (fields.isEmpty()) return this; return log(toAnnotation(fields)); }
@Override public BraveSpan setTag(String key, String value) { if (finishCalled) return this; if (trySetPeer(delegate, key, value)) return this; if (trySetKind(key, value)) return this; delegate.tag(key, value); return this; }
/** * <em>Note:</em>If the key is {@linkplain Tags#SAMPLING_PRIORITY} and the value is zero, the * current span will be abandoned and future references to the {@link #context()} will be * unsampled. This does not affect the active span, nor does it affect any equivalent instances of * this object. This is a best efforts means to handle late sampling decisions. */ @Override public BraveSpan setTag(String key, Number value) { if (finishCalled) return this; if (trySetPeer(key, value)) return this; // handle late sampling decision if (Tags.SAMPLING_PRIORITY.getKey().equals(key) && value.intValue() == 0) { delegate.abandon(); // convert the span to no-op delegate = tracer.toSpan(delegate.context().toBuilder().sampled(false).build()); } return setTag(key, value.toString()); }
@Override public void finish() { if (finishCalled) return; finishCalled = true; trySetRemoteIpAndPort(); delegate.finish(); }
/** * Converts a map to a string of form: "key1=value1 key2=value2" */ static String toAnnotation(Map<String, ?> fields) { // special-case the "event" field which is similar to the semantics of a zipkin annotation Object event = fields.get("event"); if (event != null && fields.size() == 1) return event.toString(); return joinOnEqualsSpace(fields); }
@Override public BraveSpan setTag(String key, boolean value) { if (finishCalled) return this; if (Tags.ERROR.getKey().equals(key) && !value) return this; return setTag(key, Boolean.toString(value)); }
/** Attempts to get a span from the current api, falling back to brave's native one */ BraveSpan currentSpan() { BraveScope scope = currentScopes.get().peekFirst(); if (scope != null) { return scope.span(); } else { brave.Span braveSpan = tracer.currentSpan(); if (braveSpan != null) { return new BraveSpan(tracer, braveSpan); } } return null; }
@Override public void close() { if (finishSpanOnClose) { wrapped.finish(); } scope.close(); source.deregister(this); }
@Override public BraveSpan log(long timestampMicroseconds, Map<String, ?> fields) { if (finishCalled) return this; if (fields.isEmpty()) return this; // in real life, do like zipkin-go-opentracing: "key1=value1 key2=value2" return log(timestampMicroseconds, toAnnotation(fields)); }
@Override public void finish(long finishMicros) { if (finishCalled) return; finishCalled = true; trySetRemoteIpAndPort(); delegate.finish(finishMicros); }
@Override public boolean isSampled(Span span) { // TODO replace with Span#unwrap once https://github.com/opentracing/opentracing-java/pull/211 is merged if (span instanceof SpanWrapper) { span = ((SpanWrapper) span).getDelegate(); } if (span instanceof BraveSpan) { final BraveSpan braveSpan = (BraveSpan) span; return braveSpan.unwrap().context().sampled(); } return false; }