Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Generates lot of useless logs when cookie not found #4516

Closed
slene opened this issue Jul 1, 2021 · 6 comments · Fixed by #4564
Closed

bug: Generates lot of useless logs when cookie not found #4516

slene opened this issue Jul 1, 2021 · 6 comments · Fixed by #4564
Labels
good first issue Good for newcomers

Comments

@slene
Copy link
Contributor

slene commented Jul 1, 2021

Issue description

Environment

  • apisix version (cmd: apisix version): 2.6
  • OS (cmd: uname -a): centos 7.8

Minimal test code / Steps to reproduce the issue

{
  "uris": [
    "/*"
  ],
  "name": "route",
  "methods": [
    "GET"
  ],
  "vars": [
    [
      "cookie_upstream",
      "==",
      "dev"
    ]
  ],
  "status": 1
}

When cookie cookie_upstream not found.

2021/07/01 19:30:06 [warn] 46#46: *2782 [lua] ctx.lua:157: __index(): failed to fetch cookie value by key: cookie_upstream error: nil, client: 127.0.0.1, server: _, request: "GET / HTTP/1.1", host: "localhost:9181"

When cookie header not exists.

2021/07/01 19:38:50 [warn] 46#46: *19719 [lua] ctx.lua:157: __index(): failed to fetch cookie value by key: cookie_upstream error: no cookie found in the current request, client: 127.0.0.1, server: _, request: "GET / HTTP/1.1", host: "localhost:9181"

It generates a lot of useless logs. If this is unexpected, I can submit a pr.

             elseif core_str.has_prefix(key, "cookie_") then
                 local cookie = t.cookie
-                if cookie then
+                if cookie and ngx.var.http_cookie then
                     local err
                     val, err = cookie:get(sub_str(key, 8))
-                    if not val then
+                    if err then
                         log.warn("failed to fetch cookie value by key: ",
                                  key, " error: ", err)
                     end
@spacewander
Copy link
Member

We can do the check inside

cookie = function () return ck:new() end

@slene
Copy link
Contributor Author

slene commented Jul 1, 2021

@spacewander How about this ?

@@ -111,7 +111,11 @@ end
 do
     local var_methods = {
         method = ngx.req.get_method,
-        cookie = function () return ck:new() end
+        cookie = function ()
+            if ngx.var.http_cookie then
+                return ck:new()
+            end
+        end
     }

     local ngx_var_names = {
@@ -153,7 +157,7 @@ do
                 if cookie then
                     local err
                     val, err = cookie:get(sub_str(key, 8))
-                    if not val then
+                    if err then
                         log.warn("failed to fetch cookie value by key: ",
                                  key, " error: ", err)
                     end

@spacewander
Copy link
Member

Look like we don't need to change this part: "if not val then"?

@slene
Copy link
Contributor Author

slene commented Jul 2, 2021

@spacewander

I think that value does not exist is not an error. The log should be recorded when err exists.

local value = ctx.var["cookie_upstream"]
if value then
    -- do something
end
2021/07/01 19:30:06 [warn] 46#46: *2782 [lua] ctx.lua:157: __index(): failed to fetch cookie value by key: cookie_upstream error: nil, client: 127.0.0.1, server: _, request: "GET / HTTP/1.1", host: "localhost:9181"

@spacewander
Copy link
Member

Yes, you are right.

@spacewander spacewander added the good first issue Good for newcomers label Jul 5, 2021
@spacewander
Copy link
Member

@slene
PR is welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants