使用远程父母的跟踪ID和SPAN-ID创建opentelemetry上下文

发布于 2025-01-22 13:00:44 字数 96 浏览 0 评论 0 原文

我拥有支持打开跟踪的微服务,并将跟踪ID和SPAN-ID注入标题。其他微服务支持开放遥测。如何在第二微服务中使用Trace-ID和Span-ID创建父跨度?

谢谢,

I have micro service which support open tracing and that injecting trace-id and span-id in to header. Other micro service support open telemetry. how can I create parent span using trace-id and span-id in second micro service?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘年祭陌 2025-01-29 13:00:44

您可以使用 w3c跟踪上下文实现这一目标的规格。我们需要发送 traceparent (ex:00-8652a752a752089f33e2659dff289dfff28dff28d6833aa ff-fimnf-ff-fimnf-ff-inf-inf-ff-inf-ff-inf-inf-ff-inf-inf-inf-name )从生产者通过HTTP头号(或者您可以使用消费者中的Trace-ID和Span-ID创建它)。然后,我们可以提取远程上下文并使用TraceParent创建跨度。

这是消费者控制器。 TextMapgetter用于将跟踪数据映射到上下文。 ExtractModel只是一个自定义类。

  @GetMapping(value = "/second")
  public String sencondTest(@RequestHeader(value = "traceparent") String traceparent){
    try {
      Tracer tracer = openTelemetry.getTracer("cloud.events.second");

      TextMapGetter<ExtractModel> getter = new TextMapGetter<>() {
        @Override
        public String get(ExtractModel carrier, String key) {
          if (carrier.getHeaders().containsKey(key)) {
            return carrier.getHeaders().get(key);
          }
          return null;
        }

        @Override
        public Iterable<String> keys(ExtractModel carrier) {
          return carrier.getHeaders().keySet();
        }
      };

      ExtractModel model = new ExtractModel();
      model.addHeader("traceparent", traceparent);
      Context extractedContext = openTelemetry.getPropagators().getTextMapPropagator()
              .extract(Context.current(), model, getter);

      try (Scope scope = extractedContext.makeCurrent()) {
        // Automatically use the extracted SpanContext as parent.
        Span serverSpan = tracer.spanBuilder("CloudEvents Server")
                .setSpanKind(SpanKind.SERVER)
                .startSpan();
        try {

          Thread.sleep(150);
        } finally {
          serverSpan.end();
        }
      }

    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    }

    return "Server Received!";
  }

然后,当我们配置OpentElemetrysdk时,需要在上下文传播器中设置W3CtraceContextPropagator。

// Use W3C Propagator(to extract span from HTTP headers) since we use the W3C specifications
TextMapPropagator textMapPropagator = W3CTraceContextPropagator.getInstance();

OpenTelemetrySdk openTelemetrySdk = OpenTelemetrySdk.builder()
        .setTracerProvider(tracerProvider)
        .setPropagators(ContextPropagators.create(textMapPropagator))
        .buildAndRegisterGlobal();

这是我的客户extractModel类,

public class ExtractModel {

  private Map<String, String> headers;

  public void addHeader(String key, String value) {
    if (this.headers == null){
      headers = new HashMap<>();
    }
    headers.put(key, value);
  }

  public Map<String, String> getHeaders() {
    return headers;
  }

  public void setHeaders(Map<String, String> headers) {
    this.headers = headers;
  }
}

您可以在

You can use W3C Trace Context specifications to achieve this. We need to send traceparent(Ex: 00-8652a752089f33e2659dff28d683a18f-7359b90f4355cfd9-01) from producer via HTTP headres ( or you can create it using the trace-id and span-id in the consumer). Then we can extract the remote context and create the span with traceparent.

This is the consumer controller. TextMapGetter used to map that traceparent data to the Context. ExtractModel is just a custom class.

  @GetMapping(value = "/second")
  public String sencondTest(@RequestHeader(value = "traceparent") String traceparent){
    try {
      Tracer tracer = openTelemetry.getTracer("cloud.events.second");

      TextMapGetter<ExtractModel> getter = new TextMapGetter<>() {
        @Override
        public String get(ExtractModel carrier, String key) {
          if (carrier.getHeaders().containsKey(key)) {
            return carrier.getHeaders().get(key);
          }
          return null;
        }

        @Override
        public Iterable<String> keys(ExtractModel carrier) {
          return carrier.getHeaders().keySet();
        }
      };

      ExtractModel model = new ExtractModel();
      model.addHeader("traceparent", traceparent);
      Context extractedContext = openTelemetry.getPropagators().getTextMapPropagator()
              .extract(Context.current(), model, getter);

      try (Scope scope = extractedContext.makeCurrent()) {
        // Automatically use the extracted SpanContext as parent.
        Span serverSpan = tracer.spanBuilder("CloudEvents Server")
                .setSpanKind(SpanKind.SERVER)
                .startSpan();
        try {

          Thread.sleep(150);
        } finally {
          serverSpan.end();
        }
      }

    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    }

    return "Server Received!";
  }

Then when we configuring the OpenTelemetrySdk need to set W3CTraceContextPropagator in Context Propagators.

// Use W3C Propagator(to extract span from HTTP headers) since we use the W3C specifications
TextMapPropagator textMapPropagator = W3CTraceContextPropagator.getInstance();

OpenTelemetrySdk openTelemetrySdk = OpenTelemetrySdk.builder()
        .setTracerProvider(tracerProvider)
        .setPropagators(ContextPropagators.create(textMapPropagator))
        .buildAndRegisterGlobal();

Here is my customer ExtractModel class

public class ExtractModel {

  private Map<String, String> headers;

  public void addHeader(String key, String value) {
    if (this.headers == null){
      headers = new HashMap<>();
    }
    headers.put(key, value);
  }

  public Map<String, String> getHeaders() {
    return headers;
  }

  public void setHeaders(Map<String, String> headers) {
    this.headers = headers;
  }
}

You can find more details in the official documentation for manual instrumentation.

面犯桃花 2025-01-29 13:00:44

通常,如果标题中可用,则必须将SPAN-ID和TRACE-ID传播。您在微服务中得到的任何请求,请检查标题是否具有SPAN-ID和TRACE-ID。如果是,请提取它们并将其在您的服务中使用。
如果不存在,则创建一个新的并将其在您的服务中使用,并将其添加到从微服务中删除的请求。

Generally you have to propogate the span-id and trace-id if it is available in header. Any request you get in your microservice, check if the headers have span-id and trace-id in them. If yes,extract them and use them in your service.
If it is not present then you create a new one and use it in your service and also add it to requests that go out of your microservice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文