io.opentracing.Tracer.extract()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(207)

本文整理了Java中io.opentracing.Tracer.extract()方法的一些代码示例,展示了Tracer.extract()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Tracer.extract()方法的具体详情如下:
包路径:io.opentracing.Tracer
类名称:Tracer
方法名:extract

Tracer.extract介绍

[英]Extract a SpanContext from a carrier of a given type, presumably after propagation across a process boundary.

Example:

Tracer tracer = ... 
TextMap httpHeadersCarrier = new AnHttpHeaderCarrier(httpRequest); 
SpanContext spanCtx = tracer.extract(Format.Builtin.HTTP_HEADERS, httpHeadersCarrier); 
... = tracer.buildSpan('...').asChildOf(spanCtx).startActive();

If the span serialized state is invalid (corrupt, wrong version, etc) inside the carrier this will result in an IllegalArgumentException.
[中]从给定类型的“载体”中提取上下文,可能是在跨进程边界传播之后。
例子:

Tracer tracer = ... 
TextMap httpHeadersCarrier = new AnHttpHeaderCarrier(httpRequest); 
SpanContext spanCtx = tracer.extract(Format.Builtin.HTTP_HEADERS, httpHeadersCarrier); 
... = tracer.buildSpan('...').asChildOf(spanCtx).startActive();

如果运营商内部的span序列化状态无效(损坏、版本错误等),这将导致IllegalArgumentException。

代码示例

代码示例来源:origin: stagemonitor/stagemonitor

@Override
  public <C> SpanContext extract(Format<C> format, C carrier) {
    return tracer.extract(format, carrier);
  }
}

代码示例来源:origin: stagemonitor/stagemonitor

@Override
public <C> SpanContext extract(Format<C> format, C carrier) {
  try {
    return delegate.extract(format, carrier);
  } catch (NumberFormatException e) {
    logger.error("error parsing span id, possible XSS attack: " + e.getMessage(), e);
    return null;
  }
}

代码示例来源:origin: qunarcorp/qmq

public static SpanContext extract(Message message, Tracer tracer) {
  return tracer.extract(Format.Builtin.TEXT_MAP, new QmqMessageExtractAdapter(message));
}

代码示例来源:origin: weibocom/motan

protected Span extractTraceInfo(Request request, Tracer tracer) {
  String operationName = buildOperationName(request);
  SpanBuilder span = tracer.buildSpan(operationName);
  try {
    SpanContext spanContext = tracer.extract(Format.Builtin.TEXT_MAP, new TextMapExtractAdapter(request.getAttachments()));
    if (spanContext != null) {
      span.asChildOf(spanContext);
    }
  } catch (Exception e) {
    span.withTag("Error", "extract from request fail, error msg:" + e.getMessage());
  }
  return span.start();
}

代码示例来源:origin: oracle/helidon

private Span createRequestSpan(Tracer tracer, BareRequest request) {
  Tracer.SpanBuilder spanBuilder = tracer.buildSpan("HTTP Request")
      .withTag(Tags.COMPONENT.getKey(), "helidon-webserver")
      .withTag(Tags.HTTP_METHOD.getKey(), request.method().name())
      .withTag(Tags.HTTP_URL.getKey(), request.uri().toString());
  Map<String, String> headersMap = request.headers()
                      .entrySet()
                      .stream()
                      .filter(entry -> !entry.getValue().isEmpty())
                      .collect(Collectors.toMap(Map.Entry::getKey,
                                   entry -> entry.getValue().get(0)));
  SpanContext spanContext = tracer.extract(Format.Builtin.HTTP_HEADERS, new TextMapExtractAdapter(headersMap));
  if (spanContext != null) {
    spanBuilder.asChildOf(spanContext);
  }
  return spanBuilder.start();
}

代码示例来源:origin: fengjiachun/Jupiter

private Span extractContext(Tracer tracer, JRequest request) {
    MessageWrapper msg = request.message();
    Tracer.SpanBuilder spanBuilder = tracer.buildSpan(msg != null ? msg.getOperationName() : "null");
    try {
      SpanContext spanContext = tracer.extract(
          Format.Builtin.TEXT_MAP, new TextMapExtractAdapter(request.getAttachments()));
      if (spanContext != null) {
        spanBuilder.asChildOf(spanContext);
      }
    } catch (Throwable t) {
      spanBuilder.withTag("Error", "extract from request failed: " + t.getMessage());
    }
    return spanBuilder.start();
  }
}

代码示例来源:origin: fengjiachun/Jupiter

private Span extractContext(Tracer tracer, JRequest request) {
    MessageWrapper msg = request.message();
    Tracer.SpanBuilder spanBuilder = tracer.buildSpan(msg != null ? msg.getOperationName() : "null");
    try {
      SpanContext spanContext = tracer.extract(
          Format.Builtin.TEXT_MAP, new TextMapExtractAdapter(request.getAttachments()));
      if (spanContext != null) {
        spanBuilder.asChildOf(spanContext);
      }
    } catch (Throwable t) {
      spanBuilder.withTag("Error", "extract from request failed: " + t.getMessage());
    }
    return spanBuilder.start();
  }
}

代码示例来源:origin: jersey/jersey

private Span handleRequestStart(ContainerRequest request) {
  final Map<String, String> mappedHeaders = request
      .getHeaders()
      .entrySet()
      .stream()
      .collect(Collectors.toMap(
          Map.Entry::getKey,
          (entry) -> OpenTracingUtils.formatList(entry.getValue())));
  final SpanContext extractedContext =
      globalTracer.extract(Format.Builtin.HTTP_HEADERS, new TextMapExtractAdapter(mappedHeaders));
  Tracer.SpanBuilder spanBuilder = globalTracer
      .buildSpan(OpenTracingFeature.DEFAULT_REQUEST_SPAN_NAME)
      .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER)
      .withTag(Tags.HTTP_METHOD.getKey(), request.getMethod())
      .withTag(Tags.HTTP_URL.getKey(), request.getRequestUri().toASCIIString())
      .withTag(LocalizationMessages.OPENTRACING_TAG_REQUEST_HEADERS(),
          OpenTracingUtils.headersAsString(request.getHeaders()))
      .withTag(LocalizationMessages.OPENTRACING_TAG_HAS_REQUEST_ENTITY(), request.hasEntity());
  if (extractedContext != null) {
    spanBuilder = spanBuilder.asChildOf(extractedContext);
  }
  final Span span = spanBuilder.startManual();
  request.setProperty(OpenTracingFeature.SPAN_CONTEXT_PROPERTY, span);
  span.log(LocalizationMessages.OPENTRACING_LOG_REQUEST_STARTED());
  return span;
}

代码示例来源:origin: stagemonitor/stagemonitor

io.opentracing.SpanContext spanCtx = tracer.extract(Format.Builtin.HTTP_HEADERS, new HttpServletRequestTextMapExtractAdapter(httpServletRequest));
Tracer.SpanBuilder spanBuilder = tracer.buildSpan(getRequestName())
    .asChildOf(spanCtx)

代码示例来源:origin: io.opentracing.contrib/opentracing-api-extensions-tracer

@Override
public <C> SpanContext extract(Format<C> format, C carrier) {
  if (wrappedTracer != null) {
    return wrappedTracer.extract(format, carrier);
  }
  return null;
}

代码示例来源:origin: sixt/ja-micro

protected Span getSpan(String methodName, Map<String, String> headers, OrangeContext context) {
  Span span = null;
  if (tracer != null) {
    SpanContext spanContext = tracer.extract(Format.Builtin.HTTP_HEADERS, new TextMapExtractAdapter(headers));
    if (spanContext != null) {
      span = tracer.buildSpan(methodName).asChildOf(spanContext).start();
    } else {
      span = tracer.buildSpan(methodName).start();
    }
    span.setTag("correlation_id", context.getCorrelationId());
    span.setTag("rpc.call", methodName);
    Tags.SPAN_KIND.set(span, Tags.SPAN_KIND_SERVER);
    Tags.PEER_SERVICE.set(span, context.getRpcOriginService());
    context.setTracingContext(span.context());
  }
  return span;
}

代码示例来源:origin: eclipse/hono

/**
   * Extracts a {@code SpanContext} out of the delivery annotations of the given {@code Message}.
   * 
   * @param message The AMQP message.
   * @return The extracted {@code SpanContext} (may be {@code null}).
   * @throws NullPointerException if the message is {@code null}.
   */
  protected final SpanContext extractSpanContext(final Message message) {
    return tracer.extract(Format.Builtin.TEXT_MAP, new MessageAnnotationsExtractAdapter(message));
  }
}

代码示例来源:origin: io.opentracing.contrib/opentracing-kafka-client

/**
 * Extract Span Context from Consumer record headers
 *
 * @param headers Consumer record headers
 * @return span context
 */
public static SpanContext extractSpanContext(Headers headers, Tracer tracer) {
 return tracer
   .extract(Format.Builtin.TEXT_MAP, new HeadersMapExtractAdapter(headers, true));
}

代码示例来源:origin: opentracing-contrib/java-kafka-client

/**
 * Extract Span Context from record headers
 *
 * @param headers record headers
 * @return span context
 */
static SpanContext extract(Headers headers, Tracer tracer) {
 return tracer
   .extract(Format.Builtin.TEXT_MAP, new HeadersMapExtractAdapter(headers, false));
}

代码示例来源:origin: org.ballerinalang/ballerina-core

private Object extractSpanContext(Map<String, String> traceContext, String serviceName) {
  SpanContext spanContext = null;
  Tracer tracer = tracerStore.getTracer(serviceName);
  if (tracer != null) {
    spanContext = tracer.extract(Format.Builtin.HTTP_HEADERS, new TextMapExtractAdapter(traceContext));
  }
  return spanContext;
}

代码示例来源:origin: org.wso2.carbon.apimgt/org.wso2.carbon.apimgt.tracing

/**
 * Extract the tracer specific information from headerMap
 *
 * @param tracer
 * @param headerMap
 * @return a TracingSpan object
 * */
public static TracingSpan extract(TracingTracer tracer, Map<String, String> headerMap) {
  return new TracingSpan(tracer.getTracingTracer().extract(Format.Builtin.HTTP_HEADERS,
      new TextMapExtractAdapter(headerMap)));
}

代码示例来源:origin: yangfuhai/jboot

protected Span extractTraceInfo(Request request, Tracer tracer) {
  String operationName = buildOperationName(request);
  Tracer.SpanBuilder span = tracer.buildSpan(operationName);
  try {
    SpanContext spanContext = tracer.extract(Format.Builtin.TEXT_MAP, new TextMapExtractAdapter(request.getAttachments()));
    if (spanContext != null) {
      span.asChildOf(spanContext);
    }
  } catch (Exception e) {
    span.withTag("Error", "extract from request fail, error msg:" + e.getMessage());
  }
  return span.startManual();
}

代码示例来源:origin: yangfuhai/jboot

protected Span extractTraceInfo(Tracer tracer, Invoker<?> invoker, Invocation inv) {
  String operationName = JbootDubboTracingFilterKits.buildOperationName(invoker, inv);
  Tracer.SpanBuilder span = tracer.buildSpan(operationName);
  try {
    SpanContext spanContext = tracer.extract(Format.Builtin.TEXT_MAP, new TextMapExtractAdapter(inv.getAttachments()));
    if (spanContext != null) {
      span.asChildOf(spanContext);
    }
  } catch (Exception e) {
    span.withTag("Error", "extract from request fail, error msg:" + e.getMessage());
  }
  return span.startManual();
}

代码示例来源:origin: teiid/teiid

protected SpanContext extractSpanContext(String spanContextJson) {
  try {
    JSONParser parser = new JSONParser();
    SimpleContentHandler sch = new SimpleContentHandler();
    parser.parse(spanContextJson, sch);
    Map<String, String> result = (Map<String, String>) sch.getResult();
    return getTracer().extract(Builtin.TEXT_MAP, new TextMapExtractAdapter(result));
  } catch (IllegalArgumentException | ClassCastException | ParseException e) {
    LogManager.logDetail(LogConstants.CTX_DQP, e, "Could not extract the span context"); //$NON-NLS-1$
    return null;
  }
}

代码示例来源:origin: org.teiid/teiid-engine

protected SpanContext extractSpanContext(String spanContextJson) {
  try {
    JSONParser parser = new JSONParser();
    SimpleContentHandler sch = new SimpleContentHandler();
    parser.parse(spanContextJson, sch);
    Map<String, String> result = (Map<String, String>) sch.getResult();
    return getTracer().extract(Builtin.TEXT_MAP, new TextMapExtractAdapter(result));
  } catch (IllegalArgumentException | ClassCastException | ParseException e) {
    LogManager.logDetail(LogConstants.CTX_DQP, e, "Could not extract the span context"); //$NON-NLS-1$
    return null;
  }
}

相关文章