最近写了一个链路追踪Demo分享下,实现了链路追踪过程中数据的记录,还有能扩展的地方,后期再继续补充。
原理参考上面文章 《Dubbo链路追踪——生成全局ID(traceId)》
源码地址
实现链路追踪的目的
分布式跟踪系统还有其他比较成熟的实现,例如:Naver的Pinpoint、Apache的HTrace、阿里的鹰眼Tracing、京东的Hydra、新浪的Watchman,美团点评的CAT,skywalking等。 本次主要利用Dubbo数据传播特性扩展Filter接口来实现链路追踪的目的
重点主要是zipkin及brave使用及特性,当前brave版本为 5.2.0 为 2018年8月份发布的release版本 , zipkin版本为2.2.1 所需JDK为1.8
下载最新的zipkin并启动
wget -O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec' java -jar zipkin.jar
输入 http://localhost:9411/zipkin/ 进入WebUI界面如下
代码的初步版本:方便描述
import brave.Span; import brave.Tracer; import brave.Tracing; import brave.propagation.*; import brave.sampler.Sampler; import com.alibaba.dubbo.common.Constants; import com.alibaba.dubbo.common.extension.Activate; import com.alibaba.dubbo.common.json.JSON; import com.alibaba.dubbo.common.logger.Logger; import com.alibaba.dubbo.common.logger.LoggerFactory; import com.alibaba.dubbo.remoting.exchange.ResponseCallback; import com.alibaba.dubbo.rpc.*; import com.alibaba.dubbo.rpc.protocol.dubbo.FutureAdapter; import com.alibaba.dubbo.rpc.support.RpcUtils; import zipkin2.codec.SpanBytesEncoder; import zipkin2.reporter.AsyncReporter; import zipkin2.reporter.Sender; import zipkin2.reporter.okhttp3.OkHttpSender; import java.io.IOException; import java.net.InetSocketAddress; import java.util.Map; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; /** * Created with IntelliJ IDEA. * * @author: bakerZhu * @description: * @modifytime: */ @Activate(group = {Constants.PROVIDER, Constants.CONSUMER}) public class TracingFilter implements Filter { private static final Logger log = LoggerFactory.getLogger(TracingFilter.class); private static Tracing tracing; private static Tracer tracer; private static TraceContext.Extractor<Map<String, String>> extractor; private static TraceContext.Injector<Map<String, String>> injector; static final Propagation.Getter<Map<String, String>, String> GETTER = new Propagation.Getter<Map<String, String>, String>() { @Override public String get(Map<String, String> carrier, String key) { return carrier.get(key); } @Override public String toString() { return "Map::get"; } }; static final Propagation.Setter<Map<String, String>, String> SETTER = new Propagation.Setter<Map<String, String>, String>() { @Override public void put(Map<String, String> carrier, String key, String value) { carrier.put(key, value); } @Override public String toString() { return "Map::set"; } }; static { // 1 Sender sender = OkHttpSender.create("http://localhost:9411/api/v2/spans"); // 2 AsyncReporter asyncReporter = AsyncReporter.builder(sender) .closeTimeout(500, TimeUnit.MILLISECONDS) .build(SpanBytesEncoder.JSON_V2); // 3 tracing = Tracing.newBuilder() .localServiceName("tracer-client") .spanReporter(asyncReporter) .sampler(Sampler.ALWAYS_SAMPLE) .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "user-name")) .build(); tracer = tracing.tracer(); // 4 // 4.1 extractor = tracing.propagation().extractor(GETTER); // 4.2 injector = tracing.propagation().injector(SETTER); } public TracingFilter() { } @Override public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { RpcContext rpcContext = RpcContext.getContext(); // 5 Span.Kind kind = rpcContext.isProviderSide() ? Span.Kind.SERVER : Span.Kind.CLIENT; final Span span; if (kind.equals(Span.Kind.CLIENT)) { //6 span = tracer.nextSpan(); //7 injector.inject(span.context(), invocation.getAttachments()); } else { //8 TraceContextOrSamplingFlags extracted = extractor.extract(invocation.getAttachments()); //9 span = extracted.context() != null ? tracer.joinSpan(extracted.context()) : tracer.nextSpan(extracted); } if (!span.isNoop()) { span.kind(kind).start(); //10 String service = invoker.getInterface().getSimpleName(); String method = RpcUtils.getMethodName(invocation); span.kind(kind); span.name(service + "/" + method); InetSocketAddress remoteAddress = rpcContext.getRemoteAddress(); span.remoteIpAndPort( remoteAddress.getAddress() != null ? remoteAddress.getAddress().getHostAddress() : remoteAddress.getHostName(),remoteAddress.getPort()); } boolean isOneway = false, deferFinish = false; try (Tracer.SpanInScope scope = tracer.withSpanInScope(span)){ //11 collectArguments(invocation, span, kind); Result result = invoker.invoke(invocation); if (result.hasException()) { onError(result.getException(), span); } // 12 isOneway = RpcUtils.isOneway(invoker.getUrl(), invocation); // 13 Future<Object> future = rpcContext.getFuture(); if (future instanceof FutureAdapter) { deferFinish = true; ((FutureAdapter) future).getFuture().setCallback(new FinishSpanCallback(span));// 14 } return result; } catch (Error | RuntimeException e) { onError(e, span); throw e; } finally { if (isOneway) { // 15 span.flush(); } else if (!deferFinish) { // 16 span.finish(); } } } static void onError(Throwable error, Span span) { span.error(error); if (error instanceof RpcException) { span.tag("dubbo.error_msg", RpcExceptionEnum.getMsgByCode(((RpcException) error).getCode())); } } static void collectArguments(Invocation invocation, Span span, Span.Kind kind) { if (kind == Span.Kind.CLIENT) { StringBuilder fqcn = new StringBuilder(); Object[] args = invocation.getArguments(); if (args != null && args.length > 0) { try { fqcn.append(JSON.json(args)); } catch (IOException e) { log.warn(e.getMessage(), e); } } span.tag("args", fqcn.toString()); } } static final class FinishSpanCallback implements ResponseCallback { final Span span; FinishSpanCallback(Span span) { this.span = span; } @Override public void done(Object response) { span.finish(); } @Override public void caught(Throwable exception) { onError(exception, span); span.finish(); } } // 17 private enum RpcExceptionEnum { UNKNOWN_EXCEPTION(0, "unknown exception"), NETWORK_EXCEPTION(1, "network exception"), TIMEOUT_EXCEPTION(2, "timeout exception"), BIZ_EXCEPTION(3, "biz exception"), FORBIDDEN_EXCEPTION(4, "forbidden exception"), SERIALIZATION_EXCEPTION(5, "serialization exception"),; private int code; private String msg; RpcExceptionEnum(int code, String msg) { this.code = code; this.msg = msg; } public static String getMsgByCode(int code) { for (RpcExceptionEnum error : RpcExceptionEnum.values()) { if (code == error.code) { return error.msg; } } return null; } } }
测试项
POM依赖添加
<dependency> <groupId>com.github.baker</groupId> <artifactId>Tracing</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
资源目录根路径下添加tracing.properties文件 一次调用信息 调用链 调用成功失败汇总 zipkinHost 指定zipkin服务器IP:PORT 默认为localhost:9411 serviceName 指定应用名称 默认为trace-default
调用链:
待扩展项
源码地址