# Sampling and blocking requests

## Sampling

### How it works

It is possible that while using Hypertest SDK the latency of your APIs increase. Hypertest provides a mechanism called sampling to deal wiith this. There are two ways by which Hypertest SDK samples.

First is by telling the SDK to stop recording the requests for a certain cool off period. You tell us which requests should be sampled by providing a maximum response time. The requests taking more than maximum response time are marked to be sampled. So, the next time such a request comes it wont be recorded, decreasing your latency.

Second way is by specifying the sampling rate for requests. Example: A sampling rate of 65 percent would mean, out of all the requests roughly 65 percent would actually end up recorded, other 35 would be blocked.

These two methods can be used together and are additive in nature. Sampling config must be defined at the initialization of SDK.

### Code

{% code overflow="wrap" %}

```typescript
new HypertestAgentBuilder("<HT_SERVICE_ID>", "<YOUR_SERVICE_NAME>",  "<API_KEY>", "<LOGGER_URL>", "<APPLICATION_CLASS>")
.addHttpSamplingConfig(".*", "GET", 1.0, 10*1000, 5000)
.addHttpSamplingConfig("/api", "*", 0.5, 10*1000, 1000)
.build()
```

{% endcode %}

Arguments:

1. Path: You can provide a string or a regex (using Pattern class) to tell which requests should be sampled.
2. Method: Type of request (GET, POST, PUT, etc). This is case insensitive.
3. Sampling rate: The amount of requests to be sampled.
4. Max response time (ms): Requests taking more than this time would be sampled.
5. Cool off period (ms): Requests taking more than max response time would be sampled for this cool off time period only.

You can provide a list of configs, each targeting a group of requests. The sdk starts matching the requests from top to bottom. There exists a default config for sampling at the bottom of the list by default that looks like this. It matches with all the requests, providing a fallback in case no configs match.

```typescript
{
  path: '.*',
  method: '*',
  samplingRate: 1,
  maxResponseTimeMs: 10 * 1000,
  coolOffPeriodMs: 60 * 1000
}
```

## Blocking requests

Hypertest SDK allows you to specify a config to stop recording certain requests. You can specify the method and the path of the request you want to be be blocked.

```typescript
new HypertestAgentBuilder("<HT_SERVICE_ID>", "<YOUR_SERVICE_NAME>",
 "<API_KEY>", "<LOGGER_URL>", "<APPLICATION_CLASS>")
.addHttpBlockRequestsConfig("/sampling/blocking", "*")
.addHttpBlockRequestsConfig("/test/blocking", "PUT")
.build()
```

Arguments:

1. Path: You can provide a string or a regex (using Pattern class) to tell which requests should be blocked.
2. Method: Type of request (GET, POST, PUT, etc). This is case insensitive.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-v2.hypertest.co/user-guides/java-sdk/sampling-and-blocking-requests.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
