コード例 #1
0
  // VisibleForTesting
  static zipkin.Span convert(Span span, Host host) {
    Builder zipkinSpan = zipkin.Span.builder();

    Endpoint ep =
        Endpoint.create(host.getServiceName(), host.getIpv4(), host.getPort().shortValue());

    // A zipkin span without any annotations cannot be queried, add special "lc" to
    // avoid that.
    if (notClientOrServer(span)) {
      ensureLocalComponent(span, zipkinSpan, ep);
    }
    ZipkinMessageListener.addZipkinAnnotations(zipkinSpan, span, ep);
    ZipkinMessageListener.addZipkinBinaryAnnotations(zipkinSpan, span, ep);
    if (hasClientSend(span)) {
      ensureServerAddr(span, zipkinSpan, ep);
    }
    zipkinSpan.timestamp(span.getBegin() * 1000);
    zipkinSpan.duration(span.getAccumulatedMillis() * 1000);
    zipkinSpan.traceId(span.getTraceId());
    if (span.getParents().size() > 0) {
      if (span.getParents().size() > 1) {
        log.debug(
            "zipkin doesn't support spans with multiple parents.  Omitting "
                + "other parents for "
                + span);
      }
      zipkinSpan.parentId(span.getParents().get(0));
    }
    zipkinSpan.id(span.getSpanId());
    if (StringUtils.hasText(span.getName())) {
      zipkinSpan.name(span.getName());
    }
    return zipkinSpan.build();
  }
コード例 #2
0
  public static Message<?> addSpanHeaders(TraceKeys traceKeys, Message<?> message, Span span) {
    if (span == null) {
      if (!message.getHeaders().containsKey(Span.NOT_SAMPLED_NAME)) {
        return MessageBuilder.fromMessage(message).setHeader(Span.NOT_SAMPLED_NAME, "").build();
      }
      return message;
    }

    Map<String, String> headers = new HashMap<>();
    addHeader(headers, Span.TRACE_ID_NAME, Span.toHex(span.getTraceId()));
    addHeader(headers, Span.SPAN_ID_NAME, Span.toHex(span.getSpanId()));

    if (span.isExportable()) {
      addAnnotations(traceKeys, message, span);
      Long parentId = getFirst(span.getParents());
      if (parentId != null) {
        addHeader(headers, Span.PARENT_ID_NAME, Span.toHex(parentId));
      }
      addHeader(headers, Span.SPAN_NAME_NAME, span.getName());
      addHeader(headers, Span.PROCESS_ID_NAME, span.getProcessId());
    } else {
      addHeader(headers, Span.NOT_SAMPLED_NAME, "");
    }
    return MessageBuilder.fromMessage(message).copyHeaders(headers).build();
  }
コード例 #3
0
 private Map<String, Collection<String>> headersWithTraceId(
     Map<String, Collection<String>> headers) {
   Map<String, Collection<String>> newHeaders = new HashMap<>();
   newHeaders.putAll(headers);
   Span span = getCurrentSpan();
   if (span == null) {
     setHeader(newHeaders, Trace.NOT_SAMPLED_NAME, "");
     return newHeaders;
   }
   if (span.getSpanId() == null) {
     setHeader(newHeaders, Trace.TRACE_ID_NAME, span.getTraceId());
     setHeader(newHeaders, Trace.NOT_SAMPLED_NAME, "");
     return newHeaders;
   }
   setHeader(newHeaders, Trace.TRACE_ID_NAME, span.getTraceId());
   setHeader(newHeaders, Trace.SPAN_ID_NAME, span.getSpanId());
   setHeader(newHeaders, Trace.PARENT_ID_NAME, getParentId(span));
   return newHeaders;
 }