knyar / nginx-lua-prometheus

Prometheus metric library for Nginx written in Lua
MIT License
1.46k stars 231 forks source link

Adding log_by_lua_block inside anther lua file #122

Closed frodood closed 3 years ago

frodood commented 3 years ago

hey guys,

I have a special scenario when logging nginx metrics, I don't want to log every incoming request hitting my ngx, certain conditions has to be meant before system passed the ngx request to services, so I want to log metrics only for that. I have this config

    location = /example.php {
            set $example "0";
            set $example_timeout 15000;
            set $example_read_timeout 8;
            content_by_lua_file /etc/nginx/lua/example.lua;

    }

and in my example.lua I added following

        metric_requests:inc(1, {ngx.var.server_name, ngx.var.status})
        metric_latency:observe(tonumber(ngx.var.request_time),
                               {ngx.var.server_name})
        metric_bytes:inc(tonumber(ngx.var.request_length))
        metric_response_sizes:observe(tonumber(ngx.var.bytes_sent))

but metrics are not logging, how can I solve this?

dolik-rce commented 3 years ago

This is definitely possible and should work. I believe that the proper directive to use should be log_by_lua_file, that might be the reason, why it doesn't work for you...

Just to make sure: Do you also have initialization and collection code somewhere? Do you see any errors in nginx error.log?