【问题标题】:Inject the OpenTracing's SpanContext in a Spring RestTemplate在 Spring RestTemplate 中注入 OpenTracing 的 SpanContext
【发布时间】:2019-10-14 21:44:51
【问题描述】:

我必须手动将 SpanContext 注入 Spring RestTemplate

由于与问题无关的原因,我不能只使用“opentracing-spring-web-starter”,因为这会与我设置的其他启动器发生冲突并导致项目崩溃。

我找到了与将上下文注入 HTTP 请求相关的代码的 sn-p,但我找不到与 RestTemplate 相关的任何内容:

Tags.SPAN_KIND.set(tracer.activeSpan(), Tags.SPAN_KIND_CLIENT);
Tags.HTTP_METHOD.set(tracer.activeSpan(), "GET");
Tags.HTTP_URL.set(tracer.activeSpan(), url.toString());
tracer.inject(tracer.activeSpan().context(), Format.Builtin.HTTP_HEADERS, new RequestBuilderCarrier(requestBuilder));

任何帮助将不胜感激,在此先感谢!

【问题讨论】:

    标签: spring-boot resttemplate opentracing jaeger


    【解决方案1】:

    您可以在java-spring-web 库中找到代码:

        @Override
        public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] body,
                                            ClientHttpRequestExecution execution) throws IOException {
            ClientHttpResponse httpResponse;
    
            Span span = tracer.buildSpan(httpRequest.getMethod().toString())
                    .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
                    .start();
            tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS,
                    new HttpHeadersCarrier(httpRequest.getHeaders()));
            ...
        }
    

    参考:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      • 1970-01-01
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 2018-02-23
      • 2019-11-21
      相关资源
      最近更新 更多