|
Spring Cloud Sleuth 与 OpenTracing 兼容。如果在类路径上有 OpenTracing,我们会自动注册 OpenTracking
如果将逻辑包装在 Runnable runnable = new Runnable() { @Override public void run() { // do some work } @Override public String toString() { return "spanNameFromToStringMethod"; } }; // Manual `TraceRunnable` creation with explicit "calculateTax" Span name Runnable traceRunnable = new TraceRunnable(this.tracing, spanNamer, runnable, "calculateTax"); // Wrapping `Runnable` with `Tracing`. That way the current span will be available // in the thread of `Runnable` Runnable traceRunnableFromTracer = this.tracing.currentTraceContext() .wrap(runnable);
下面的示例展示如何对 Callable<String> callable = new Callable<String>() { @Override public String call() throws Exception { return someLogic(); } @Override public String toString() { return "spanNameFromToStringMethod"; } }; // Manual `TraceCallable` creation with explicit "calculateTax" Span name Callable<String> traceCallable = new TraceCallable<>(this.tracing, spanNamer, callable, "calculateTax"); // Wrapping `Callable` with `Tracing`. That way the current span will be available // in the thread of `Callable` Callable<String> traceCallableFromTracer = this.tracing.currentTraceContext() .wrap(callable); 这样,就可以确保为每次执行创建并关闭一个新的 span。
我们注册了一个名为
假设你有以下 HystrixCommand<String> hystrixCommand = new HystrixCommand<String>(setter) { @Override protected String run() throws Exception { return someLogic(); } };
要传递跟踪信息,必须在 TraceCommand<String> traceCommand = new TraceCommand<String>(tracer, setter) { @Override public String doRun() throws Exception { return someLogic(); } };
我们注册了一个自定义的
可以为不希望为其创建 span 的线程名称定义正则表达式列表。为此,请在
通过将
通过
要更改跟踪过滤器注册的顺序,请设置
要禁用记录未捕获异常的过滤器,可以禁用
因为我们希望 span 名称精确,所以我们使用一个
如果控制器返回一个
通过
要更改跟踪过滤器注册的顺序,请设置
通过与 Brave 的集成,Spring Cloud Sleuth 支持 Dubbo。只需添加 <dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-instrumentation-dubbo-rpc</artifactId> </dependency>
还需要使用以下内容设置 dubbo.provider.filter=tracing dubbo.consumer.filter=tracing 你可以在这里阅读更多关于 Brave - Dubbo 集成的内容。这里有一个 Spring Cloud Sleuth 和 Dubbo 的例子。
我们注入一个
要阻止
有时需要使用异步 Rest Template 的多个实现。在以下代码段中,可以看到如何设置此类自定义 @Configuration @EnableAutoConfiguration static class Config { @Bean(name = "customAsyncRestTemplate") public AsyncRestTemplate traceAsyncRestTemplate() { return new AsyncRestTemplate(asyncClientFactory(), clientHttpRequestFactory()); } private ClientHttpRequestFactory clientHttpRequestFactory() { ClientHttpRequestFactory clientHttpRequestFactory = new CustomClientHttpRequestFactory(); // CUSTOMIZE HERE return clientHttpRequestFactory; } private AsyncClientHttpRequestFactory asyncClientFactory() { AsyncClientHttpRequestFactory factory = new CustomAsyncClientHttpRequestFactory(); // CUSTOMIZE HERE return factory; } }
我们注入了一个
要阻止此功能,请将
如果使用 Traverson 库,可以将 @Autowired RestTemplate restTemplate; Traverson traverson = new Traverson(URI.create("http://some/address"), MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_UTF8).setRestOperations(restTemplate); // use Traverson
我们检测
要阻止这些功能,请将
我们检测 Netty 的
要阻止这些功能,请将
默认情况下,Spring Cloud Sleuth 通过
部分 Feign 监测是通过
Spring Cloud Sleuth 通过
Maven: <dependency> <groupId>io.github.lognet</groupId> <artifactId>grpc-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-instrumentation-grpc</artifactId> </dependency> Gradle: compile("io.github.lognet:grpc-spring-boot-starter")
compile("io.zipkin.brave:brave-instrumentation-grpc")
Spring Cloud Sleuth 利用 grpc-spring-boot-starter 注册 Brave 的 Brave 服务器拦截器,所有服务都用
gRPC 客户端利用
Sleuth 创建了一个
在 Spring Cloud Sleuth 中,我们监测与异步相关的组件,以便在线程之间传递跟踪信息。通过将
如果你使用
在 Spring Cloud Sleuth 中,我们监测计划的方法执行,以便在线程之间传递跟踪信息。通过将
如果你使用
如果要跳过某些
我们提供
下面的示例展示如何在使用 CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> {
// perform some logic
return 1_000_000L;
}, new TraceableExecutorService(beanFactory, executorService,
// 'calculateTax' explicitly names the span - this param is optional
"calculateTax"));
如果有 bean 实现了要从 span 创建中排除的
有时,你需要设置 @Configuration @EnableAutoConfiguration @EnableAsync // add the infrastructure role to ensure that the bean gets auto-proxied @Role(BeanDefinition.ROLE_INFRASTRUCTURE) static class CustomExecutorConfig extends AsyncConfigurerSupport { @Autowired BeanFactory beanFactory; @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); // CUSTOMIZE HERE executor.setCorePoolSize(7); executor.setMaxPoolSize(42); executor.setQueueCapacity(11); executor.setThreadNamePrefix("MyExecutor-"); // DON'T FORGET TO INITIALIZE executor.initialize(); return new LazyTraceExecutor(this.beanFactory, executor); } }
通过将
Spring Cloud Sleuth 集成了 Spring Integration。它为发布和订阅事件创建 span。要禁用 Spring 集成监测,请将
你可以提供
如果要自定义从消息头读取和写入跟踪上下文的方式,则可以注册以下类型的 bean:
我们监测
要阻止此功能,请将
我们对 Spring Kafka 的
要阻止此功能,请将 |