knyar / nginx-lua-prometheus

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

Feature request: Handling user-agents #163

Closed 2ge closed 1 year ago

2ge commented 1 year ago

Thanks for nice exporter. It would be possible to add exporting HTTP User-Agent ?

In grafana I would like to have pie chart with User-Agents with versions and without versions - I got nice regexp for parsing :)

/(.*?)[\s_\/]+[v]?(\d+(?:[\.\d\-]|deve?l?|alpha|beta|pre|rc|ga|rtm)*)/i

Thanks for consideration

dolik-rce commented 1 year ago

Sure you can do anything, as long as you can code it in Lua. That is not something this library should do, as you can already do it in your configuration. You'll need something like

~~ initialization
requests = prometheus:counter("requests_total", "Request counts", {"endpoint", "ua_name", "ua_version"})

~~ logging
local user_agent = ngx.re.match(ngx.var.http_user_agent, "(.*?)[\s_\/]+[v]?(\d+(?:[\.\d\-]|deve?l?|alpha|beta|pre|rc|ga|rtm)*)", "i")
requests:inc(1, {ngx.var.uri, user_agent[1], user_agent[2]})

DISCLAIMER: I did not test the code, netiher your regex. Just put it together from some examples and documentation, so it might contain syntactical or other errors. But the general idea should be clear, I hope.

2ge commented 1 year ago

thank you a lot! this is very helpful.

knyar commented 1 year ago

Thanks @dolik-rce for providing a response here.