微服务全链路跟踪:jaeger集成istio,并兼容uber-trace-id与b3 - lipeng的个人空间 - OSCHINA

标签: | 发表时间:2020-05-03 08:20 | 作者:
出处:https://my.oschina.net

微服务全链路跟踪:grpc集成zipkin

微服务全链路跟踪:grpc集成jaeger

微服务全链路跟踪:springcloud集成jaeger

微服务全链路跟踪:jaeger集成istio,并兼容uber-trace-id与b3

微服务全链路跟踪:jaeger集成hystrix

公司有自己的一套基于k8s的paas系统,并且集成了istio,这里主要是想讲解下springcloud服务如何集成istio

jaeger跨进程传递

在基于HTTP协议的分布式调用中,通常会使用HTTP Header来传递SpanContext的内容。常见的Wire Protocol包含Zipkin使用的b3 HTTP header,Jaeger使用的uber-trace-id HTTP Header,LightStep使用的"x-ot-span-context" HTTP Header等。Istio1.0支持b3 header和x-ot-span-context header,可以和Zipkin,Jaeger及LightStep对接;istio1.4以上支持uber-trace-id,请参考github官方说明: https://github.com/istio/istio/issues/12400现在我们我们接入非istio服务都是默认配置uber-trace-id

uber-trace-id

在这里插入图片描述

图中可以看到其中traceId、spanId等字段都拼接到一个key中了

b3

istio的b3头详情可以参考: https://www.envoyproxy.io/docs/envoy/latest/configuration/http/http_conn_man/headers#x-request-idb3并没有将字段都拼接,而是单个传递,下面是案例:

      X-B3-TraceId:427fde2dc7edb084
X-B3-ParentSpanId:427fde2dc7edb084
X-B3-SpanId:827e270489aafbd7
X-B3-Sampled:1

变更jaeger传输为b3

如果需要集成istio的jaeger,则需要将传输方式修改为b3 微服务全链路跟踪:springcloud集成jaeger该章中已经描述了如何集成jaeger,这里只需要修改一个配置enable-b3-propagation,如下

      opentracing:
  jaeger:
    enable-b3-propagation: true// 默认为false
    udp-sender:
      host: localhost
      port: 6831
    remote-reporter:
      flush-interval: 1000
      max-queue-size: 5000
    log-spans: true
    probabilistic-sampler:
      sampling-rate: 1

这样springboot服务就可以与istio中的jaeger信息串起来形成完整全链路。 在这里插入图片描述

兼容uber-trace-id与b3

现在遇到了另外一个问题,公司已经很早就搭建了一套全链路jaeger,并且已经接入了大部分系统,采用的是默认的header传输,即:uber-trace-id 而下游有很多paas内部系统都是非java的不方便接入jaeger,只是注入了istio,并自动注入了jaeger-agent,这里使用的是b3头传输,这就导致了部分链路上下游无法串联起来。而如果需要统一传输方式暂不现实,首先如果都改成b3,则需要上游很多已接入的系统修改配置为b3,如果是都改成uber-trace-id,istio当前版本不支持,如果需要升级istio,则需要升级kubernetes,风险比较大,所以这里根据实际情况先采用集成两种头,即上游都是uber-trace-id,到中间层服务时手动注入b3相关头如下。

grpc注入b3头

这里需要使用grpc的拦截器

      import com.google.common.collect.ImmutableMap;
import io.grpc.*;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.propagation.Format;
import io.opentracing.propagation.TextMap;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/** 
 * An intercepter that applies tracing via OpenTracing to all client requests. 
 */
@Slf4j
public class ClientTracingInterceptor implements ClientInterceptor {

    private final Tracer tracer;
    private final OperationNameConstructor operationNameConstructor;
    private final boolean streaming;
    private final boolean verbose;
    private final Set<clientrequestattribute> tracedAttributes;
    private final ActiveSpanSource activeSpanSource;
    private final  Metadata.Key<string> b3TraceIdKey = Metadata.Key.of("X-B3-TraceId", Metadata.ASCII_STRING_MARSHALLER);
    private final  Metadata.Key<string> b3SpanIdKey = Metadata.Key.of("X-B3-SpanId", Metadata.ASCII_STRING_MARSHALLER);
    private final  Metadata.Key<string> b3ParentSpanIdKey = Metadata.Key.of("X-B3-ParentSpanId", Metadata.ASCII_STRING_MARSHALLER);
    private final  Metadata.Key<string> b3SampledKey = Metadata.Key.of("X-B3-Sampled", Metadata.ASCII_STRING_MARSHALLER);
    /**
     * @param
     */
    public ClientTracingInterceptor(Tracer tracer) {
        this.tracer=tracer;
        this.operationNameConstructor = OperationNameConstructor.DEFAULT;
        this.streaming = false;
        this.verbose = false;
        this.tracedAttributes = new HashSet<clientrequestattribute>();
        this.activeSpanSource = ActiveSpanSource.GRPC_CONTEXT;
    }

    private ClientTracingInterceptor(Tracer tracer, OperationNameConstructor operationNameConstructor, boolean streaming,
        boolean verbose, Set<clientrequestattribute> tracedAttributes, ActiveSpanSource activeSpanSource) {
        this.tracer = tracer;
        this.operationNameConstructor = operationNameConstructor;
        this.streaming = streaming;
        this.verbose = verbose;
        this.tracedAttributes = tracedAttributes;
        this.activeSpanSource = activeSpanSource;
    }

    /**
     * Use this intercepter to trace all requests made by this client channel.
     * @param channel to be traced
     * @return intercepted channel
     */ 
    public Channel intercept(Channel channel) {
        return ClientInterceptors.intercept(channel, this);
    }

    @Override
    public <reqt, respt> ClientCall<reqt, respt> interceptCall(
        MethodDescriptor<reqt, respt> method, 
        CallOptions callOptions, 
        Channel next
    ) {
        final String operationName = operationNameConstructor.constructOperationName(method);

        Span activeSpan = this.activeSpanSource.getActiveSpan();
        final Span span = createSpanFromParent(activeSpan, operationName);

        for (ClientRequestAttribute attr : this.tracedAttributes) {
            switch (attr) {
                case ALL_CALL_OPTIONS:
                    span.setTag("grpc.call_options", callOptions.toString());
                    break;
                case AUTHORITY:
                    if (callOptions.getAuthority() == null) {
                        span.setTag("grpc.authority", "null");
                    } else {
                        span.setTag("grpc.authority", callOptions.getAuthority());                        
                    }
                    break;
                case COMPRESSOR:
                    if (callOptions.getCompressor() == null) {
                        span.setTag("grpc.compressor", "null");
                    } else {
                        span.setTag("grpc.compressor", callOptions.getCompressor());
                    }
                    break;
                case DEADLINE:
                    if (callOptions.getDeadline() == null) {
                        span.setTag("grpc.deadline_millis", "null");
                    } else {
                        span.setTag("grpc.deadline_millis", callOptions.getDeadline().timeRemaining(TimeUnit.MILLISECONDS));
                    }
                    break;
                case METHOD_NAME:
                    span.setTag("grpc.method_name", method.getFullMethodName());
                    break;
                case METHOD_TYPE:
                    if (method.getType() == null) {
                        span.setTag("grpc.method_type", "null");
                    } else {
                        span.setTag("grpc.method_type", method.getType().toString());
                    }
                    break;
                case HEADERS:
                	break;
            }
        }

        return new ForwardingClientCall.SimpleForwardingClientCall<reqt, respt>(next.newCall(method, callOptions)) {

            @Override
            public void start(Listener<respt> responseListener, Metadata headers) {
                if (verbose) {
                    span.log("Started call");
                }
                if (tracedAttributes.contains(ClientRequestAttribute.HEADERS)) {
                    span.setTag("grpc.headers", headers.toString()); 
                }

                tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, new TextMap() {
                    @Override
                    public void put(String key, String value) {
                        log.info("jaeger key:{},value:{}",key,value);
                        Metadata.Key<string> headerKey = Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER);
                        headers.put(headerKey, value);
                        String[] mm=value.split("%3A");
                        if("uber-trace-id".equals(key)&amp;&amp;mm.length==4){
                            headers.put(b3TraceIdKey,mm[0]);
                            log.info("jaeger traceId:{}",mm[0]);
                            headers.put(b3SpanIdKey,mm[1]);
                            headers.put(b3ParentSpanIdKey,mm[2]);
                            headers.put(b3SampledKey,mm[3]);
                        }

                    }
					@Override
					public Iterator<entry<string, string>&gt; iterator() {
						throw new UnsupportedOperationException(
								"TextMapInjectAdapter should only be used with Tracer.inject()");
					}
                });

                Listener<respt> tracingResponseListener = new ForwardingClientCallListener
                    .SimpleForwardingClientCallListener<respt>(responseListener) {

                    @Override
                    public void onHeaders(Metadata headers) {
                        if (verbose) { span.log(ImmutableMap.of("Response headers received", headers.toString())); }
                        delegate().onHeaders(headers);
                    }

                    @Override
                    public void onMessage(RespT message) {
                        if (streaming || verbose) { span.log("Response received"); }
                        delegate().onMessage(message);
                    }

                    @Override 
                    public void onClose(Status status, Metadata trailers) {
                        if (verbose) { 
                            if (status.getCode().value() == 0) { span.log("Call closed"); }
                            else { span.log(ImmutableMap.of("Call failed", status.getDescription())); }
                        }
                        span.finish();
                        delegate().onClose(status, trailers);
                    }
                };
                delegate().start(tracingResponseListener, headers);
            }

            @Override 
            public void cancel(@Nullable String message, @Nullable Throwable cause) {
                String errorMessage;
                if (message == null) {
                    errorMessage = "Error";
                } else {
                    errorMessage = message;
                }
                if (cause == null) {
                    span.log(errorMessage);
                } else {
                    span.log(ImmutableMap.of(errorMessage, cause.getMessage()));
                }
                delegate().cancel(message, cause);
            }

            @Override
            public void halfClose() {
                if (streaming) { span.log("Finished sending messages"); }
                delegate().halfClose();
            }

            @Override
            public void sendMessage(ReqT message) {
                if (streaming || verbose) { span.log("Message sent"); }
                delegate().sendMessage(message);
            }
        };
    }
    
    private Span createSpanFromParent(Span parentSpan, String operationName) {
        if (parentSpan == null) {
            return tracer.buildSpan(operationName).startManual();
        } else {
            return tracer.buildSpan(operationName).asChildOf(parentSpan).startManual();
        }
    }

    /**
     * Builds the configuration of a ClientTracingInterceptor.
     */
    public static class Builder {

        private Tracer tracer;
        private OperationNameConstructor operationNameConstructor;
        private boolean streaming;
        private boolean verbose;
        private Set<clientrequestattribute> tracedAttributes;
        private ActiveSpanSource activeSpanSource;  

        /**
         * @param tracer to use for this intercepter
         * Creates a Builder with default configuration
         */
        public Builder(Tracer tracer) {
            this.tracer = tracer;
            this.operationNameConstructor = OperationNameConstructor.DEFAULT;
            this.streaming = false;
            this.verbose = false;
            this.tracedAttributes = new HashSet<clientrequestattribute>();
            this.activeSpanSource = ActiveSpanSource.GRPC_CONTEXT;
        } 

        /**
         * @param operationNameConstructor to name all spans created by this intercepter
         * @return this Builder with configured operation name
         */
        public Builder withOperationName(OperationNameConstructor operationNameConstructor) {
            this.operationNameConstructor = operationNameConstructor;
            return this;
        } 

        /**
         * Logs streaming events to client spans.
         * @return this Builder configured to log streaming events
         */
        public Builder withStreaming() {
            this.streaming = true;
            return this;
        }

        /**
         * @param tracedAttributes to set as tags on client spans
         *  created by this intercepter
         * @return this Builder configured to trace attributes
         */
        public Builder withTracedAttributes(ClientRequestAttribute... tracedAttributes) {
            this.tracedAttributes = new HashSet<clientrequestattribute>(
                Arrays.asList(tracedAttributes));
            return this;
        }

        /**
         * Logs all request life-cycle events to client spans.
         * @return this Builder configured to be verbose
         */
        public Builder withVerbosity() {
            this.verbose = true;
            return this;
        }

        /**
         * @param activeSpanSource that provides a method of getting the 
         *  active span before the client call
         * @return this Builder configured to start client span as children 
         *  of the span returned by activeSpanSource.getActiveSpan()
         */
        public Builder withActiveSpanSource(ActiveSpanSource activeSpanSource) {
            this.activeSpanSource = activeSpanSource;
            return this;
        }

        /**
         * @return a ClientTracingInterceptor with this Builder's configuration
         */
        public ClientTracingInterceptor build() {
            return new ClientTracingInterceptor(this.tracer, this.operationNameConstructor, 
                this.streaming, this.verbose, this.tracedAttributes, this.activeSpanSource);
        }
    }

    public enum ClientRequestAttribute {
        METHOD_TYPE,
        METHOD_NAME,
        DEADLINE,
        COMPRESSOR,
        AUTHORITY,
        ALL_CALL_OPTIONS,
        HEADERS
    }
}

主要改动点在header那一块 在这里插入图片描述

feign注入b3头

      @Configuration
public class FeignConfig implements RequestInterceptor {
	@Autowired
    private final Tracer tracer;
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        if(attributes!=null&amp;&amp;attributes.getRequest()!=null){
            HttpServletRequest request = attributes.getRequest();

           JaegerSpanContext context=(JaegerSpanContext) tracer.activeSpan().context();
           requestTemplate.header("X-B3-TraceId",String.valueOf(context.getTraceId()));
           requestTemplate.header("X-B3-SpanId", String.valueOf(context.getSpanId()));
           requestTemplate.header("X-B3-ParentSpanId", String.valueOf(context.getParentId()));
           requestTemplate.header("X-B3-Sampled", context.isSampled()?"1":"0");
        }

    }

    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

}

经过手动注入后,就可以实现上下游串起来,暂时达到目标,后面的方案是统一传输方式,慢慢升级。

相关 [微服务 跟踪 jaeger] 推荐:

微服务全链路跟踪:jaeger集成istio,并兼容uber-trace-id与b3 - lipeng的个人空间 - OSCHINA

- -
微服务全链路跟踪:grpc集成zipkin. 微服务全链路跟踪:grpc集成jaeger. 微服务全链路跟踪:springcloud集成jaeger. 微服务全链路跟踪:jaeger集成istio,并兼容uber-trace-id与b3. 微服务全链路跟踪:jaeger集成hystrix. 公司有自己的一套基于k8s的paas系统,并且集成了istio,这里主要是想讲解下springcloud服务如何集成istio.

使用ebpf跟踪rpcx微服务

- - IT瘾-dev
ebpf是一种创新的革命性技术,它能在内核中运行沙箱程序, 而无需修改内核源码或者加载内核模块. 将 Linux 内核变成可编程之后,就能基于现有的(而非增加新的)抽象层来打造更加智能、 功能更加丰富的基础设施软件,而不会增加系统的复杂度,也不会牺牲执行效率和安全性. BPF的第一个版本在1994年问世.

SpringCloud项目接入Jaeger(下) - 掘金

- -
spring-cloud-sleuth这个组件时,会面临两个问题. 首先是日志中无法显示traceId和spanId这些链路信息,其次是不能在用. spring-cloud-sleuth所提供的方式进行链路传值. spring-cloud-sleuth是将traceId等链路信息保存在. slf4j的MDC(Mapped Diagnostic Contexts)中,然后通过%X{traceId}这种方式将traceId提取出来,比如打印到控制台的默认格式是:.

为Jaeger安装环境搭建监控基础设施

- - InfoQ - 促进软件开发领域知识与创新的传播
本文最初发表于 RedHat开发者博客,经原作者Juraci Paixão Kröhling和RedHat授权由InfoQ中文站翻译分享. 在生产环境中部署 Jaeger时,持续观察Jaeger实例,保证它的行为符合预期是非常重要的. 毕竟,Jaeger停机将会意味着跟踪数据的丢失,这样的话,我们很难理解生产环境的应用中究竟出现了什么问题.

[原]Java应用日志如何与Jaeger的trace关联

- - 程序员欣宸的博客
这里分类和汇总了欣宸的全部原创(含配套源码): https://github.com/zq2599/blog_demos. 经过 《Jaeger开发入门(java版)》的实战,相信您已经能将自己的应用接入Jaeger,并用来跟踪定位问题了,本文将介绍Jaeger一个小巧而强大的辅助功能,用少量改动大幅度提升定位问题的便利性:将业务日志与Jaeger的trace关联.

[原]Jaeger的客户端采样配置(Java版)

- - 程序员欣宸的博客
这里分类和汇总了欣宸的全部原创(含配套源码): https://github.com/zq2599/blog_demos. 采样很好理解:使用Jaeger时,未必需要将所有请求都上报到Jaeger,有时候只要抽取其中一部分观察即可,这就是按照一定策略进行采样;. Jaeger采样配置分为客户端和服务端两种配置,默认用的是服务端配置.

利用Jaeger打造云原生架构下分布式追踪系统

- -
Jaeger由Uber开源并被云原生基金会(CNCF)纳入孵化项目,背后有大厂和强大的组织支持,项目目前开发活跃;. 原生支持 OpenTracing 标准(可以认为是OpenTracing协议的参考实现),支持多种主流语言,可以复用大量的 OpenTracing 组件;. 高扩展,易伸缩,没有单点故障,可以随着业务方便扩容;.

初识微服务

- - ITeye博客
微服务架构越来越火,有必要学习一下. 软件开发过程中碰到什么问题. 一个简单的应用会随着时间推移逐渐变大. 在每次的sprint中,开发团队都会面对新“故事”,然后开发许多新代码. 几年后,这个小而简单的应用会变成了一个巨大的怪物. 一旦你的应用变成一个又大又复杂的怪物,那开发团队肯定很痛苦. 敏捷开发和部署举步维艰,其中最主要问题就是这个应用太复杂,以至于任何单个开发者都不可能搞懂它.

Servlet – 会话跟踪

- - ImportNew
HTTP本身是 “无状态”协议,它不保存连接交互信息,一次响应完成之后即连接断开,下一次请求需要重新建立连接,服务器不记录上次连接的内容.因此如果判断两次连接是否是同一用户, 就需要使用 会话跟踪技术来解决.常见的会话跟踪技术有如下几种:. URL重写: 在URL结尾附加. 会话ID标识,服务器通过会话ID识别不同用户..

谈微服务架构

- - 人月神话的BLOG
其实在前面很多文章谈到SOA,特别是系统内的SOA和组件化的时候已经很多内容和微服务架构思想是相同的,对于微服务架构,既然出现了这个新名称,那就再谈下微服务架构本身的一些特点和特性. 从这个图可以看到微服务架构的第一个重点,即业务系统本身的组件化和服务化,原来开发一个业务系统本身虽然分了组件和模块,但是本质还是紧耦合的,这关键的一个判断标准就是如果要将原有的业务系统按照模块分开部署到不同的进程里面并完成一个完整业务系统是不可能实现的.