Baoyuantop commented on code in PR #12696:
URL: https://github.com/apache/apisix/pull/12696#discussion_r2548441991


##########
test-nginx:
##########


Review Comment:
   Is this change necessary?



##########
apisix/utils/log-util.lua:
##########
@@ -82,23 +82,74 @@ local function gen_log_format(format)
     return log_format
 end
 
+local log_vars = {
+    latency = function() return (ngx_now() - ngx.req.start_time()) * 1000 end,
+    upstream_latency = function(ctx)
+        return ctx.var.upstream_response_time and 
ctx.var.upstream_response_time * 1000 or nil
+    end,
+    apisix_latency = function(ctx)
+        local latency = (ngx_now() - ngx.req.start_time()) * 1000
+        local upstream_latency =
+            ctx.var.upstream_response_time and ctx.var.upstream_response_time 
* 1000 or 0
+        local apisix_latency = 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
+        return apisix_latency
+    end,
+    client_ip = function(ctx) return core.request.get_remote_client_ip(ctx) 
end,
+    start_time = function() return ngx.req.start_time() * 1000 end,
+    hostname = function() return core.utils.gethostname() end,
+    version = function() return core.version.VERSION end,
+    request_method = function() return ngx.req.get_method() end,
+    request_headers = function() return ngx.req.get_headers() end,
+    request_querystring = function() return ngx.req.get_uri_args() end,
+    request_body = function(ctx, max_req_body_bytes)
+        local max_bytes = max_req_body_bytes or MAX_REQ_BODY
+        local req_body, err = get_request_body(max_bytes)
+        if err then
+            core.log.error("fail to get request body: ", err)
+            return nil
+        end
+        return req_body
+    end,
+    response_status = function() return ngx.status end,
+    response_headers = function() return ngx.resp.get_headers() end,
+    response_size = function(ctx) return ctx.var.bytes_sent end,
+    response_body = function (ctx) return ctx.resp_body end,
+    upstream = function(ctx) return ctx.var.upstream_addr end,
+    url = function(ctx)
+        local var = ctx.var
+        return string.format("%s://%s:%s%s",
+            var.scheme,
+            var.host,
+            var.server_port,
+            var.request_uri)
+    end,
+    consumer_username = function(ctx)
+        return ctx.consumer and ctx.consumer.username or nil
+    end,
+    service_id = function(ctx)

Review Comment:
   In the original get_full_log logic, if no route is matched (matched_route is 
empty), service_id will fall back to var.host.



##########
apisix/utils/log-util.lua:
##########
@@ -82,23 +82,74 @@ local function gen_log_format(format)
     return log_format
 end
 
+local log_vars = {
+    latency = function() return (ngx_now() - ngx.req.start_time()) * 1000 end,

Review Comment:
   The calculation logic for latency, upstream_latency, and apisix_latency has 
been reimplemented in log_vars. This is completely duplicated from the existing 
latency_details_in_ms function logic.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to