美文网首页
Spring Cloud Sleuth 计算接口访问时间

Spring Cloud Sleuth 计算接口访问时间

作者: _浅墨_ | 来源:发表于2017-08-24 17:19 被阅读186次

    Sleuth 流程图


    trace
    span

    The initial span that starts a trace is called a root span. The value of span id of that span is equal to trace id.

    Trace: A set of spans forming a tree-like structure. For example, if you are running a distributed big-data store, a trace might be formed by a put request.

    Annotation: is used to record existence of an event in time. Some of the core annotations used to define the start and stop of a request are:

    • cs - Client Sent - The client has made a request. This annotation depicts the start of the span.

    • sr - Server Received - The server side got the request and will start processing it. If one subtracts the cs timestamp from this timestamp one will receive the network latency.

    • ss - Server Sent - Annotated upon completion of request processing (when the response got sent back to the client). If one subtracts the sr timestamp from this timestamp one will receive the time needed by the server side to process the request.

    • cr - Client Received - Signifies the end of the span. The client has successfully received the response from the server side. If one subtracts the cs timestamp from this timestamp one will receive the whole time needed by the client to receive the response from the server.

    Baggage vs. Span Tags

    Baggage travels with the trace (i.e. every child span contains the baggage of its parent). Zipkin has no knowledge of baggage and will not even receive that information.

    Tags are attached to a specific span - they are presented for that particular span only. However you can search by tag to find the trace, where there exists a span having the searched tag value.

    If you want to be able to lookup a span based on baggage, you should add corresponding entry as a tag in the root span.

    @Autowired Tracer tracer;
    
    Span span = tracer.getCurrentSpan();
    String baggageKey = "key";
    String baggageValue = "foo";
    span.setBaggageItem(baggageKey, baggageValue);
    tracer.addTag(baggageKey, baggageValue);
    

    根据 span 计算接口耗时:

    /**
         * There could be instrumentation delay between span creation and the
         * semantic start of the span (client send). When there's a difference,
         * spans look confusing. Ex users expect duration to be client
         * receive - send, but it is a little more than that. Rather than have
         * to teach each user about the possibility of instrumentation overhead,
         * we truncate absolute duration (span finish - create) to semantic
         * duration (client receive - send)
         */
        private static long calculateDurationInMicros(Span span) {
            Log clientSend = hasLog(Span.CLIENT_SEND, span);
            Log clientReceived = hasLog(Span.CLIENT_RECV, span);
            if (clientSend != null && clientReceived != null) {
                return (clientReceived.getTimestamp() - clientSend.getTimestamp()) * 1000;
            }
            return span.getAccumulatedMillis();
        }
    
        private static Log hasLog(String logName, Span span) {
            for (Log log : span.logs()) {
                if (logName.equals(log.getEvent())) {
                    return log;
                }
            }
            return null;
        }
    

    参考:

    1. Spring Cloud Sleuth 官方文档
    2. spring-cloud-sleuth
    3. spring-cloud-sleuth

    相关文章

      网友评论

          本文标题:Spring Cloud Sleuth 计算接口访问时间

          本文链接:https://www.haomeiwen.com/subject/iyvedxtx.html