statsd_sample_rate
Sets the sample rate for metrics sent to StatsD, allowing for controlled sampling of statistics.
Description
The statsd_sample_rate directive specifies the percentage of requests that should be sampled and sent to the StatsD server. This is particularly useful for reducing the load on the StatsD server when generating high traffic, as it allows you to report metrics on a subset of requests instead of sending every single metric. For instance, a sample rate of '10' means that, on average, 10% of all metrics will be sent. The directive accepts an integer value as its argument, representing the sample rate percentage.
When defining the directive, it can be placed in the http, server, or location contexts. This flexibility allows users to control sampling at different scopes within the NGINX configuration. If not set, the default behavior is to send all metrics (100%), which can lead to high bandwidth usage and potential overload of the StatsD server. The sample rate is treated as a simple integer and will directly scale down the metrics sent based on the provided percentage.
This directive plays an important role in performance tuning and monitoring environments. In cases where applications generate a large volume of data, using statsd_sample_rate can significantly decrease the metrics being sent, while still allowing for gathering sufficient insights from the sampled data.
Config Example
http {
statsd_server your.statsd.server.com;
statsd_sample_rate 10; # Sample 10% of requests
server {
location / {
statsd_count "your_product.requests" 1;
}
}
}Setting the sample rate to 0 will stop all metrics from being sent. Ensure a valid percentage is used.
Be aware that the percentage is an integer and should not exceed 100, as values above 100 may lead to undefined behavior.