statsd_timing

The `statsd_timing` directive sends timing data to a StatsD server for performance monitoring.

Syntaxstatsd_timing key value [condition];
Defaultnone
Contextserver, location, if in server, if in location
Arguments2-3

Description

The statsd_timing directive in NGINX is part of the NGINX statsd module, enabling users to send timing metrics to a StatsD server. This directive allows you to log how long a request takes to process, offering valuable insight into response times and performance monitoring. The timing information is sent as a UDP packet, which carries the key and value representing the timing data evaluated from an NGINX variable or a static number.

The syntax for the directive accepts two or three arguments: the key to identify the metric and the value representing the timing, which can be a variable (like $upstream_response_time) that holds the duration to be logged. The third optional argument provides a condition for when the timing information should be sent — if this evaluates to true, the timing will be sent; if not, it will be ignored. This condition is beneficial for avoiding unnecessary logging when specific criteria are not met, improving efficiency and reducing noise in metrics.

It is important to ensure that an active StatsD server is configured using the statsd_server directive in your NGINX configuration, which is required for this directive to function. By efficiently logging request timings, developers can identify bottlenecks in their applications and optimize performance based on comprehensive data over time.

Config Example

location / {
    statsd_timing "your_product.pages.index_response_time" "$upstream_response_time";
}

Ensure the StatsD server is correctly configured and reachable, otherwise, timing metrics will not be sent.

Using a variable for timing may result in no timing data being sent if the variable evaluates to zero or an empty string; be sure to handle such cases appropriately.

Remember to set the statsd_sample_rate if you want to avoid overwhelming your StatsD server.

← Back to all directives