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

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

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

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

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

微服务全链路跟踪:jaeger增加tag参数

公司有自己的一套基于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-id b3并没有将字段都拼接,而是单个传递,下面是案例:

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集成istio,并兼容uber-trace-id与b3 荐的更多相关文章

  1. Go微服务全链路跟踪详解

    在微服务架构中,调用链是漫长而复杂的,要了解其中的每个环节及其性能,你需要全链路跟踪. 它的原理很简单,你可以在每个请求开始时生成一个唯一的ID,并将其传递到整个调用链. 该ID称为Correlati ...

  2. Spring Cloud 微服务分布式链路跟踪 Sleuth 与 Zipkin

    Zipkin 是一个开放源代码分布式的跟踪系统,由 Twitter 公司开源,它致力于收集服务的定时数据,以解决微服务架构中的延迟问题,包括数据的收集.存储.查找和展现.它的理论模型来自于Google ...

  3. 微服务, 架构, 服务治理, 链路跟踪, 服务发现, 流量控制, Service Mesh

    微服务, 架构, 服务治理, 链路跟踪, 服务发现, 流量控制, Service Mesh 微服务架构   本文将介绍微服务架构和相关的组件,介绍他们是什么以及为什么要使用微服务架构和这些组件.本文侧 ...

  4. 微服务之分布式跟踪系统(springboot+zipkin+mysql)

    通过上一节<微服务之分布式跟踪系统(springboot+zipkin)>我们简单熟悉了zipkin的使用,但是收集的数据都保存在内存中重启后数据丢失,不过zipkin的Storage除了 ...

  5. SpringCloud初体验:六、利用 Sleuth 和 Zipkin 给微服务加上链路监控追踪查看功能

    首先:装上 Zipkin 服务,收集调用链跟踪数据,体验时装在了本机docker上, 方便快捷 docker run -d -p : openzipkin/zipkin 安装后访问地址也是 9411端 ...

  6. SpringBoot 整合 Elastic Stack 最新版本(7.14.1)分布式日志解决方案,开源微服务全栈项目【有来商城】的日志落地实践

    一. 前言 日志对于一个程序的重要程度不用过多的言语修饰,本篇将以实战的方式讲述开源微服务全栈项目 有来商城 是如何整合当下主流日志解决方案 ELK +Filebeat . 话不多说,先看实现的效果图 ...

  7. SpringBoot之微服务日志链路追踪

    SpringBoot之微服务日志链路追踪 简介 在微服务里,业务出现问题或者程序出的任何问题,都少不了查看日志,一般我们使用 ELK 相关的日志收集工具,服务多的情况下,业务问题也是有些难以排查,只能 ...

  8. IDEA 集成 Docker 插件实现一键远程部署 SpringBoot 应用,无需三方依赖,开源微服务全栈项目有来商城云环境的部署方式

    一. 前言 最近有些童鞋对开源微服务商城项目 youlai-mall 如何部署到线上环境以及项目中 的Dockerfile 文件有疑问,所以写了这篇文章做个答疑以及演示完整的微服务项目发布到线上的流程 ...

  9. 我对微服务、SpringCloud、k8s、Istio的一些杂想

    一.微服务与SOA “微服务”是一个名词,没有这个名词之前也有“微服务”,一个朗朗上口的名词能让大家产生一个认知共识,这对推动一个事务的发展挺重要的,不然你叫微服务他叫小服务的大家很难集中到一个点上. ...

  10. 微服务、SpringCloud、k8s、Istio杂谈

    一.微服务与SOA “微服务”是一个名词,没有这个名词之前也有“微服务”,一个朗朗上口的名词能让大家产生一个认知共识,这对推动一个事务的发展挺重要的,不然你叫微服务他叫小服务的大家很难集中到一个点上. ...

随机推荐

  1. WPF/C#:在DataGrid中显示选择框

    前言 在使用WPF的过程中可能会经常遇到在DataGrid的最前或者最后添加一列选择框的需求,今天跟大家分享一下,在自己的项目中是如何实现的. 整体实现效果如下: 如果对此感兴趣,可以接下来看具体实现 ...

  2. CentOS7系统搭建web环境 php&nginx&pgsql

    环境:VMware.CentOS-7-x86_64-DVD-2009.iso.nginx-1.26.1.php-7.2.0.postgresql-12 php最好安装对应php项目所需版本,否则会出现 ...

  3. Masonry在视图相对关系处理中的各种“offset”

    如果我们需要设置一个view在另一个view的右边缘距离一定距离的地方,利用Masonry这么写: [a mas_makeConstraints:^(MASConstraintMaker *make) ...

  4. 01-Python介绍、安装与入门

    Python介绍 关于Python Python 是一种解释型.面向对象.动态数据类型的高级程序设计语言. 解释型:Python将代码边解释边运行,而不是像C一样编译.类似解释型的语言还有JAVA. ...

  5. 分享两个内置Google广告位的Typecho主题

    前言 很多项目的开始都是因为情怀和热爱,"为爱发电"是一件很值得尊敬的事情,然而大量"为爱发电"的项目最后却不得不因"难以为继"而被迫停服. ...

  6. 3568F-Qt工程编译说明

  7. Node.js - fs.path模块

    首先我有话说,是谁说的学完ajax就可以去vue了,太天真了我,学会js钻出来个ajax,学完ajax钻出来个node.js这一步步的,当然node不会学到太深入把表面的认识一下就可以了,这之后可能更 ...

  8. java中的基准测试框架JMH

    JHM是openJDK开发的一个benchmark框架.它是一个Maven依赖,所以创建一个Maven项目,引入下面两个依赖: <dependency> <groupId>or ...

  9. 尝试官方的第一个SpringNative 0.11程序(WSL2)

    Spring Native是Spring推出微服务体系Spring Cloud之后的又一大举动,从名字可以猜出,Spring Native是一门面向云原生的技术.如果你还对这个概念不太理解,可以多看一 ...

  10. virtualbox ubuntu拓展存储空间

    1. 关闭虚拟机,右键点击virtualbox图标,选择打开文件位置,记录下路径: 2. 找到需要拓容的ubuntu虚拟机的.vdi文件,记录下路径: 3. windows命令行转到virtualbo ...