Package brave.http

Class HttpRuleSampler

  • All Implemented Interfaces:
    brave.sampler.SamplerFunction<HttpRequest>

    public final class HttpRuleSampler
    extends HttpSampler
    implements brave.sampler.SamplerFunction<HttpRequest>
    Assigns sample rates to http routes.

    Ex. Here's a sampler that traces 100 requests per second to /foo and 10 POST requests to /bar per second. This doesn't start new traces for requests to favicon (which many browsers automatically fetch). Other requests will use a global rate provided by the tracing component.

    
     import static brave.http.HttpRequestMatchers.methodIsEqualTo;
     import static brave.http.HttpRequestMatchers.pathStartsWith;
     import static brave.sampler.Matchers.and;
    
     httpTracingBuilder.serverSampler(HttpRuleSampler.newBuilder()
       .putRule(pathStartsWith("/favicon"), Sampler.NEVER_SAMPLE)
       .putRule(pathStartsWith("/foo"), RateLimitingSampler.create(100))
       .putRule(and(methodIsEqualTo("POST"), pathStartsWith("/bar")), RateLimitingSampler.create(10))
       .build());
     

    Ex. Here's a custom matcher for the endpoint "/play&country=US"

    
     Matcher<HttpRequest> playInTheUSA = request -> {
       if (!"/play".equals(request.path())) return false;
       String url = request.url();
       if (url == null) return false;
       String query = URI.create(url).getQuery();
       return query != null && query.contains("country=US");
     };
     

    Implementation notes

    Be careful when implementing matchers as HttpRequest methods can return null.
    Since:
    4.4