Hello!
On Tue, Jun 5, 2012 at 9:20 AM, Elias Torres <elias@hubspot.com> wrote:
> Let's speak nginx ;-)
>
> location / {
>
> proxy_pass http://backend;
>
> // after the request is processed
>
> lua_code '
> ngx.var.DICT.foo.incr(ngx.var.path .. resp.code)
> '
> }
>
> Does that make sense?
>
This is exactly what log_by_lua and log_by_lua_file directives are
for. These two directives run at the "log" phase in the nginx request
processing cycle. They're not implemented in any released versions of
ngx_lua yet but they're already in the "logby" branch in ngx_lua's git
repository:
https://github.com/chaoslawful/lua-nginx-module/tree/logby
Your example should be rewritten as something like this:
lua_shared_dict foo 100k;
server {
...
location /lua {
...
log_by_lua '
local foo = ngx.shared.foo
local key = ngx.var.uri .. ngx.status
local newval, err = foo:incr(key, 1)
if not newval then
if err == "not found" then
foo:add(key, 0)
newval, err = foo:incr(key, 1)
if not newval then
ngx.log(ngx.ERR, "failed to incr ", key, ": ", err)
return
end
else
ngx.log(ngx.ERR, "failed to incr ", key, ": ", err)
return
end
end
print(key, ": ", foo:get(key))
';
}
}
This example runs successfully when using ngx_lua's "logby" branch.
And there is also a (passing) test case for this:
https://github.com/chaoslawful/lua-nginx-module/blob/logby/t/075-logby.t#L108
The "logby" branch will be merged into "master" and finally be
included in an ngx_lua release in the very near future :)
BTW, I'm cc'ing the openresty mailing list
(https://groups.google.com/group/openresty/ ) so that other people can
also see our discussion here :)
Best regards,
-agentzh
> On Mon, Jun 4, 2012 at 9:00 PM, agentzh <age...@gmail.com> wrote:
>> Hello!
>>
>> On Tue, Jun 5, 2012 at 4:59 AM, Elias Torres <eto...@hubspot.com> wrote:
>>> Thank you for all your awesome work. I'm a big fan.
>>
>> Glad to hear that :)
>>
>>> I have a question:
>>>
>>> How do you recommend I 'log/track' response codes from upstream
>>> servers/backends and then reject new requests in the access phase?
>>>
>>
>> I don't quite follow your question. Could you elaborate?
>>
>> Regards,
>> -agentzh