statsd_count
The `statsd_count` directive sends a count metric to a StatsD server, allowing you to track occurrences of specific events.
Description
The statsd_count directive is used within NGINX to increment a specified counter by a defined value whenever a particular event occurs, such as an HTTP request. This metric is sent to a configured StatsD server, enabling monitoring and metrics aggregation for applications. The directive accepts 2 to 3 arguments: the first is the metric key (a string) that identifies the counter within StatsD, the second is the numeric value to increment, and an optional third argument is a condition that determines whether the count should be recorded based on a specified NGINX variable. This allows for more granular control over what events are tallied.
Behavior-wise, if the third parameter is provided, the count will only be sent if the specified NGINX variable evaluates to a truthy value (non-empty or different from 0). This capability is particularly useful when you want to avoid incrementing counters for cases that are considered invalid or not useful. Overall, this directive facilitates efficient monitoring of application performance metrics in a production environment.
Config Example
statsd_count "your_product.requests" 1; statsd_count "your_product.pages.index_requests" 1; statsd_count "your_product.pages.index_responses" 1 "$request_completion";
Ensure that the StatsD server is properly configured and reachable from NGINX to avoid lost metrics.
Be cautious with the third argument; if the condition variable is not defined or incorrectly specified, the count may not increment when expected.