Skip to content

Commit

Permalink
自定义Opentracing字段输出
Browse files Browse the repository at this point in the history
  • Loading branch information
HaojunRen committed Dec 22, 2019
1 parent 7608e5b commit 8b5f72e
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public class StrategyOpentracingConstant {

public static final String SPRING_APPLICATION_STRATEGY_TRACE_OPENTRACING_ENABLED = "spring.application.strategy.trace.opentracing.enabled";
public static final String SPRING_APPLICATION_STRATEGY_TRACE_OPENTRACING_SEPARATE_SPAN_ENABLED = "spring.application.strategy.trace.opentracing.separate.span.enabled";
public static final String SPRING_APPLICATION_STRATEGY_TRACE_OPENTRACING_RULE_OUTPUT_ENABLED = "spring.application.strategy.trace.opentracing.rule.output.enabled";
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public class StrategyOpentracingOperation {
@Value("${" + StrategyOpentracingConstant.SPRING_APPLICATION_STRATEGY_TRACE_OPENTRACING_SEPARATE_SPAN_ENABLED + ":true}")
protected Boolean traceOpentracingSeparateSpanEnabled;

@Value("${" + StrategyOpentracingConstant.SPRING_APPLICATION_STRATEGY_TRACE_OPENTRACING_RULE_OUTPUT_ENABLED + ":false}")
protected Boolean traceOpentracingRuleOutputEnabled;

public void opentracingInitialize() {
if (!traceOpentracingEnabled) {
return;
Expand Down Expand Up @@ -94,25 +97,27 @@ public void opentracingHeader(Map<String, String> customizationMap) {
span.setTag(DiscoveryConstant.N_D_SERVICE_REGION, strategyContextHolder.getHeader(DiscoveryConstant.N_D_SERVICE_REGION));
span.setTag(DiscoveryConstant.N_D_SERVICE_ENVIRONMENT, strategyContextHolder.getHeader(DiscoveryConstant.N_D_SERVICE_ENVIRONMENT));

String routeVersion = strategyContextHolder.getHeader(DiscoveryConstant.N_D_VERSION);
if (StringUtils.isNotEmpty(routeVersion)) {
span.setTag(DiscoveryConstant.N_D_VERSION, routeVersion);
}
String routeRegion = strategyContextHolder.getHeader(DiscoveryConstant.N_D_REGION);
if (StringUtils.isNotEmpty(routeRegion)) {
span.setTag(DiscoveryConstant.N_D_REGION, routeRegion);
}
String routeAddress = strategyContextHolder.getHeader(DiscoveryConstant.N_D_ADDRESS);
if (StringUtils.isNotEmpty(routeAddress)) {
span.setTag(DiscoveryConstant.N_D_ADDRESS, routeAddress);
}
String routeVersionWeight = strategyContextHolder.getHeader(DiscoveryConstant.N_D_VERSION_WEIGHT);
if (StringUtils.isNotEmpty(routeVersionWeight)) {
span.setTag(DiscoveryConstant.N_D_VERSION_WEIGHT, routeVersionWeight);
}
String routeRegionWeight = strategyContextHolder.getHeader(DiscoveryConstant.N_D_REGION_WEIGHT);
if (StringUtils.isNotEmpty(routeRegionWeight)) {
span.setTag(DiscoveryConstant.N_D_REGION_WEIGHT, routeRegionWeight);
if (traceOpentracingRuleOutputEnabled) {
String routeVersion = strategyContextHolder.getHeader(DiscoveryConstant.N_D_VERSION);
if (StringUtils.isNotEmpty(routeVersion)) {
span.setTag(DiscoveryConstant.N_D_VERSION, routeVersion);
}
String routeRegion = strategyContextHolder.getHeader(DiscoveryConstant.N_D_REGION);
if (StringUtils.isNotEmpty(routeRegion)) {
span.setTag(DiscoveryConstant.N_D_REGION, routeRegion);
}
String routeAddress = strategyContextHolder.getHeader(DiscoveryConstant.N_D_ADDRESS);
if (StringUtils.isNotEmpty(routeAddress)) {
span.setTag(DiscoveryConstant.N_D_ADDRESS, routeAddress);
}
String routeVersionWeight = strategyContextHolder.getHeader(DiscoveryConstant.N_D_VERSION_WEIGHT);
if (StringUtils.isNotEmpty(routeVersionWeight)) {
span.setTag(DiscoveryConstant.N_D_VERSION_WEIGHT, routeVersionWeight);
}
String routeRegionWeight = strategyContextHolder.getHeader(DiscoveryConstant.N_D_REGION_WEIGHT);
if (StringUtils.isNotEmpty(routeRegionWeight)) {
span.setTag(DiscoveryConstant.N_D_REGION_WEIGHT, routeRegionWeight);
}
}

LOG.debug("Trace chain information outputs to Opentracing...");
Expand Down Expand Up @@ -152,25 +157,27 @@ public void opentracingLocal(String className, String methodName, Map<String, St
span.setTag(DiscoveryConstant.N_D_SERVICE_REGION, pluginAdapter.getRegion());
span.setTag(DiscoveryConstant.N_D_SERVICE_ENVIRONMENT, pluginAdapter.getEnvironment());

String routeVersion = strategyContextHolder.getHeader(DiscoveryConstant.N_D_VERSION);
if (StringUtils.isNotEmpty(routeVersion)) {
span.setTag(DiscoveryConstant.N_D_VERSION, routeVersion);
}
String routeRegion = strategyContextHolder.getHeader(DiscoveryConstant.N_D_REGION);
if (StringUtils.isNotEmpty(routeRegion)) {
span.setTag(DiscoveryConstant.N_D_REGION, routeRegion);
}
String routeAddress = strategyContextHolder.getHeader(DiscoveryConstant.N_D_ADDRESS);
if (StringUtils.isNotEmpty(routeAddress)) {
span.setTag(DiscoveryConstant.N_D_ADDRESS, routeAddress);
}
String routeVersionWeight = strategyContextHolder.getHeader(DiscoveryConstant.N_D_VERSION_WEIGHT);
if (StringUtils.isNotEmpty(routeVersionWeight)) {
span.setTag(DiscoveryConstant.N_D_VERSION_WEIGHT, routeVersionWeight);
}
String routeRegionWeight = strategyContextHolder.getHeader(DiscoveryConstant.N_D_REGION_WEIGHT);
if (StringUtils.isNotEmpty(routeRegionWeight)) {
span.setTag(DiscoveryConstant.N_D_REGION_WEIGHT, routeRegionWeight);
if (traceOpentracingRuleOutputEnabled) {
String routeVersion = strategyContextHolder.getHeader(DiscoveryConstant.N_D_VERSION);
if (StringUtils.isNotEmpty(routeVersion)) {
span.setTag(DiscoveryConstant.N_D_VERSION, routeVersion);
}
String routeRegion = strategyContextHolder.getHeader(DiscoveryConstant.N_D_REGION);
if (StringUtils.isNotEmpty(routeRegion)) {
span.setTag(DiscoveryConstant.N_D_REGION, routeRegion);
}
String routeAddress = strategyContextHolder.getHeader(DiscoveryConstant.N_D_ADDRESS);
if (StringUtils.isNotEmpty(routeAddress)) {
span.setTag(DiscoveryConstant.N_D_ADDRESS, routeAddress);
}
String routeVersionWeight = strategyContextHolder.getHeader(DiscoveryConstant.N_D_VERSION_WEIGHT);
if (StringUtils.isNotEmpty(routeVersionWeight)) {
span.setTag(DiscoveryConstant.N_D_VERSION_WEIGHT, routeVersionWeight);
}
String routeRegionWeight = strategyContextHolder.getHeader(DiscoveryConstant.N_D_REGION_WEIGHT);
if (StringUtils.isNotEmpty(routeRegionWeight)) {
span.setTag(DiscoveryConstant.N_D_REGION_WEIGHT, routeRegionWeight);
}
}

LOG.debug("Trace chain information outputs to Opentracing...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"type": "java.lang.Boolean",
"defaultValue": true,
"description": "Whether strategy trace opentracing separate span is enabled."
},
{
"name": "spring.application.strategy.trace.opentracing.rule.output.enabled",
"type": "java.lang.Boolean",
"defaultValue": false,
"description": "Whether strategy trace opentracing rule output is enabled."
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import com.nepxion.discovery.plugin.strategy.sentinel.opentracing.constant.SentinelOpenTracingConstant;

public class SentinelOpenTracingProcessorSlotEntryCallback implements ProcessorSlotEntryCallback<DefaultNode> {
private Boolean traceOpentracingSentinelRuleOutputEnabled = Boolean.valueOf(System.getProperty(SentinelOpenTracingConstant.SPRING_APPLICATION_STRATEGY_TRACE_OPENTRACING_SENTINEL_RULE_OUTPUT_ENABLED, "false"));
private Boolean traceOpentracingSentinelArgsOutputEnabled = Boolean.valueOf(System.getProperty(SentinelOpenTracingConstant.SPRING_APPLICATION_STRATEGY_TRACE_OPENTRACING_SENTINEL_ARGS_OUTPUT_ENABLED, "false"));

@Override
public void onPass(Context context, ResourceWrapper resourceWrapper, DefaultNode param, int count, Object... args) throws Exception {

Expand All @@ -38,11 +41,15 @@ public void onBlocked(BlockException e, Context context, ResourceWrapper resourc
span.setTag(SentinelOpenTracingConstant.RESOURCE_TYPE, resourceWrapper.getResourceType());
span.setTag(SentinelOpenTracingConstant.ENTRY_TYPE, resourceWrapper.getEntryType().toString());
span.setTag(SentinelOpenTracingConstant.RULE_LIMIT_APP, e.getRuleLimitApp());
span.setTag(SentinelOpenTracingConstant.RULE, e.getRule().toString());
if (traceOpentracingSentinelRuleOutputEnabled) {
span.setTag(SentinelOpenTracingConstant.RULE, e.getRule().toString());
}
span.setTag(SentinelOpenTracingConstant.CAUSE, e.getClass().getName());
span.setTag(SentinelOpenTracingConstant.BLOCK_EXCEPTION, e.getMessage());
span.setTag(SentinelOpenTracingConstant.COUNT, count);
span.setTag(SentinelOpenTracingConstant.ARGS, JSON.toJSONString(args));
if (traceOpentracingSentinelArgsOutputEnabled) {
span.setTag(SentinelOpenTracingConstant.ARGS, JSON.toJSONString(args));
}
span.finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
*/

public class SentinelOpenTracingConstant {
public static final String SPRING_APPLICATION_STRATEGY_TRACE_OPENTRACING_SENTINEL_RULE_OUTPUT_ENABLED = "spring.application.strategy.trace.opentracing.sentinel.rule.output.enabled";
public static final String SPRING_APPLICATION_STRATEGY_TRACE_OPENTRACING_SENTINEL_ARGS_OUTPUT_ENABLED = "spring.application.strategy.trace.opentracing.sentinel.args.output.enabled";

public static final String SPAN_NAME = "SENTINEL";
public static final String ORIGIN = "origin";
public static final String ASYNC = "async";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ spring.application.strategy.trace.logger.enabled=true
# spring.application.strategy.trace.opentracing.enabled=false
# 启动和关闭调用链的灰度信息在Opentracing中以独立的Span节点输出,如果关闭,则灰度信息输出到原生的Span节点中。缺失则默认为true
# spring.application.strategy.trace.opentracing.separate.span.enabled=true
# 启动和关闭调用链的灰度规则策略信息在Opentracing中的输出。缺失则默认为false
# spring.application.strategy.trace.opentracing.rule.output.enabled=false
# 启动和关闭调用链的Debug日志打印,注意每调用一次都会打印一次,会对性能有所影响,建议压测环境和生产环境关闭。缺失则默认为false
spring.application.strategy.trace.debug.enabled=true
# 开启Spring Cloud Gateway网关上实现Hystrix线程隔离模式做服务隔离时,必须把spring.application.strategy.hystrix.threadlocal.supported设置为true,同时要引入discovery-plugin-strategy-starter-hystrix包,否则线程切换时会发生ThreadLocal上下文对象丢失。缺失则默认为false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ spring.application.strategy.trace.logger.enabled=true
# spring.application.strategy.trace.opentracing.enabled=false
# 启动和关闭调用链的灰度信息在Opentracing中以独立的Span节点输出,如果关闭,则灰度信息输出到原生的Span节点中。缺失则默认为true
# spring.application.strategy.trace.opentracing.separate.span.enabled=true
# 启动和关闭调用链的灰度规则策略信息在Opentracing中的输出。缺失则默认为false
# spring.application.strategy.trace.opentracing.rule.output.enabled=false
# 启动和关闭调用链的Debug日志打印,注意每调用一次都会打印一次,会对性能有所影响,建议压测环境和生产环境关闭。缺失则默认为false
spring.application.strategy.trace.debug.enabled=true
# 开启服务端实现Hystrix线程隔离模式做服务隔离时,必须把spring.application.strategy.hystrix.threadlocal.supported设置为true,同时要引入discovery-plugin-strategy-starter-hystrix包,否则线程切换时会发生ThreadLocal上下文对象丢失。缺失则默认为false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ spring.application.strategy.trace.logger.enabled=true
# spring.application.strategy.trace.opentracing.enabled=false
# 启动和关闭调用链的灰度信息在Opentracing中以独立的Span节点输出,如果关闭,则灰度信息输出到原生的Span节点中。缺失则默认为true
# spring.application.strategy.trace.opentracing.separate.span.enabled=true
# 启动和关闭调用链的灰度规则策略信息在Opentracing中的输出。缺失则默认为false
# spring.application.strategy.trace.opentracing.rule.output.enabled=false
# 启动和关闭调用链的Debug日志打印,注意每调用一次都会打印一次,会对性能有所影响,建议压测环境和生产环境关闭。缺失则默认为false
spring.application.strategy.trace.debug.enabled=true
# 开启Zuul网关上实现Hystrix线程隔离模式做服务隔离时,必须把spring.application.strategy.hystrix.threadlocal.supported设置为true,同时要引入discovery-plugin-strategy-starter-hystrix包,否则线程切换时会发生ThreadLocal上下文对象丢失。缺失则默认为false
Expand Down

0 comments on commit 8b5f72e

Please sign in to comment.