protected void attachTraceInfo(Tracer tracer, Span span, final Request request) { if (span == null) { setHeader(request, Span.SAMPLED_NAME, Span.SPAN_NOT_SAMPLED); return; } setHeader(request, TraceRequestAttributes.HANDLED_SPAN_REQUEST_ATTR, "true"); setHeader(request, Span.SPAN_ID_NAME, Span.idToHex(span.getSpanId())); setHeader(request, Span.TRACE_ID_NAME, span.traceIdString()); setHeader(request, Span.SPAN_NAME_NAME, span.getName()); setHeader(request, Span.SAMPLED_NAME, span.isExportable() ? Span.SPAN_SAMPLED : Span.SPAN_NOT_SAMPLED); setHeader(request, Span.PARENT_ID_NAME,Span.idToHex(getParentId(span))); setHeader(request, Span.PROCESS_ID_NAME, span.getProcessId()); if(span.getSavedSpan()!=null && span.getSavedSpan().tags()!=null){ for (Entry<String, String> stringStringEntry : span.getSavedSpan().tags().entrySet()) { setHeader(request, stringStringEntry.getKey(), stringStringEntry.getValue()); } } /*spanInjector.inject(span,null); tracer.inject(span.context(), Format.Builtin.TEXT_MAP, new TextMap() { @Override public void put(String key, String value) { request.setAttachment(key, value); } @Override public Iterator<Entry<String, String>> iterator() { throw new UnsupportedOperationException("TextMapInjectAdapter should only be used with Tracer.inject()"); } });*/ }
@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); }
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; }
@SuppressWarnings("deprecation") private static Span createContinuedSpan(Span span, /*@Nullable*/ Span saved) { if (saved == null && span.getSavedSpan() != null) { saved = span.getSavedSpan(); } return new Span(span, saved); }
@Override public void addTag(String key, String value) { Span s = getCurrentSpan(); if (s != null && s.isExportable()) { s.tag(key, value); } }
@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()); }
setInstanceIdIfPresent(zipkinSpan, Span.INSTANCEID); zipkinSpan.shared(convertedSpan.isShared()); zipkinSpan.timestamp(convertedSpan.getBegin() * 1000L); if (!convertedSpan.isRunning()) { // duration is authoritative, only write when the span stopped zipkinSpan.duration(calculateDurationInMicros(convertedSpan)); zipkinSpan.traceId(convertedSpan.traceIdString()); if (convertedSpan.getParents().size() > 0) { if (convertedSpan.getParents().size() > 1) { log.error("Zipkin doesn't support spans with multiple parents. Omitting " + "other parents for " + convertedSpan); zipkinSpan.parentId(Span.idToHex(convertedSpan.getParents().get(0))); zipkinSpan.id(Span.idToHex(convertedSpan.getSpanId())); if (StringUtils.hasText(convertedSpan.getName())) { zipkinSpan.name(convertedSpan.getName());
@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); } }
if (parent == null) { Span span = Span.builder() .name(shortenedName) .traceIdHigh(this.traceId128 ? createTraceIdHigh() : 0L) Span.builder() .name(shortenedName) .traceIdHigh(parent.getTraceIdHigh()) .traceId(parent.getTraceId()) .parent(parent.getSpanId()) .spanId(id) .processId(parent.getProcessId()) .savedSpan(parent) .exportable(parent.isExportable()) .baggage(parent.getBaggage()) .build(); this.spanLogger.logStartedSpan(parent, span);
return null; final Span savedSpan = span.getSavedSpan(); Span current = OpenCensusSleuthSpanContextHolder.getCurrentSpan(); if (current == null || !span.equals(current)) { ExceptionUtils.warn( "Tried to close span but it is not the current span: " + current); } else { span.stop(); if (savedSpan != null && span.getParents().contains(savedSpan.getSpanId())) { this.spanReporter.report(span); this.spanLogger.logStoppedSpan(savedSpan, span); } else { if (!span.isRemote()) { this.spanReporter.report(span); this.spanLogger.logStoppedSpan(null, span);
private void rewriteIds(Span span, TraceSpan.SpanKind kind, TraceSpan.Builder builder) { if (span.getParents().size() > 0) { if (span.getParents().size() > 1) { LOGGER.error("Stackdriver Trace doesn't support spans with multiple parents. Omitting " + "other parents for " + span); parentId = span.getParents().get(0); builder.setSpanId(rewriteId(span.getSpanId())); builder.setSpanId(span.getSpanId()); if (!span.isRemote()) { builder.setParentSpanId(rewriteId(span.getSpanId()));
if (parent != null) { span = tracer.createSpan(spanName, parent); if (parent.isRemote()) { parent.logEvent(Span.SERVER_RECV); span = tracer.createSpan(spanName); span.logEvent(Span.SERVER_RECV); span.tag("error", e.getMessage()); throw e; } finally { if (span != null) { if (span.hasSavedSpan()) { Span parent = span.getSavedSpan(); if (parent.isRemote()) { parent.logEvent(Span.SERVER_SEND); parent.stop(); spanReporter.report(parent); span.logEvent(Span.SERVER_SEND);
private void debug() { Span span = this.spanAccessor.getCurrentSpan(); this.log.info(String.format("traceId: %s, spanId: %s", span.getTraceId(), span.getSpanId())); } }
TraceLogWrap.request("MQ", name, ""); Span span = tracer.createSpan(name); Long parentId = !span.getParents().isEmpty() ? span.getParents().get(0) : null; HashMap<String, Object> hashMap = new HashMap<String, Object>() {{ put(TraceMessageHeaders.TRACE_ID_NAME, Span.idToHex(span.getTraceId())); tracer.createSpan(name); } else { Span.SpanBuilder spanBuilder = Span.builder(); 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)); String processId = header.containsKey(TraceMessageHeaders.PROCESS_ID_NAME) ? String.valueOf(header.get(TraceMessageHeaders.PROCESS_ID_NAME)) : null; String spanName = header.containsKey(TraceMessageHeaders.SPAN_NAME_NAME) ? String.valueOf(header.get(TraceMessageHeaders.SPAN_NAME_NAME)) : null; spanBuilder.parent(Span.hexToId(parentId));
span.logEvent(Span.CLIENT_SEND); if(span.getSavedSpan()!=null && span.getSavedSpan().tags()!=null){ for (Entry<String, String> stringStringEntry : span.getSavedSpan().tags().entrySet()) { setHeader(request, stringStringEntry.getKey(), stringStringEntry.getValue()); span.tag("requestId", String.valueOf(request.getRequestId())); span.tag(Span.SPAN_LOCAL_COMPONENT_TAG_NAME, MOTAN_TAG); attachTraceInfo(tracer, span, request); return process(tracer,caller, request, span);
protected TraceSpan convert(Span span) { Span adjustedSpan = span; for (SpanAdjuster adjuster : this.spanAdjusters) { adjustedSpan = adjuster.adjust(adjustedSpan); } TraceSpan.Builder builder = TraceSpan.newBuilder(); // Set name if (StringUtils.hasText(adjustedSpan.getName())) { builder.setName(adjustedSpan.getName()); } TraceSpan.SpanKind kind = getKind(adjustedSpan); builder.setKind(kind); builder.setSpanId(adjustedSpan.getSpanId()); rewriteIds(adjustedSpan, kind, builder); writeStartEndTime(adjustedSpan, builder); builder.putAllLabels(this.labelExtractor.extract(adjustedSpan, builder.getKind(), this.instanceId)); return builder.build(); }
@Override public String getRequestId() { return String.valueOf(spanAccessor.getCurrentSpan().getSpanId()); } }
public Map<String, String> extract(Span span, TraceSpan.SpanKind kind, String instanceId) { Map<String, String> labels = new HashMap<>(); for (Map.Entry<String, String> tag : span.tags().entrySet()) { labels.put(label(tag.getKey()), tag.getValue()); } for (Log log : span.logs()) { labels.put(label(log.getEvent()), formatTimestamp(log.getTimestamp())); } if (span.tags().containsKey(Span.SPAN_PEER_SERVICE_TAG_NAME)) { labels.put("/component", span.tags().get(Span.SPAN_PEER_SERVICE_TAG_NAME)); } if (span.getParents() == null || span.getParents().isEmpty()) { labels.put("/agent", this.agentName); } if ((kind == TraceSpan.SpanKind.RPC_CLIENT || kind == TraceSpan.SpanKind.RPC_SERVER) && StringUtils.hasText(instanceId)) { if (StringUtils.hasText(instanceId)) { labels.put(label(Span.INSTANCEID), instanceId); } } return labels; }
private Long getParentId(Span span) { return !span.getParents().isEmpty() ? span.getParents().get(0) : null; }
protected Span extractTraceInfo(Request request, Tracer tracer) { Span parentSpan = tracer.getCurrentSpan(); if(parentSpan==null){ Span.SpanBuilder spanBuilder = Span.builder(); if(request.getAttachments().get(TraceMessageHeaders.TRACE_ID_NAME)!=null && !"".equals(request.getAttachments().get(TraceMessageHeaders.TRACE_ID_NAME))){ spanBuilder.traceId(Span.hexToId(request.getAttachments().get(TraceMessageHeaders.TRACE_ID_NAME))); spanBuilder.spanId(Span.hexToId(request.getAttachments().get(TraceMessageHeaders.SPAN_ID_NAME))); spanBuilder.exportable(Span.SPAN_SAMPLED.equals(request.getAttachments().get(TraceMessageHeaders.SAMPLED_NAME))); spanBuilder.processId(request.getAttachments().get(TraceMessageHeaders.PROCESS_ID_NAME)); spanBuilder.parent(Span.hexToId(request.getAttachments().get(TraceMessageHeaders.PARENT_ID_NAME))); spanBuilder.name(request.getAttachments().get(TraceMessageHeaders.SPAN_NAME_NAME)); spanBuilder.remote(true); }else if(request.getAttachments().get(Span.TRACE_ID_NAME)!=null && !"".equals(request.getAttachments().get(Span.TRACE_ID_NAME))){ spanBuilder.traceId(Span.hexToId(request.getAttachments().get(Span.TRACE_ID_NAME))); spanBuilder.spanId(Span.hexToId(request.getAttachments().get(Span.SPAN_ID_NAME))); spanBuilder.exportable(Span.SPAN_SAMPLED.equals(request.getAttachments().get(Span.SAMPLED_NAME))); spanBuilder.processId(request.getAttachments().get(Span.PROCESS_ID_NAME)); if(request.getAttachments().get(Span.PARENT_ID_NAME)!=null){ spanBuilder.parent(Span.hexToId(request.getAttachments().get(Span.PARENT_ID_NAME))); newSpan.tag("motan_method",operationName); newSpan.tag("requestId", String.valueOf(request.getRequestId())); newSpan.tag(Span.SPAN_LOCAL_COMPONENT_TAG_NAME, MOTAN_TAG);