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

Add variable value instead of prometheus metrics in init_worker_by_lua_block #146

Closed
SanaZulfiqar73 opened this issue Jul 27, 2022 · 1 comment

Comments

@SanaZulfiqar73
Copy link

Hey,

I have this scenario where I am calculating the latency value by subtracting two value and want to expose it as prometheus metrics. Is there any way I can do expose this value?
Probably something like this:

http {
    map $host $request_time_ms {
            default '';
    }

    log_by_lua '
          ngx.var.request_time_ms = $request_time - $upstream_custom_response_time
    ';

    lua_shared_dict prometheus_metrics 10M;
    lua_package_path "/path/to/nginx-lua-prometheus/?.lua;;";

    init_worker_by_lua_block {
        prometheus = require("prometheus").init("prometheus_metrics")

        metric_latency = prometheus:histogram(ngx.var.request_time_ms, "HTTP request latency", {"uri", "status"})
      }
}
@knyar
Copy link
Owner

knyar commented Aug 29, 2022

Something like this should work:

log_by_lua '
  ngx.var.request_time_ms = ngx.var.request_time - ngx.var.upstream_custom_response_time
  metric_latency:observe(ngx.var.request_time_ms, {ngx.var.server_name, ngx.var.status})
';

@knyar knyar closed this as completed Aug 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants