apisix icon indicating copy to clipboard operation
apisix copied to clipboard

request help: prometheus plugin loses data when APISIX restarts

Open Fuchange opened this issue 5 years ago • 8 comments

Issue description

I use the prometheus plugin to collect statistics of apifix . But when I restart APISX, apisix_http_status is cleared.

For example, I:

  1. Use routing A 2 times
  2. Restart APISIX
  3. Use routing A 5 times

I hope to see apisix_http_status{route_name="A"} 7 in prometheus, but it will be 5.

Can anyone help ?

Environment

Request help without environment information will be ignored or closed.

  • apisix version (cmd: apisix version): 2.4
  • OpenResty / Nginx version (cmd: nginx -V or openresty -V): openresty/1.19.3.1
  • etcd version, if have (cmd: run curl http://127.0.0.1:9090/v1/server_info to get the info from server-info API): 3.4.0

Fuchange avatar Aug 12 '21 07:08 Fuchange

@liuxiran @Xu-Mj : - )

Fuchange avatar Aug 12 '21 07:08 Fuchange

@Fuchange APISIX puts all the metrics data in shared memory, while restart operation will fork another new master and the new master, in turn, forking a bunch of workers, they use a new shared memory block, so metrics data are not inherited from the old shared memory.

tokers avatar Aug 12 '21 11:08 tokers

@Fuchange APISIX puts all the metrics data in shared memory, while restart operation will fork another new master and the new master, in turn, forking a bunch of workers, they use a new shared memory block, so metrics data are not inherited from the old shared memory.

Thanks for your answer. I understand why it happened.

Do we have a recommended solution ? A counter that always reset do not make much sense for information statistics .

Fuchange avatar Aug 13 '21 08:08 Fuchange

You don't need to "solve" it. It should be handled by Prometheus. Please read https://www.robustperception.io/how-does-a-prometheus-counter-work, which starts from "A common question is what happens when the process restarts and the counter is reset to 0".

spacewander avatar Aug 16 '21 10:08 spacewander

You don't need to "solve" it. It should be handled by Prometheus. Please read https://www.robustperception.io/how-does-a-prometheus-counter-work, which starts from "A common question is what happens when the process restarts and the counter is reset to 0".

@spacewander Thanks for your answer.

They said "What you really want to know is how fast it's counting" . But what we need is not how fast but how much .

Using rate() to get how much is difficult to get an integer (because " rate() also has logic to handle samples not quite covering the entire time range you pass to rate() ")

By the way , they said "This makes it important that it not be possible for Counters to be decremented , a Counter that has the potential to be decremented is in reality a Gauge"

Fuchange avatar Aug 17 '21 09:08 Fuchange

Using rate() to get how much is difficult to get an integer (because " rate() also has logic to handle samples not quite covering the entire time range you pass to rate() ")

It's recommended to use rate() which solves some edge cases. Calculate the time range manually may be wrong in some situations, for example, your sample time and query time may be mismatched.

spacewander avatar Aug 17 '21 13:08 spacewander

It's recommended to use rate() which solves some edge cases. Calculate the time range manually may be wrong in some situations, for example, your sample time and query time may be mismatched.

@spacewander So is "apisix_http_status" a counter that can only provide "how fast" but not "how much" because of it possible reduced ?

Fuchange avatar Aug 18 '21 03:08 Fuchange

The rate() should handle it. And this is a Prometheus issue, which is better to be discussed in the Prometheus community.

spacewander avatar Aug 18 '21 03:08 spacewander