From f306670a49cc971389cdc332ff96c3f1b2d3c72b Mon Sep 17 00:00:00 2001 From: cn Date: Tue, 29 Jan 2019 14:06:10 +0100 Subject: [PATCH] Add guard to avg_request_time when requests_time_num is 0 avoiding NaN metric --- metrics_graphite.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/metrics_graphite.lua b/metrics_graphite.lua index 26fc2d4..3474860 100644 --- a/metrics_graphite.lua +++ b/metrics_graphite.lua @@ -104,7 +104,10 @@ function MetricsGraphite:worker() return end - local avg_request_time = this.stats:get("request_time_sum") / this.stats:get("request_time_num") + local avg_request_time = nil + if this.stats:get("request_time_num") > 0 then + avg_request_time = this.stats:get("request_time_sum") / this.stats:get("request_time_num") + end self.stats:set("request_time_sum", 0) self.stats:set("request_time_num", 0) @@ -117,7 +120,9 @@ function MetricsGraphite:worker() sock:send(this.mbase .. ".nginx_metrics.acc_request_length " .. this.stats:get("request_length") .. " " .. ngx.time() .. "\n") sock:send(this.mbase .. ".nginx_metrics.acc_bytes_sent " .. this.stats:get("bytes_sent") .. " " .. ngx.time() .. "\n") - sock:send(this.mbase .. ".nginx_metrics.avg_request_time " .. avg_request_time .. " " .. ngx.time() .. "\n") + if avg_request_time then + sock:send(this.mbase .. ".nginx_metrics.avg_request_time " .. avg_request_time .. " " .. ngx.time() .. "\n") + end for k,_ in pairs(self.query_status) do sock:send(this.mbase .. ".nginx_metrics.num_" .. k .. " " .. this.stats:get(k) .. " " .. ngx.time() .. "\n")