String spanId = header.containsKey(TraceMessageHeaders.SPAN_ID_NAME) ? String.valueOf(header.get(TraceMessageHeaders.SPAN_ID_NAME)) : traceId; spanBuilder .traceIdHigh(traceId.length() == 32 ? Span.hexToId(traceId, 0) : 0) .traceId(Span.hexToId(traceId)) .spanId(Span.hexToId(spanId));
@Override @javax.annotation.Nullable public Span createSpan(String name, /*@Nullable*/ Sampler sampler) { String shortenedName = SpanNameUtil.shorten(name); Span span; if (isTracing()) { span = createChild(getCurrentSpan(), shortenedName); } else { long id = createId(); span = Span.builder() .name(shortenedName) .traceIdHigh(this.traceId128 ? createTraceIdHigh() : 0L) .traceId(id) .spanId(id) .build(); if (sampler == null) { sampler = this.defaultSampler; } span = sampledSpan(span, sampler); this.spanLogger.logStartedSpan(null, span); } return continueSpan(span); }
Span.builder() .name(shortenedName) .traceIdHigh(this.traceId128 ? createTraceIdHigh() : 0L) .traceId(id) .spanId(id) Span.builder() .name(shortenedName) .traceIdHigh(parent.getTraceIdHigh()) .traceId(parent.getTraceId()) .parent(parent.getSpanId())
private static Span sampledSpan(Span span, Sampler sampler) { if (!sampler.isSampled(span)) { // Copy everything, except set exportable to false return Span.builder() .begin(span.getBegin()) .traceIdHigh(span.getTraceIdHigh()) .traceId(span.getTraceId()) .spanId(span.getSpanId()) .name(span.getName()) .exportable(false) .build(); } return span; }
@Test public void testFromSleuthNotSampled() { Span sleuthSpan = Span.builder() .name("name") .traceIdHigh(12L) .traceId(22L) .spanId(23L) .exportable(false) .build(); assertSpanEquals(new OpenCensusSleuthSpan(sleuthSpan), sleuthSpan); }
@Test public void testFromSleuthSampled() { Span sleuthSpan = Span.builder() .name("name") .traceIdHigh(12L) .traceId(22L) .spanId(23L) .exportable(true) .build(); assertSpanEquals(new OpenCensusSleuthSpan(sleuthSpan), sleuthSpan); }
private static org.springframework.cloud.sleuth.Span createSleuthSpan( long tidHi, long tidLo, long sid, boolean exportable) { return org.springframework.cloud.sleuth.Span.builder() .name("name") .traceIdHigh(tidHi) .traceId(tidLo) .spanId(sid) .exportable(exportable) .build(); }