代碼埋點 instrumentation:Brave
Java 可以通過 Brave 來埋點:
https://github.com/openzipkin/brave
實現在各種開源組件(例如 HTTP Client、JDBC Driver、Dubbo 等)的埋點(instrumentation)上報
例如有:
- brave-instrumentation-spring-webmvc Spring MVC 收到請求後埋點
- brave-instrumentation-okhttp3 通過 OKHttp 客戶端發送 HTTP 請求時埋點
- brave-instrumentation-httpclient 通過 Apache HttpClient 發送 HTTP 請求時埋點
- brave-instrumentation-mysql 通過 MySQL 驅動訪問 DB 時埋點
- brave-instrumentation-mongodb 訪問 MongoDB 埋點
- brave-instrumentation-dubbo Dubbo 調用時埋點
更多 Brave instrumentation: https://github.com/openzipkin/brave/tree/master/instrumentation
其他非 Java 語言也有對應的埋點庫,參考:https://zipkin.io/pages/tracers_instrumentation.html
數據上報:Reporter
埋點後需要將數據上報,就要通過 Reporter: https://github.com/openzipkin/zipkin-reporter-java
一般創通過 AsyncReporter 創建一個異步上報的 Reporter:
Reporter<Span> reporter = AsyncReporter.create(Sender sender)
其中 Sender 有很多種實現:
- URLConnectionSender 通過 Java 自帶的 HTTP 客戶端上報
- OkHttpSender 通過 OKHttp 上報
- KafkaSender 通過 Kafka 消息隊列上報
- ActiveMQSender 通過 ActiveMQ 消息隊列上報
- RabbitMQSender 通過 RabbitMQ 消息隊列上報
參考代碼:
Reporter<Span> reporter = AsyncReporter.create(URLConnectionSender.create("http://localhost:9411/api/v2/spans"));
Reporter<Span> reporter = AsyncReporter.create(OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans"));
Reporter<Span> reporter = AsyncReporter.create(KafkaSender.create("localhost:9092"));
Reporter<Span> reporter = AsyncReporter.create(ActiveMQSender.create("failover:tcp://localhost:61616"));
Reporter<Span> reporter = AsyncReporter.create(RabbitMQSender.create("localhost:5672"));
數據收集:Collector
數據收集和數據上報是對應的,各個業務代碼將調用鏈數據通過 HTTP、消息隊列 等方式上報後,Zipkin 需要接收這些數據。
https://github.com/openzipkin/zipkin/tree/master/zipkin-server#collector-1
常用 Collector:
- HTTP Collector - 通過
POST /api/v1/spans
和POST /api/v2/spans
接口接收數據 - ActiveMQ Collector - 通過 ActiveMQ 消費者接收數據
- Kafka Collector - 通過 Kafka 消費者接收數據
RabbitMQ collector - 通過 RabbitMQ 消費者接收數據
數據存儲:Storage
https://github.com/openzipkin/zipkin/tree/master/zipkin-server#storage
Zipkin 數據最終保存的地方,主要有:
- In-Memory Storage 直接保存在內存中
- Cassandra Storage 保存在 Cassandra
- Elasticsearch Storage 保存在 Elasticsearch
- MySQL Storage 保存在 MySQL(不推薦使用)