motan學習筆記 五 opentracing學習入門







opentracing是什麼?

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


opentracing 在哪裏有java版?

從github,下載java版本的分支(https://github.com/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集合

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <C> void inject(SpanContext spanContext, Format<C> format, C carrier);  
  2. <C> SpanContext extract(Format<C> format, C carrier);  

Format其實就是 定義的幾個map  
[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public interface Format<C> {  
  2.     final class Builtin<C> implements Format<C> {   
  3.         public final static Format<TextMap> TEXT_MAP = new Builtin<TextMap>();  
  4.     public final static Format<TextMap> HTTP_HEADERS = new Builtin<TextMap>();  
  5.         public final static Format<ByteBuffer> BINARY = new Builtin<ByteBuffer>();  
  6.     }  

SpanBuilder

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

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. interface SpanBuilder extends SpanContext {  
  2.   
  3.       /** 
  4.        * A shorthand for addReference(References.CHILD_OF, parent). 
  5.        */  
  6.       SpanBuilder asChildOf(SpanContext parent);  
  7.   
  8.       /** 
  9.        * A shorthand for addReference(References.CHILD_OF, parent.context()). 
  10.        */  
  11.       SpanBuilder asChildOf(Span parent);  
  12.   
  13.       /** 
  14.        * Add a reference from the Span being built to a distinct (usually parent) Span. May be called multiple times to 
  15.        * represent multiple such References. 
  16.        * 
  17.        * @param referenceType the reference type, typically one of the constants defined in References 
  18.        * @param referencedContext the SpanContext being referenced; e.g., for a References.CHILD_OF referenceType, the 
  19.        *                          referencedContext is the parent 
  20.        * 
  21.        * @see io.opentracing.References 
  22.        */  
  23.       SpanBuilder addReference(String referenceType, SpanContext referencedContext);  
  24.   
  25.       /** Same as {@link Span#setTag(String, String)}, but for the span being built. */  
  26.       SpanBuilder withTag(String key, String value);  
  27.   
  28.       /** Same as {@link Span#setTag(String, String)}, but for the span being built. */  
  29.       SpanBuilder withTag(String key, boolean value);  
  30.   
  31.       /** Same as {@link Span#setTag(String, String)}, but for the span being built. */  
  32.       SpanBuilder withTag(String key, Number value);  
  33.   
  34.       /** Specify a timestamp of when the Span was started, represented in microseconds since epoch. */  
  35.       SpanBuilder withStartTimestamp(long microseconds);  
  36.   
  37.       /** Returns the started Span. */  
  38.       Span start();  
  39.   
  40.   }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章