Updates to latest Sampling infrastructure (#1456)
* Updates to latest Sampling infrastructure
Brave recently switched to an interface model for higher level sampling
like HTTP. So, `HttpSampler` -> `SamplingFunction<HttpRequest>`. Don't
worry because `HttpSampler` was retrofitted as a `SamplingFunction`.
This change moves to the higher interface, avoiding deprecated methods
and such. More interestingly, this weaves in support for `RpcTracing`,
which *finally* introduces RPC sampling the same way. Specifically, this
adds `SamplingFunction<RpcRequest>` under the same conventions as HTTP.
Most immediately, this can be used here in gRPC and Dubbo, as
autoconfiguration exists. It also works with any autoconfiguration that
isn't here, such as Armeria.
Ex. Here's a sampler that traces 100 "GetUserToken" requests per second. This
doesn't start new traces for requests to the health check service. Other
requests will use a global rate provided by the tracing component.
```java
import static brave.rpc.RpcRequestMatchers.methodEquals;
import static brave.rpc.RpcRequestMatchers.serviceEquals;
import static brave.sampler.Matchers.and;
--snip--
@Bean(name = ServerSampler.NAME)
SamplerFunction<RpcRequest> myRpcSampler() {
Matcher<RpcRequest> userAuth = and(
serviceEquals("users.UserService"),
methodEquals("GetUserToken")
);
return RpcRuleSampler.newBuilder()
.putRule(serviceEquals("grpc.health.v1.Health"), Sampler.NEVER_SAMPLE)
.putRule(userAuth, RateLimitingSampler.create(100)).build();
}
```
This commit is contained in:
committed by
Marcin Grzejszczak
parent
dc82c5cafc
commit
be5fa04118
@@ -838,21 +838,23 @@ Sleuth will search for beans of those types and automatically apply customizatio
|
||||
|
||||
=== HTTP
|
||||
|
||||
If a customization of client / server parsing of the HTTP related spans is required,
|
||||
just register a bean of type `brave.http.HttpClientParser` or
|
||||
If a customization of client / server parsing of the HTTP related spans is
|
||||
required, just register a bean of type `brave.http.HttpClientParser` or
|
||||
`brave.http.HttpServerParser`. If client /server sampling is required, just
|
||||
register a bean of type `brave.http.HttpSampler` and name the bean
|
||||
`sleuthClientSampler` for client sampler and `sleuthServerSampler` for server sampler.
|
||||
For your convenience the `@ClientSampler` and `@ServerSampler`
|
||||
annotations can be used to inject the proper beans or to
|
||||
reference the bean names via their static String `NAME` fields.
|
||||
register a bean of type `brave.sampler.SamplerFunction<HttpRequest>` and name
|
||||
the bean `sleuthHttpClientSampler` for client sampler and
|
||||
`sleuthHttpServerSampler` for server sampler.
|
||||
|
||||
For your convenience the `@HttpClientSampler` and `@HttpServerSampler`
|
||||
annotations can be used to inject the proper beans or to reference the bean
|
||||
names via their static String `NAME` fields.
|
||||
|
||||
Check out Brave's code to see an example of how to make a path-based sampler
|
||||
https://github.com/openzipkin/brave/tree/master/instrumentation/http#sampling-policy
|
||||
|
||||
If you want to completely rewrite the `HttpTracing` bean you can use the `SkipPatternProvider`
|
||||
interface to retrieve the URL `Pattern` for spans that should be not sampled. Below you can see
|
||||
an example of usage of `SkipPatternProvider` inside a server side, `HttpSampler`.
|
||||
an example of usage of `SkipPatternProvider` inside a server side, `Sampler<HttpRequest>`.
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@@ -874,6 +876,34 @@ In the following example, we register the `TracingFilter` bean, add the `ZIPKIN-
|
||||
include::{project-root}/tests/spring-cloud-sleuth-instrumentation-mvc-tests/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceFilterIntegrationTests.java[tags=response_headers,indent=0]
|
||||
----
|
||||
|
||||
=== RPC
|
||||
|
||||
Sleuth automatically configures the `RpcTracing` bean which serves as a
|
||||
foundation for RPC instrumentation such as gRPC or Dubbo.
|
||||
|
||||
If a customization of client / server sampling of the RPC traces is required,
|
||||
just register a bean of type `brave.sampler.SamplerFunction<RpcRequest>` and
|
||||
name the bean `sleuthRpcClientSampler` for client sampler and
|
||||
`sleuthRpcServerSampler` for server sampler.
|
||||
|
||||
For your convenience the `@RpcClientSampler` and `@RpcServerSampler`
|
||||
annotations can be used to inject the proper beans or to reference the bean
|
||||
names via their static String `NAME` fields.
|
||||
|
||||
Ex. Here's a sampler that traces 100 "GetUserToken" server requests per second.
|
||||
This doesn't start new traces for requests to the health check service. Other
|
||||
requests will use the global sampling configuration.
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@Configuration
|
||||
class Config {
|
||||
include::{project-root}/tests/spring-cloud-sleuth-instrumentation-rpc-tests/src/test/java/org/springframework/cloud/sleuth/instrument/rpc/TraceRpcAutoConfigurationIntegrationTests.java[tags=custom_rpc_server_sampler,indent=2]
|
||||
}
|
||||
----
|
||||
|
||||
For more, see https://github.com/openzipkin/brave/tree/master/instrumentation/rpc#sampling-policy
|
||||
|
||||
=== Custom service name
|
||||
|
||||
By default, Sleuth assumes that, when you send a span to Zipkin, you want the span's service name to be equal to the value of the `spring.application.name` property.
|
||||
@@ -1113,13 +1143,13 @@ To change the order of tracing filter registration, please set the
|
||||
==== Dubbo RPC support
|
||||
|
||||
Via the integration with Brave, Spring Cloud Sleuth supports https://dubbo.apache.org/[Dubbo].
|
||||
It's enough to add the `brave-instrumentation-dubbo-rpc` dependency:
|
||||
It's enough to add the `brave-instrumentation-dubbo` dependency:
|
||||
|
||||
[source,xml,indent=0]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>io.zipkin.brave</groupId>
|
||||
<artifactId>brave-instrumentation-dubbo-rpc</artifactId>
|
||||
<artifactId>brave-instrumentation-dubbo</artifactId>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
|
||||
Reference in New Issue
Block a user