基于Dubbo的分布式链路追踪

源码cloud分支
假设已经了解了Dubbo的Filter,Spring Cloud Sleuth,ZipKin。
其实openZipKin已经提供了该功能,请切换到dubbo分支
但它是基于最新的spring cloud sleuth。spring cloud sleuth 2.0.0,我也没去了解最新的版本的代码,因为公司版本不是想升就升,所以造了一个轮子,其思想和源码差不多。
另外使用spring cloud sleuth来链路追踪而不使用其他库,比如Brave。那就是它集成了将链路信息记录到LogBack的功能。
基于Brave的Demo 请看源码master分支
本文直接贴代码,有空会进行讲解


  • 定义Filter
@Activate(group = {Constants.PROVIDER, Constants.CONSUMER})
public class MyTracingFilter implements Filter {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyTracingFilter.class);

    protected void addRequestTags(RpcContext context) {
        this.httpTraceKeysInjector.addRequestTags(context.getUrl().getAddress(),
                context.getUrl().getHost(),
                context.getUrl().getPath(),
                context.getMethodName(),
                Collections.emptyMap());
    }

    HttpTraceKeysInjector httpTraceKeysInjector;
    Tracer tracer;
    DubboExtractor dubboExtractor;
    DubboInject injector;
    SpanReporter spanReporter;

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        tracer = ApplicationBeanHolder.getBean(Tracer.class);
        if (tracer == null) {
            return invoker.invoke(invocation);
        } else {
            injectBean();
        }


        RpcContext rpcContext = RpcContext.getContext();
        Span span;
        String service = invoker.getInterface().getSimpleName();
        String method = RpcUtils.getMethodName(invocation);
        String spanName = service + "/" + method;
        if (rpcContext.isConsumerSide()) {

            span = tracer.createSpan(spanName);
            injector.inject(span, new DubboRequestTextMap(RpcContext.getContext()));
            addRequestTags(RpcContext.getContext());
            span.logEvent(Span.CLIENT_SEND);
            Result result;
            try {
                result =  invoker.invoke(invocation);
            } finally {
                closeSpan(span,true);
            }
            return result;
        } else {
            Span parentSpan = dubboExtractor.joinTrace(new DubboRequestTextMap(RpcContext.getContext()));
            if (parentSpan != null) {
                span = parentSpan;
                tracer.continueSpan(span);
                span.logEvent(Span.SERVER_RECV);
            } else {
                String header = RpcContext.getContext().getAttachment(Span.SPAN_FLAGS);
                if (Span.SPAN_SAMPLED.equals(header)) {
                    span = tracer.createSpan(spanName, new AlwaysSampler());
                } else {
                    span = tracer.createSpan(spanName);
                }
                span.logEvent(Span.SERVER_RECV);
            }
            Result result;
            try {
                result =  invoker.invoke(invocation);
            } finally {
                recordParentSpan(span);
                closeSpan(span,false);
            }
            return result;
        }

    }

    private void recordParentSpan(Span parent) {
        if (parent == null) {
            return;
        }
        if (parent.isRemote()) {
            tracer.getCurrentSpan().logEvent(Span.SERVER_SEND);
            parent.stop();
            this.spanReporter.report(parent);
        }
    }

    private void injectBean() {
        injector = ApplicationBeanHolder.getBean(DubboInject.class);
        dubboExtractor = ApplicationBeanHolder.getBean(DubboExtractor.class);
        httpTraceKeysInjector = ApplicationBeanHolder.getBean(HttpTraceKeysInjector.class);
        spanReporter = ApplicationBeanHolder.getBean(SpanReporter.class);
    }

    private void closeSpan(Span span, Boolean type) {
        if (type) {
            tracer.getCurrentSpan().logEvent(Span.CLIENT_RECV);
        }
        if (span != null) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Closing Dubbo span " + span);
            }
            tracer.close(span);
        }
    }
}
  • DubboInject
public class DubboInject implements SpanInjector<SpanTextMap> {

    private static final DubboSpanMapper SPAN_CARRIER_MAPPER = new DubboSpanMapper();

    @Override
    public void inject(Span span, SpanTextMap map) {
        Map<String, String> carrier = SPAN_CARRIER_MAPPER.convert(map);
        setHeader(map, carrier, Span.TRACE_ID_NAME, span.traceIdString());
        setIdHeader(map, carrier, Span.SPAN_ID_NAME, span.getSpanId());
        setHeader(map, carrier, Span.SAMPLED_NAME, span.isExportable() ? Span.SPAN_SAMPLED : Span.SPAN_NOT_SAMPLED);
        setHeader(map, carrier, Span.SPAN_NAME_NAME, span.getName());
        setIdHeader(map, carrier, Span.PARENT_ID_NAME, getParentId(span));
        setHeader(map, carrier, Span.PROCESS_ID_NAME, span.getProcessId());
        for (Map.Entry<String, String> entry : span.baggageItems()) {
            map.put(prefixedKey(entry.getKey()), entry.getValue());
        }
    }

    private String prefixedKey(String key) {
        if (key.startsWith(Span.SPAN_BAGGAGE_HEADER_PREFIX
                + DubboSpanMapper.HEADER_DELIMITER)) {
            return key;
        }
        return Span.SPAN_BAGGAGE_HEADER_PREFIX + DubboSpanMapper.HEADER_DELIMITER
                + key;
    }

    private Long getParentId(Span span) {
        return !span.getParents().isEmpty() ? span.getParents().get(0) : null;
    }

    private void setIdHeader(SpanTextMap map, Map<String, String> carrier, String name, Long value) {
        if (value != null) {
            setHeader(map, carrier, name, Span.idToHex(value));
        }
    }

    private void setHeader(SpanTextMap map, Map<String, String> carrier, String name, String value) {
        if (StringUtils.hasText(value) && !carrier.containsKey(name)) {
            map.put(name, value);
        }
    }
}
  • DubboExtractor
public class DubboExtractor implements SpanExtractor<SpanTextMap> {

    private static final org.apache.commons.logging.Log log = LogFactory.getLog(
            MethodHandles.lookup().lookupClass());

    private static final String HTTP_COMPONENT = "http";

    private static final DubboSpanMapper SPAN_CARRIER_MAPPER = new DubboSpanMapper();

    private final Pattern skipPattern;
    private final Random random;

    public DubboExtractor(Pattern skipPattern) {
        this.skipPattern = skipPattern;
        this.random = new Random();
    }

    @Override
    public Span joinTrace(SpanTextMap textMap) {
        Map<String, String> carrier = SPAN_CARRIER_MAPPER.convert(textMap);
        boolean debug = Span.SPAN_SAMPLED.equals(carrier.get(Span.SPAN_FLAGS));
        boolean idToBeGenerated = debug && onlySpanIdIsPresent(carrier);

        if (!idToBeGenerated && traceIdIsMissing(carrier)) {
            return null;
        }
        try {
            return buildParentSpan(carrier, idToBeGenerated);
        } catch (Exception e) {
            log.error("Exception occurred while trying to extract span from carrier", e);
            return null;
        }
    }

    private boolean onlySpanIdIsPresent(Map<String, String> carrier) {
        return traceIdIsMissing(carrier) && spanIdIsPresent(carrier);
    }

    private boolean traceIdIsMissing(Map<String, String> carrier) {
        return carrier.get(Span.TRACE_ID_NAME) == null;
    }

    private boolean spanIdIsPresent(Map<String, String> carrier) {
        return carrier.get(Span.SPAN_ID_NAME) != null;
    }

    private String generateId() {
        return Span.idToHex(this.random.nextLong());
    }

    private long spanId(String spanId, String traceId) {
        if (spanId == null) {
            if (log.isDebugEnabled()) {
                log.debug("Request is missing a span id but it has a trace id. We'll assume that this is "
                        + "a root span with span id equal to the lower 64-bits of the trace id");
            }
            return Span.hexToId(traceId);
        } else {
            return Span.hexToId(spanId);
        }
    }

    private Span buildParentSpan(Map<String, String> carrier, boolean idToBeGenerated) {
        String traceId = carrier.get(Span.TRACE_ID_NAME);
        if (traceId == null) {
            traceId = generateId();
        }
        Span.SpanBuilder span = Span.builder()
                .traceIdHigh(traceId.length() == 32 ? Span.hexToId(traceId, 0) : 0)
                .traceId(Span.hexToId(traceId))
                .spanId(spanId(carrier.get(Span.SPAN_ID_NAME), traceId));
        String parentName = carrier.get(Span.SPAN_NAME_NAME);
        if (StringUtils.hasText(parentName)) {
            span.name(parentName);
        } else {
            span.name(HTTP_COMPONENT + ":/parent"
                    + carrier.get(DubboSpanMapper.URI_HEADER));
        }
        String processId = carrier.get(Span.PROCESS_ID_NAME);
        if (StringUtils.hasText(processId)) {
            span.processId(processId);
        }
        String parentId = carrier.get(Span.PARENT_ID_NAME);
        if (parentId != null) {
            span.parent(Span.hexToId(parentId));
        }
        span.remote(true);

//        boolean skip = this.skipPattern
//                .matcher(carrier.get(DubboSpanMapper.URI_HEADER)).matches()
//                || Span.SPAN_NOT_SAMPLED.equals(carrier.get(Span.SAMPLED_NAME));
        boolean skip = false;
        // trace, span id were retrieved from the headers and span is sampled
        span.exportable(!(skip || idToBeGenerated));
        boolean debug = Span.SPAN_SAMPLED.equals(carrier.get(Span.SPAN_FLAGS));
        if (debug) {
            span.exportable(true);
        } else if (skip) {
            span.exportable(false);
        }
        for (Map.Entry<String, String> entry : carrier.entrySet()) {
            if (entry.getKey().toLowerCase()
                    .startsWith(DubboSpanMapper.BAGGAGE_PREFIX)) {
                span.baggage(unprefixedKey(entry.getKey()), entry.getValue());
            }
        }
        return span.build();
    }

    private String unprefixedKey(String key) {
        return key.substring(key.indexOf(DubboSpanMapper.HEADER_DELIMITER) + 1)
                .toLowerCase();
    }
}

  • DubboRequestTextMap
public class DubboRequestTextMap implements SpanTextMap {

    private final RpcContext delegate;

    public DubboRequestTextMap(RpcContext delegate) {
        this.delegate = delegate;
    }

    @Override
    public Iterator<Map.Entry<String, String>> iterator() {
        return this.delegate.getAttachments().entrySet().iterator();
    }

    @Override
    public void put(String key, String value) {
        if (!StringUtils.hasText(value)) {
            return;
        }
        this.delegate.getAttachments().put(key, value);
    }
}
  • DubboSpanMapper
public class DubboSpanMapper {

    static final String HEADER_DELIMITER = "-";
    static final String BAGGAGE_PREFIX = Span.SPAN_BAGGAGE_HEADER_PREFIX
            + HEADER_DELIMITER;
    static final String URI_HEADER = "X-Span-Uri";

    private static Comparator<String> IGNORE_CASE_COMPARATOR = new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            return o1.toLowerCase().compareTo(o2.toLowerCase());
        }
    };

    /**
     * Acceptable span fields
     */
    private static final Set<String> SPAN_FIELDS;

    static {
        TreeSet<String> fields = new TreeSet<>(IGNORE_CASE_COMPARATOR);
        Collections.addAll(fields, Span.SPAN_FLAGS, Span.TRACE_ID_NAME, Span.SPAN_ID_NAME,
                Span.PROCESS_ID_NAME, Span.SPAN_NAME_NAME, Span.PARENT_ID_NAME,
                Span.SAMPLED_NAME, URI_HEADER);
        SPAN_FIELDS = Collections.unmodifiableSet(fields);
    }

    /**
     * Create new Map of carrier values
     */
    Map<String, String> convert(SpanTextMap textMap) {
        Map<String, String> carrier = new TreeMap<>(IGNORE_CASE_COMPARATOR);
        for (Map.Entry<String, String> entry : textMap) {
            if (isAcceptable(entry.getKey())) {
                carrier.put(entry.getKey(), entry.getValue());
            }
        }
        return Collections.unmodifiableMap(carrier);
    }

    private boolean isAcceptable(String key) {
        return SPAN_FIELDS.contains(key) || key.startsWith(BAGGAGE_PREFIX);
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,772评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,458评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,610评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,640评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,657评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,590评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,962评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,631评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,870评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,611评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,704评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,386评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,969评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,944评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,179评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,742评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,440评论 2 342

推荐阅读更多精彩内容