當網關和服務在實施全鏈路分布式灰度發布和路由時候,我們需要一款追蹤系統來監控網關和服務走的是哪個灰度組,哪個灰度版本,哪個灰度區域,甚至監控從Http Header頭部全程傳遞的灰度規則和路由策略。這個功能意義在於:
- 不僅可以監控全鏈路中基本的調用信息,也可以監控額外的灰度信息,有助於我們判斷灰度發布和路由是否執行准確,一旦有問題,也可以快速定位
- 可以監控流量何時切換到新版本,或者新的區域,或者新的機器上
- 可以監控灰度規則和路由策略是否配置准確
- 可以監控網關和服務灰度上下級樹狀關系
- 可以監控全鏈路流量拓撲圖
筆者嘗試調研了一系列分布式追蹤系統和中間件,包括Opentracing、Uber Jaeger、Twitter Zipkin、Apache Skywalking、Pinpoint、CAT等,最后決定采用Opentracing + Uber Jaeger方式來實現,重要原因除了易用性和可擴展性外,Opentracing支持WebMvc和WebFlux兩種方式,業界的追蹤系統能支持WebFlux相對較少
[OpenTracing] OpenTracing已進入CNCF,正在為全球的分布式追蹤系統提供統一的概念、規范、架構和數據標准。它通過提供平台無關、廠商無關的API,使得開發人員能夠方便的添加(或更換)追蹤系統的實現。對於存在多樣化的技術棧共存的調用鏈中,Opentracing適配Java、C、Go和.Net等技術棧,實現全鏈路分布式追蹤功能。迄今為止,Uber Jaeger、Twitter Zipkin和Apache Skywalking已經適配了Opentracing規范
筆者以Nepxion社區的Discovery開源框架(對該開源框架感興趣的同學,請訪問如下鏈接)為例子展開整合
源碼主頁,請訪問
https://github.com/Nepxion/Discovery
指南主頁,請訪問
https://github.com/Nepxion/DiscoveryGuide
文檔主頁,請訪問
https://pan.baidu.com/s/1i57rXaNKPuhGRqZ2MONZOA#list/path=%2FNepxion
整合的效果圖
基本概念
灰度調用鏈主要包括如下11個參數。使用者可以自行定義要傳遞的調用鏈參數,例如:traceId, spanId等;也可以自行定義要傳遞的業務調用鏈參數,例如:mobile, user等
1. n-d-service-group - 服務所屬組或者應用
2. n-d-service-type - 服務類型,分為“網關”和“服務”
3. n-d-service-id - 服務ID
4. n-d-service-address - 服務地址,包括Host和Port
5. n-d-service-version - 服務版本
6. n-d-service-region - 服務所屬區域
7. n-d-version - 版本路由值
8. n-d-region - 區域路由值
9. n-d-address - 地址路由值
10. n-d-version-weight - 版本權重路由值
11. n-d-region-weight - 區域權重路由值
核心實現
Opentracing通用模塊
源碼參考
https://github.com/Nepxion/Discovery/tree/master/discovery-plugin-strategy-opentracing
由於OpenTracing擴展需要兼顧到Spring Cloud Gateway、Zuul和服務,它的核心邏輯存在着一定的可封裝性,所以筆者抽取出一個公共模塊discovery-plugin-strategy-opentracing,包含configuration、operation、context等模塊,着重闡述operation模塊,其它比較簡單,不一一贅述了
在闡述前,筆者需要解釋一個配置,該配置將決定核心實現以及終端界面的顯示
- 如果開啟,灰度信息輸出到獨立的Span節點中,意味着在界面顯示中,灰度信息通過獨立的GRAY Span節點來顯示。優點是信息簡潔明了,缺點是Span節點會增長一倍。我們可以稱呼它為【模式A】
- 如果關閉,灰度信息輸出到原生的Span節點中,意味着在界面顯示中,灰度信息會和原生Span節點的調用信息、協議信息等混在一起,缺點是信息龐雜混合,優點是Span節點數不會增長。我們可以稱呼它為【模式B】
# 啟動和關閉調用鏈的灰度信息在Opentracing中以獨立的Span節點輸出,如果關閉,則灰度信息輸出到原生的Span節點中。缺失則默認為true
spring.application.strategy.trace.opentracing.separate.span.enabled=true
Opentracing公共操作類 - StrategyOpentracingOperation.java
- 裝配注入Opentracing的Tracer對象
- opentracingInitialize方法,提供給網關和服務的Span節點初始化
- 【模式A】下,tracer.buildSpan(...).start()實現新建一個Span,並把它放置到存儲上下文的StrategyOpentracingContext的ThreadLocal里
- 【模式B】下,不需要做任何工作
- opentracingHeader方法,提供給網關的灰度調用鏈輸出
- 【模式A】下,首先從StrategyOpentracingContext的ThreadLocal里獲取Span對象,其次把customizationMap(自定義的調用鏈參數)的元素都放入到Tag中,最后把灰度調用鏈主11個參數(通過strategyContextHolder.getHeader(...)獲取)和更多上下文信息放入到Tag中
- 【模式B】下,跟【模式A】類似,唯一區別的是Tags.COMPONENT的處理,由於原生的Span節點已經帶有該信息,所以不需要放入到Tag中
- opentracingLocal方法,提供給服務的灰度調用鏈輸出
- 【模式A】下,首先從StrategyOpentracingContext的ThreadLocal里獲取Span對象,其次把customizationMap(自定義的調用鏈參數)的元素都放入到Tag中,最后把灰度調用鏈主11個參數(通過pluginAdapter.getXXX()獲取)和更多上下文信息放入到Tag中
- 【模式B】下,跟【模式A】類似,唯一區別的是Tags.COMPONENT的處理,由於原生的Span節點已經帶有該信息,所以不需要放入到Tag中
- opentracingError方法,提供給服務的灰度調用鏈異常輸出
- 【模式A】下,首先從StrategyOpentracingContext的ThreadLocal里獲取Span對象,其次span.log(...)方法實現異常輸出
- 【模式B】下,不需要做任何工作
- opentracingClear方法,灰度調用鏈的Span上報和清除
- 【模式A】下,首先從StrategyOpentracingContext的ThreadLocal里獲取Span對象,其次span.finish()方法實現Span上報,最后StrategyOpentracingContext.clearCurrentContext()方法實現Span清除
- 【模式B】下,不需要做任何工作
- getCurrentSpan方法
- 【模式A】下,返回StrategyOpentracingContext.getCurrentContext().getSpan(),即opentracingInitialize新建的Span對象
- 【模式B】下,返回tracer.activeSpan(),即原生的Span對象
public class StrategyOpentracingOperation {
private static final Logger LOG = LoggerFactory.getLogger(StrategyOpentracingOperation.class);
@Autowired
protected PluginAdapter pluginAdapter;
@Autowired
protected StrategyContextHolder strategyContextHolder;
@Autowired
private Tracer tracer;
@Value("${" + StrategyOpentracingConstant.SPRING_APPLICATION_STRATEGY_TRACE_OPENTRACING_ENABLED + ":false}")
protected Boolean traceOpentracingEnabled;
@Value("${" + StrategyOpentracingConstant.SPRING_APPLICATION_STRATEGY_TRACE_OPENTRACING_SEPARATE_SPAN_ENABLED + ":true}")
protected Boolean traceOpentracingSeparateSpanEnabled;
public void opentracingInitialize() {
if (!traceOpentracingEnabled) {
return;
}
if (!traceOpentracingSeparateSpanEnabled) {
return;
}
Span span = tracer.buildSpan(DiscoveryConstant.SPAN_VALUE).start();
StrategyOpentracingContext.getCurrentContext().setSpan(span);
LOG.debug("Trace chain for Opentracing initialized...");
}
public void opentracingHeader(Map<String, String> customizationMap) {
if (!traceOpentracingEnabled) {
return;
}
Span span = getCurrentSpan();
if (span == null) {
LOG.error("Span not found in context to opentracing header");
return;
}
if (MapUtils.isNotEmpty(customizationMap)) {
for (Map.Entry<String, String> entry : customizationMap.entrySet()) {
span.setTag(entry.getKey(), entry.getValue());
}
}
if (traceOpentracingSeparateSpanEnabled) {
span.setTag(Tags.COMPONENT.getKey(), DiscoveryConstant.TAG_COMPONENT_VALUE);
}
span.setTag(DiscoveryConstant.PLUGIN, DiscoveryConstant.PLUGIN_VALUE);
span.setTag(DiscoveryConstant.TRACE_ID, span.context().toTraceId());
span.setTag(DiscoveryConstant.SPAN_ID, span.context().toSpanId());
span.setTag(DiscoveryConstant.N_D_SERVICE_GROUP, strategyContextHolder.getHeader(DiscoveryConstant.N_D_SERVICE_GROUP));
...
String routeVersion = strategyContextHolder.getHeader(DiscoveryConstant.N_D_VERSION);
if (StringUtils.isNotEmpty(routeVersion)) {
span.setTag(DiscoveryConstant.N_D_VERSION, routeVersion);
}
...
LOG.debug("Trace chain information outputs to Opentracing...");
}
public void opentracingLocal(String className, String methodName, Map<String, String> customizationMap) {
if (!traceOpentracingEnabled) {
return;
}
Span span = getCurrentSpan();
if (span == null) {
LOG.error("Span not found in context to opentracing local");
return;
}
if (MapUtils.isNotEmpty(customizationMap)) {
for (Map.Entry<String, String> entry : customizationMap.entrySet()) {
span.setTag(entry.getKey(), entry.getValue());
}
}
if (traceOpentracingSeparateSpanEnabled) {
span.setTag(Tags.COMPONENT.getKey(), DiscoveryConstant.TAG_COMPONENT_VALUE);
}
span.setTag(DiscoveryConstant.PLUGIN, DiscoveryConstant.PLUGIN_VALUE);
span.setTag(DiscoveryConstant.CLASS, className);
span.setTag(DiscoveryConstant.METHOD, methodName);
span.setTag(DiscoveryConstant.TRACE_ID, span.context().toTraceId());
span.setTag(DiscoveryConstant.SPAN_ID, span.context().toSpanId());
span.setTag(DiscoveryConstant.N_D_SERVICE_GROUP, pluginAdapter.getGroup());
...
String routeVersion = strategyContextHolder.getHeader(DiscoveryConstant.N_D_VERSION);
if (StringUtils.isNotEmpty(routeVersion)) {
span.setTag(DiscoveryConstant.N_D_VERSION, routeVersion);
}
...
LOG.debug("Trace chain information outputs to Opentracing...");
}
public void opentracingError(String className, String methodName, Throwable e) {
if (!traceOpentracingEnabled) {
return;
}
if (!traceOpentracingSeparateSpanEnabled) {
return;
}
Span span = getCurrentSpan();
if (span == null) {
LOG.error("Span not found in context to opentracing error");
return;
}
span.log(new ImmutableMap.Builder<String, Object>()
.put(DiscoveryConstant.CLASS, className)
.put(DiscoveryConstant.METHOD, methodName)
.put(DiscoveryConstant.EVENT, Tags.ERROR.getKey())
.put(DiscoveryConstant.ERROR_OBJECT, e)
.build());
LOG.debug("Trace chain error outputs to Opentracing...");
}
public void opentracingClear() {
if (!traceOpentracingEnabled) {
return;
}
if (!traceOpentracingSeparateSpanEnabled) {
return;
}
Span span = getCurrentSpan();
if (span != null) {
span.finish();
} else {
LOG.error("Span not found in context to opentracing clear");
}
StrategyOpentracingContext.clearCurrentContext();
LOG.debug("Trace chain context of Opentracing cleared...");
}
public Span getCurrentSpan() {
return traceOpentracingSeparateSpanEnabled ? StrategyOpentracingContext.getCurrentContext().getSpan() : tracer.activeSpan();
}
public String getTraceId() {
if (!traceOpentracingEnabled) {
return null;
}
Span span = getCurrentSpan();
if (span != null) {
return span.context().toTraceId();
}
return null;
}
public String getSpanId() {
if (!traceOpentracingEnabled) {
return null;
}
Span span = getCurrentSpan();
if (span != null) {
return span.context().toSpanId();
}
return null;
}
}
Opentracing Service模塊
實現OpenTracing對服務的擴展,包含configuration、tracer等模塊,着重闡述tracer模塊,其它比較簡單,不一一贅述了
Opentracing的服務追蹤類 - DefaultServiceStrategyOpentracingTracer.java
- 繼承DefaultServiceStrategyTracer,並注入StrategyOpentracingOperation
- trace方法里先執行opentracingInitialize初始化Span,這樣可以讓后面的邏輯都可以從Span中拿到traceId和spanId,執行opentracingLocal實現服務的灰度調用鏈輸出
- error方法里執行opentracingError實現服務的灰度調用鏈異常輸出
- release方法里執行opentracingClear實現灰度調用鏈的Span上報和清除
public class DefaultServiceStrategyOpentracingTracer extends DefaultServiceStrategyTracer {
@Autowired
private StrategyOpentracingOperation strategyOpentracingOperation;
@Override
public void trace(ServiceStrategyTracerInterceptor interceptor, MethodInvocation invocation) {
strategyOpentracingOperation.opentracingInitialize();
super.trace(interceptor, invocation);
strategyOpentracingOperation.opentracingLocal(interceptor.getMethod(invocation).getDeclaringClass().getName(), interceptor.getMethodName(invocation), getCustomizationMap());
}
@Override
public void error(ServiceStrategyTracerInterceptor interceptor, MethodInvocation invocation, Throwable e) {
super.error(interceptor, invocation, e);
strategyOpentracingOperation.opentracingError(interceptor.getMethod(invocation).getDeclaringClass().getName(), interceptor.getMethodName(invocation), e);
}
@Override
public void release(ServiceStrategyTracerInterceptor interceptor, MethodInvocation invocation) {
super.release(interceptor, invocation);
strategyOpentracingOperation.opentracingClear();
}
@Override
public String getTraceId() {
return strategyOpentracingOperation.getTraceId();
}
@Override
public String getSpanId() {
return strategyOpentracingOperation.getSpanId();
}
}
Opentracing Spring Cloud Gateway模塊
實現OpenTracing對Spring Cloud Gateway的擴展,跟discovery-plugin-strategy-starter-service-opentracing模塊類似,不一一贅述了
Opentracing Zuul模塊
源碼參考
https://github.com/Nepxion/Discovery/tree/master/discovery-plugin-strategy-starter-zuul-opentracing
實現OpenTracing對Zuul的擴展,跟discovery-plugin-strategy-starter-service-opentracing模塊類似,不一一贅述了
使用說明
示例參考
https://github.com/Nepxion/DiscoveryGuide
使用方式
Opentracing輸出方式以Uber Jaeger為例來說明,步驟非常簡單
- 從https://pan.baidu.com/s/1i57rXaNKPuhGRqZ2MONZOA#list/path=%2FNepxion獲取Jaeger-1.14.0.zip,Windows操作系統下解壓后運行jaeger.bat,Mac和Lunix操作系統請自行研究
- 執行Postman調用后,訪問http://localhost:16686查看灰度調用鏈
- 灰度調用鏈支持WebMvc和WebFlux兩種方式,以GRAY字樣的標記來標識
開關控制
對於Opentracing調用鏈功能的開啟和關閉,需要通過如下開關做控制:
# 啟動和關閉調用鏈。缺失則默認為false
spring.application.strategy.trace.enabled=true
# 啟動和關閉調用鏈的Opentracing輸出,支持F版或更高版本的配置,其它版本不需要該行配置。缺失則默認為false
spring.application.strategy.trace.opentracing.enabled=true
# 啟動和關閉調用鏈的灰度信息在Opentracing中以獨立的Span節點輸出,如果關閉,則灰度信息輸出到原生的Span節點中。缺失則默認為true
spring.application.strategy.trace.opentracing.separate.span.enabled=true
可選功能
自定義調用鏈上下文參數的創建(該類不是必須的),繼承DefaultStrategyTracerAdapter
// 自定義調用鏈上下文參數的創建
// 對於getTraceId和getSpanId方法,在Opentracing等調用鏈中間件引入的情況下,由調用鏈中間件決定,在這里定義不會起作用;在Opentracing等調用鏈中間件未引入的情況下,在這里定義才有效,下面代碼中表示從Http Header中獲取,並全鏈路傳遞
// 對於getCustomizationMap方法,表示輸出到調用鏈中的定制化業務參數,可以同時輸出到日志和Opentracing等調用鏈中間件,下面代碼中表示從Http Header中獲取,並全鏈路傳遞
public class MyStrategyTracerAdapter extends DefaultStrategyTracerAdapter {
@Override
public String getTraceId() {
return StringUtils.isNotEmpty(strategyContextHolder.getHeader(DiscoveryConstant.TRACE_ID)) ? strategyContextHolder.getHeader(DiscoveryConstant.TRACE_ID) : StringUtils.EMPTY;
}
@Override
public String getSpanId() {
return StringUtils.isNotEmpty(strategyContextHolder.getHeader(DiscoveryConstant.SPAN_ID)) ? strategyContextHolder.getHeader(DiscoveryConstant.SPAN_ID) : StringUtils.EMPTY;
}
@Override
public Map<String, String> getCustomizationMap() {
return new ImmutableMap.Builder<String, String>()
.put("mobile", StringUtils.isNotEmpty(strategyContextHolder.getHeader("mobile")) ? strategyContextHolder.getHeader("mobile") : StringUtils.EMPTY)
.put("user", StringUtils.isNotEmpty(strategyContextHolder.getHeader("user")) ? strategyContextHolder.getHeader("user") : StringUtils.EMPTY)
.build();
}
}
在配置類里@Bean方式進行調用鏈類創建,覆蓋框架內置的調用鏈類
@Bean
public StrategyTracerAdapter strategyTracerAdapter() {
return new MyStrategyTracerAdapter();
}
本文作者
任浩軍, 10 多年開源經歷,Github ID:@HaojunRen,Nepxion 開源社區創始人,Nacos Group Member,Spring Cloud Alibaba & Nacos & Sentinel Committer
請聯系我
微信、公眾號和文檔
轉載,請保留原文地址,謝謝 ~