@SuppressWarnings("deprecation") private static SpanContext fromSleuthSpan(org.springframework.cloud.sleuth.Span span) { return SpanContext.create( TraceId.fromBytes( ByteBuffer.allocate(TraceId.SIZE) .putLong(span.getTraceIdHigh()) .putLong(span.getTraceId()) .array()), SpanId.fromBytes(ByteBuffer.allocate(SpanId.SIZE).putLong(span.getSpanId()).array()), Boolean.TRUE.equals(span.isExportable()) ? sampledOptions : notSampledOptions); } }
private void debug() { Span span = this.spanAccessor.getCurrentSpan(); this.log.info(String.format("traceId: %s, spanId: %s", span.getTraceId(), span.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; }
@Around("anyExchangeRestOperationsMethod()") public Object wrapWithCorrelationIdForRestOperations(ProceedingJoinPoint pjp) throws Throwable { Span span = getSpanOrCreateOne(); log.debug("Wrapping RestTemplate call with correlation id [" + span.getTraceId() + "]"); HttpEntity httpEntity = (HttpEntity) pjp.getArgs()[HTTP_ENTITY_PARAM_INDEX]; HttpEntity newHttpEntity = createNewHttpEntity(httpEntity, span); List<Object> newArgs = modifyHttpEntityInMethodArguments(pjp, newHttpEntity); return pjp.proceed(newArgs.toArray()); }
private static void assertSpanEquals( Span span, org.springframework.cloud.sleuth.Span sleuthSpan) { assertThat(Long.parseLong(span.getContext().getTraceId().toLowerBase16().substring(0, 16), 16)) .isEqualTo(sleuthSpan.getTraceIdHigh()); assertThat(Long.parseLong(span.getContext().getTraceId().toLowerBase16().substring(16, 32), 16)) .isEqualTo(sleuthSpan.getTraceId()); assertThat(Long.parseLong(span.getContext().getSpanId().toLowerBase16(), 16)) .isEqualTo(sleuthSpan.getSpanId()); assertThat(span.getContext().getTraceOptions().isSampled()) .isEqualTo(sleuthSpan.isExportable()); } }
/** * Process id is id of process uniting several operations, logically defining some complete execution * including several service calls * @return id of process */ @Override public UUID getProcessId() { ContextVerifier.assertWithinContext(); return UUID.fromString(TraceContextHolder.getCurrentSpan().getTraceId()); }
private static final void assertSpanEquals(io.opencensus.trace.Span span, Span sleuthSpan) { assertThat(span.getContext().isValid()).isTrue(); assertThat(Long.parseLong(span.getContext().getTraceId().toLowerBase16().substring(0, 16), 16)) .isEqualTo(sleuthSpan.getTraceIdHigh()); assertThat(Long.parseLong(span.getContext().getTraceId().toLowerBase16().substring(16, 32), 16)) .isEqualTo(sleuthSpan.getTraceId()); assertThat(Long.parseLong(span.getContext().getSpanId().toLowerBase16(), 16)) .isEqualTo(sleuthSpan.getSpanId()); assertThat(span.getContext().getTraceOptions().isSampled()) .isEqualTo(sleuthSpan.isExportable()); } }
/** * Process id is id of process uniting several operations, logically defining some complete execution * including several service calls * @return id of process if operation is part of process, null otherwise */ @Override public UUID getProcessId() { ContextVerifier.assertWithinContext(); if(!isProcess()) { return null; } return UUID.fromString(TraceContextHolder.getCurrentSpan().getTraceId()); }
.name(shortenedName) .traceIdHigh(parent.getTraceIdHigh()) .traceId(parent.getTraceId()) .parent(parent.getSpanId()) .spanId(id)
@Override public void inject(Span span, RpcContext carrier) { Map<String, String> attachments = carrier.getAttachments(); if (span.getTraceId() != 0) { attachments.put(Span.TRACE_ID_NAME, Span.idToHex(span.getTraceId())); } if (span.getSpanId() != 0) { attachments.put(Span.SPAN_ID_NAME, Span.idToHex(span.getSpanId())); } attachments.put(Span.SAMPLED_NAME, span.isExportable() ? Span.SPAN_SAMPLED : Span.SPAN_NOT_SAMPLED); attachments.put(Span.SPAN_NAME_NAME, span.getName()); Long parentId = getParentId(span); if (parentId != null && parentId != 0) { attachments.put(Span.PARENT_ID_NAME, Span.idToHex(parentId)); } attachments.put(Span.PROCESS_ID_NAME, span.getProcessId()); }
@SuppressWarnings("unchecked") private HttpEntity createNewHttpEntity(HttpEntity httpEntity, Span span) { HttpHeaders newHttpHeaders = new HttpHeaders(); newHttpHeaders.putAll(httpEntity.getHeaders()); if (span != null) { addHeaderIfPresent(newHttpHeaders, Span.SPAN_ID_NAME, String.valueOf(span.getSpanId())); addHeaderIfPresent(newHttpHeaders, Span.TRACE_ID_NAME, String.valueOf(span.getTraceId())); addHeaderIfPresent(newHttpHeaders, OLD_CORRELATION_ID_HEADER, String.valueOf(span.getTraceId())); addHeaderIfPresent(newHttpHeaders, Span.SPAN_NAME_NAME, span.getName()); addHeaderIfPresent(newHttpHeaders, Span.PARENT_ID_NAME, getFirst(span.getParents())); addHeaderIfPresent(newHttpHeaders, Span.PROCESS_ID_NAME, span.getProcessId()); } return new HttpEntity(httpEntity.getBody(), newHttpHeaders); }