zipkin源码学习

业务使用zipkin,需要在配置类

@Configuration
public class ZipkinBean {

    /**
     * 配置收集器
     *
 * @return
 */
@Bean
public SpanCollector spanCollector() {
    Config config = HttpSpanCollector.Config.builder().compressionEnabled(false).connectTimeout(5000)
            .flushInterval(1).readTimeout(6000).build();
    return HttpSpanCollector.create("http://192.168.119.158:9411", config, new EmptySpanCollectorMetricsHandler());
}

/**
 * Brave各工具类的封装
 *
 * @param spanCollector
 * @return
 */
@Bean
public Brave brave(SpanCollector spanCollector) {
    Builder builder = new Builder("service2");// 指定serviceName
    builder.spanCollector(spanCollector);
    builder.traceSampler(Sampler.create(1));// 采集率
    return builder.build();
}

/**
 * 拦截器,需要serverRequestInterceptor,serverResponseInterceptor 分别完成sr和ss操作
 *
 * @param brave
 * @return
 */
@Bean
public BraveServletFilter braveServletFilter(Brave brave) {
    return new BraveServletFilter(brave.serverRequestInterceptor(), brave.serverResponseInterceptor(),
            new DefaultSpanNameProvider());
}

/**
 * httpClient客户端,需要clientRequestInterceptor,clientResponseInterceptor分别完成cs和cr操作
 *
 * @param brave
 * @return
 */
@Bean
public CloseableHttpClient httpClient(Brave brave) {
    CloseableHttpClient httpclient = HttpClients.custom()
            .addInterceptorFirst(new BraveHttpRequestInterceptor(brave.clientRequestInterceptor(),
                    new DefaultSpanNameProvider()))
            .addInterceptorFirst(new BraveHttpResponseInterceptor(brave.clientResponseInterceptor())).build();
    return httpclient;
    }
}

其中的拦截器brave.serverRequestInterceptor()即为下面的这个类:

package com.github.kristofa.brave;

import com.github.kristofa.brave.internal.Util;
import java.util.Iterator;
import java.util.logging.Logger;

public class ServerRequestInterceptor {
    private static final Logger LOGGER = Logger.getLogger(ServerRequestInterceptor.class.getName());
    private final ServerTracer serverTracer;

    public ServerRequestInterceptor(ServerTracer serverTracer) {
        this.serverTracer = (ServerTracer)Util.checkNotNull(serverTracer, "Null serverTracer", new Object[0]);
    }

    public void handle(ServerRequestAdapter adapter) {
        this.serverTracer.clearCurrentSpan();
        TraceData traceData = adapter.getTraceData();
        Boolean sample = traceData.getSample();
        if (sample != null && Boolean.FALSE.equals(sample)) {
            this.serverTracer.setStateNoTracing();
            LOGGER.fine("Received indication that we should NOT trace.");
        } else {
            if (traceData.getSpanId() != null) {
            LOGGER.fine("Received span information as part of request.");
            SpanId spanId = traceData.getSpanId();
            this.serverTracer.setStateCurrentTrace(spanId.traceId, spanId.spanId, spanId.nullableParentId(), adapter.getSpanName());
        } else {
            LOGGER.fine("Received no span state.");
            this.serverTracer.setStateUnknown(adapter.getSpanName());
        }

        this.serverTracer.setServerReceived();
        Iterator var6 = adapter.requestAnnotations().iterator();

        while(var6.hasNext()) {
            KeyValueAnnotation annotation = (KeyValueAnnotation)var6.next();
            this.serverTracer.submitBinaryAnnotation(annotation.getKey(), annotation.getValue());
        }
    }

}

}

可以看到这个adaptor获取到了请求的所有内容:

zipkin源码学习_第1张图片

拦截器 brave.clientRequestInterceptor()作用为:将tranceid和spanid等发往下一个节点

package com.github.kristofa.brave;

import com.github.kristofa.brave.internal.Util;
import com.twitter.zipkin.gen.Endpoint;
import java.util.Iterator;

public class ClientRequestInterceptor {
    private final ClientTracer clientTracer;

    public ClientRequestInterceptor(ClientTracer clientTracer) {
        this.clientTracer = (ClientTracer)Util.checkNotNull(clientTracer, "Null clientTracer", new Object[0]);
    }

    public void handle(ClientRequestAdapter adapter) {
        SpanId spanId = this.clientTracer.startNewSpan(adapter.getSpanName());
        if (spanId == null) {
            adapter.addSpanIdToRequest((SpanId)null);
        } else {
            adapter.addSpanIdToRequest(spanId);
            Iterator var3 = adapter.requestAnnotations().iterator();

            while(var3.hasNext()) {
                KeyValueAnnotation annotation = (KeyValueAnnotation)var3.next();
                this.clientTracer.submitBinaryAnnotation(annotation.getKey(), annotation.getValue());
            }

            this.recordClientSentAnnotations(adapter.serverAddress());
        }

    }

    private void recordClientSentAnnotations(Endpoint serverAddress) {
        if (serverAddress == null) {
            this.clientTracer.setClientSent();
        } else {
            this.clientTracer.setClientSent(serverAddress.ipv4, serverAddress.port, serverAddress.service_name);
        }

    }
}

你可能感兴趣的:(zipkin源码学习)