I want to have a graph where all recent IPs that requested my webserver get shown as total request count. Is something like this doable? Can I add a query and remove it afterwards via Prometheus?
CodePudding user response:
Technically, yes. You will need to:
- Expose some metric (probably a counter) in your server - say,
requests_count
, with a label; say,ip
- Whenever you receive a request,
inc
the metric with the label set to the requester IP - In Grafana, graph the metric, likely summing it by the IP address to handle the case where you have several horizontally scaled servers handling requests
sum(your_prometheus_namespace_requests_count) by (ip)
- Set the
Legend
of the graph in Grafana to{{ ip }}
to 'name' each line after the IP address it represents
However, every different label value a metric has causes a whole new metric to exist in the Prometheus time-series database; you can think of a metric like requests_count{ip="192.168.0.1"}=1
to be somewhat similar to requests_count_ip_192_168_0_1{}=1
in terms of how it consumes memory. Each metric instance currently being held in the Prometheus TSDB head takes something on the order of 3kB to exist. What that means is that if you're handling millions of requests, you're going to be swamping Prometheus' memory with gigabytes of data just from this one metric alone. A more detailed explanation about this issue exists in this other answer: https://stackoverflow.com/a/69167162/511258
With that in mind, this approach would make sense if you know for a fact you expect a small volume of IP addresses to connect (maybe on an internal intranet, or a client you distribute to a small number of known clients), but if you are planning to deploy to the web this would allow a very easy way for people to (unknowingly, most likely) crash your monitoring systems.
You may want to investigate an alternative -- for example, Grafana is capable of ingesting data from some common log aggregation platforms, so perhaps you can do some structured (e.g. JSON) logging, hold that in e.g. Elasticsearch, and then create a graph from the data held within that.