1. 程式人生 > >motan學習筆記 五 opentracing學習入門

motan學習筆記 五 opentracing學習入門

opentracing是什麼?

opentracing(http://opentracing.io/)  是分散式跟蹤系統,當我們把系統拆成服務化,分散式系統的時候,查詢一個問題,很可能需要多個登入多臺機器。 opentracing 定義了一套api 通過提供平臺無關、廠商無關的API,使得開發人員能夠方便的新增(或更換)追蹤系統的實現。OpenTracing正在為全球的分散式追蹤,提供統一的概念和資料標準。

opentracing 在哪裡有java版?

從下面的圖,可以看出api為定義模組,mock擋板 noop空實現  impl具體實現

opentracing 定義了哪些api?

span

一條訊息,相當於一個打點資訊,裡面有兩個重要的方法  finish 和 其他 finish 用來完成span的資訊,並且用於提交 而其他方法用來組裝span的資料

SpanContext


及傳輸的上下文,儲存一些資料,一般loaclThread的,主要方法

Iterable<Map.Entry<String, String>> baggageItems();

Tracer

定義了兩個方法,一個是往spancontext 插入引數,一個獲取spancontext,相當於span集合
<C> void inject(SpanContext spanContext, Format<C> format, C carrier);
<C> SpanContext extract(Format<C> format, C carrier);

Format其實就是 定義的幾個map  
public interface Format<C> {
    final class Builtin<C> implements Format<C> { 
        public final static Format<TextMap> TEXT_MAP = new Builtin<TextMap>();
	public final static Format<TextMap> HTTP_HEADERS = new Builtin<TextMap>();
        public final static Format<ByteBuffer> BINARY = new Builtin<ByteBuffer>();
    }

SpanBuilder

顧名思義,builder模式,用來構建 span, 關鍵是start方法,構建成功
interface SpanBuilder extends SpanContext {

      /**
       * A shorthand for addReference(References.CHILD_OF, parent).
       */
      SpanBuilder asChildOf(SpanContext parent);

      /**
       * A shorthand for addReference(References.CHILD_OF, parent.context()).
       */
      SpanBuilder asChildOf(Span parent);

      /**
       * Add a reference from the Span being built to a distinct (usually parent) Span. May be called multiple times to
       * represent multiple such References.
       *
       * @param referenceType the reference type, typically one of the constants defined in References
       * @param referencedContext the SpanContext being referenced; e.g., for a References.CHILD_OF referenceType, the
       *                          referencedContext is the parent
       *
       * @see io.opentracing.References
       */
      SpanBuilder addReference(String referenceType, SpanContext referencedContext);

      /** Same as {@link Span#setTag(String, String)}, but for the span being built. */
      SpanBuilder withTag(String key, String value);

      /** Same as {@link Span#setTag(String, String)}, but for the span being built. */
      SpanBuilder withTag(String key, boolean value);

      /** Same as {@link Span#setTag(String, String)}, but for the span being built. */
      SpanBuilder withTag(String key, Number value);

      /** Specify a timestamp of when the Span was started, represented in microseconds since epoch. */
      SpanBuilder withStartTimestamp(long microseconds);

      /** Returns the started Span. */
      Span start();

  }