This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 4cd1edc  refactor: Method for latency calculation and related 
refactoring (#5480)
4cd1edc is described below

commit 4cd1edcc39a1733c0ea10e2eccc324db47a112c8
Author: Bisakh <[email protected]>
AuthorDate: Fri Nov 12 12:57:27 2021 +0530

    refactor: Method for latency calculation and related refactoring (#5480)
---
 apisix/plugins/datadog.lua             | 12 ++++--------
 apisix/plugins/prometheus/exporter.lua | 17 ++++-------------
 apisix/utils/log-util.lua              | 19 +++++++++++++++++++
 3 files changed, 27 insertions(+), 21 deletions(-)

diff --git a/apisix/plugins/datadog.lua b/apisix/plugins/datadog.lua
index 0a6a134..6a55886 100644
--- a/apisix/plugins/datadog.lua
+++ b/apisix/plugins/datadog.lua
@@ -18,6 +18,7 @@ local core = require("apisix.core")
 local plugin = require("apisix.plugin")
 local batch_processor = require("apisix.utils.batch-processor")
 local fetch_log = require("apisix.utils.log-util").get_full_log
+local latency_details = require("apisix.utils.log-util").latency_details_in_ms
 local service_fetch = require("apisix.http.service").get
 local ngx = ngx
 local udp = ngx.socket.udp
@@ -139,7 +140,7 @@ function _M.log(conf, ctx)
     end
 
     local entry = fetch_log(ngx, {})
-    entry.upstream_latency = ctx.var.upstream_response_time * 1000
+    entry.latency, entry.upstream_latency, entry.apisix_latency = 
latency_details(ctx)
     entry.balancer_ip = ctx.balancer_ip or ""
     entry.scheme = ctx.upstream_scheme or ""
 
@@ -215,23 +216,18 @@ function _M.log(conf, ctx)
             end
 
             -- upstream latency
-            local apisix_latency = entry.latency
             if entry.upstream_latency then
                 local ok, err = sock:send(format("%s:%s|%s%s", prefix ..
-                                        "upstream.latency", 
entry.upstream_latency, "h", suffix))
+                                "upstream.latency", entry.upstream_latency, 
"h", suffix))
                 if not ok then
                     core.log.error("failed to report upstream latency to 
dogstatsd server: host["
                                 .. host .. "] port[" .. tostring(port) .. "] 
err: " .. err)
                 end
-                apisix_latency =  apisix_latency - entry.upstream_latency
-                if apisix_latency < 0 then
-                    apisix_latency = 0
-                end
             end
 
             -- apisix_latency
             local ok, err = sock:send(format("%s:%s|%s%s", prefix ..
-                                            "apisix.latency", apisix_latency, 
"h", suffix))
+                                    "apisix.latency", entry.apisix_latency, 
"h", suffix))
             if not ok then
                 core.log.error("failed to report apisix latency to dogstatsd 
server: host[" .. host
                         .. "] port[" .. tostring(port) .. "] err: " .. err)
diff --git a/apisix/plugins/prometheus/exporter.lua 
b/apisix/plugins/prometheus/exporter.lua
index b2c1ebc..d565b4c 100644
--- a/apisix/plugins/prometheus/exporter.lua
+++ b/apisix/plugins/prometheus/exporter.lua
@@ -33,6 +33,7 @@ local clear_tab = core.table.clear
 local get_stream_routes = router.stream_routes
 local get_protos = require("apisix.plugins.grpc-transcode.proto").protos
 local service_fetch = require("apisix.http.service").get
+local latency_details = require("apisix.utils.log-util").latency_details_in_ms
 
 
 
@@ -143,25 +144,15 @@ function _M.log(conf, ctx)
         gen_arr(vars.status, route_id, matched_uri, matched_host,
                 service_id, consumer_name, balancer_ip))
 
-    local latency = (ngx.now() - ngx.req.start_time()) * 1000
+    local latency, upstream_latency, apisix_latency = latency_details(ctx)
     metrics.latency:observe(latency,
         gen_arr("request", route_id, service_id, consumer_name, balancer_ip))
 
-    local apisix_latency = latency
-    if ctx.var.upstream_response_time then
-        local upstream_latency = ctx.var.upstream_response_time * 1000
+    if upstream_latency then
         metrics.latency:observe(upstream_latency,
             gen_arr("upstream", route_id, service_id, consumer_name, 
balancer_ip))
-        apisix_latency =  apisix_latency - upstream_latency
-
-        -- The latency might be negative, as Nginx uses different time 
measurements in
-        -- different metrics.
-        -- See 
https://github.com/apache/apisix/issues/5146#issuecomment-928919399
-        if apisix_latency < 0 then
-            apisix_latency = 0
-        end
-
     end
+
     metrics.latency:observe(apisix_latency,
         gen_arr("apisix", route_id, service_id, consumer_name, balancer_ip))
 
diff --git a/apisix/utils/log-util.lua b/apisix/utils/log-util.lua
index cf3fc22..10620d1 100644
--- a/apisix/utils/log-util.lua
+++ b/apisix/utils/log-util.lua
@@ -153,4 +153,23 @@ function _M.get_req_original(ctx, conf)
 end
 
 
+function _M.latency_details_in_ms(ctx)
+    local latency = (ngx.now() - ngx.req.start_time()) * 1000
+    local upstream_latency, apisix_latency = nil, latency
+
+    if ctx.var.upstream_response_time then
+        upstream_latency = ctx.var.upstream_response_time * 1000
+        apisix_latency = apisix_latency - upstream_latency
+
+        -- The latency might be negative, as Nginx uses different time 
measurements in
+        -- different metrics.
+        -- See 
https://github.com/apache/apisix/issues/5146#issuecomment-928919399
+        if apisix_latency < 0 then
+            apisix_latency = 0
+        end
+    end
+
+    return latency, upstream_latency, apisix_latency
+end
+
 return _M

Reply via email to