Timestamp of data that collected from telegraf is disordered.
Relevent telegraf.conf
[global_tags]
vm_os_type = "linux"
vm_uuid = "5b616bfd-dbc4-4d9b-a015-610870b8bb14"
[agent]
interval = "60s"
round_interval = true
metric_batch_size = 1000
metric_buffer_limit = 10000
collection_jitter = "5s"
flush_interval = "60s"
flush_jitter = "10s"
debug = false
quiet = false
hostname = "5b616bfd-dbc4-4d9b-a015-610870b8bb14"
omit_hostname = false
[[inputs.procstat]]
systemd_unit = "redis-server.service"
name_prefix = "vm_linux_"
[[outputs.file]]
files = ["/etc/telegraf/test.txt"]
System info
Telegraf 1.14.1, CentOS Linux release 7.9.2009 (Core)
Docker
No response
Steps to reproduce
1.Set up the telegraf.conf 2.check the timestamp of data in output file "/etc/telegraf/test.txt"
...
Expected behavior
cat /etc/telegraf/test.txt
vm_linux_procstat_lookup,host=5b616bfd-dbc4-4d9b-a015-610870b8bb14,pid_finder=pgrep,result=success,systemd_unit=redis-server.service,vm_os_type=linux,vm_uuid=5b616bfd-dbc4-4d9b-a015-610870b8bb14 pid_count=1i,running=1i,result_code=0i 1638958861000000000
vm_linux_procstat_lookup,host=5b616bfd-dbc4-4d9b-a015-610870b8bb14,pid_finder=pgrep,result=success,systemd_unit=redis-server.service,vm_os_type=linux,vm_uuid=5b616bfd-dbc4-4d9b-a015-610870b8bb14 result_code=0i,pid_count=1i,running=1i 1638958921000000000
vm_linux_procstat_lookup,host=5b616bfd-dbc4-4d9b-a015-610870b8bb14,pid_finder=pgrep,result=success,systemd_unit=redis-server.service,vm_os_type=linux,vm_uuid=5b616bfd-dbc4-4d9b-a015-610870b8bb14 pid_count=1i,running=1i,result_code=0i 1638958982000000000
Actual behavior
If we check the output file we can see that the out put is disordered.
cat /etc/telegraf/test.txt
vm_linux_procstat_lookup,host=5b616bfd-dbc4-4d9b-a015-610870b8bb14,pid_finder=pgrep,result=success,systemd_unit=redis-server.service,vm_os_type=linux,vm_uuid=5b616bfd-dbc4-4d9b-a015-610870b8bb14 result_code=0i,pid_count=1i,running=1i 1638958921000000000
vm_linux_procstat_lookup,host=5b616bfd-dbc4-4d9b-a015-610870b8bb14,pid_finder=pgrep,result=success,systemd_unit=redis-server.service,vm_os_type=linux,vm_uuid=5b616bfd-dbc4-4d9b-a015-610870b8bb14 pid_count=1i,running=1i,result_code=0i 1638958861000000000
vm_linux_procstat_lookup,host=5b616bfd-dbc4-4d9b-a015-610870b8bb14,pid_finder=pgrep,result=success,systemd_unit=redis-server.service,vm_os_type=linux,vm_uuid=5b616bfd-dbc4-4d9b-a015-610870b8bb14 pid_count=1i,running=1i,result_code=0i 1638958982000000000
Additional info
As we can see, the timestamps that attached to the first and second data is disordered. This issue happens sometimes but not frequently . , but it still impact my proceeding alarm service logic.
Moreover, this is not only about procstat plugin. This issue actually happens in all types of plugins, including cpu, mem, and others.( I tested before)
Can someone help please? Feel free to ask more information of this issue if needed.
@zzcpower there is no guarantee whatsoever on the order of the timestamps IIRC but they are forwarded as they arrive. Is there a specific reason to order the metrics by time?
@zzcpower there is no guarantee whatsoever on the order of the timestamps IIRC but they are forwarded as they arrive. Is there a specific reason to order the metrics by time?
Yes, especially in real-time data processing situation. Let's say if we want to build an alarm service of redis by using the data from procstat_lookup. As we know when running=1, we say the process is running, and when running=0, we say the process is down then the alarm will be triggered. If the error(running=0) and disorder happen at the same time, we will get a series of streaming data like running=1, running=0, running=1, running=0, running=0,running=0......(The bold text is where disorder happens). Therefore, the alarm will be triggered when running =0 ,and immediately recovered, then triggered again. Did I make sense?
@zzcpower that makes sense and I do understand your point. However, telegraf itself cannot "fix" this issue, here is why:
In telegraf, the input plugins will query data and, (leaving out intermediate processing for simplicity), telegraf collects those metrics as batches (you can control it with buffer_size etc). Then, after flush_interval passed this batch will be written to the output(s). Now many input plugins get the metric timestamps from their endpoints (e.g. a webserver). So if now a batch is sent and now one of those endpoints sends a metric with a timestamp in the past you get your disorder... Telegraf cannot order the metrics as the last batch is out already.
Anyway, you have now two possibilities IMO.
- Add an
orderflag to the output you need and then sort the metrics by time before pushing them out. This will fix the order in a batch, but cannot deal with cross-batch disorder like described above. - Order the series in your receiver. You have the time, so you can order the received data. I.e. drop everything older than the latest timestamp you already know, sort the rest by time. That's also why I push my metrics into a time-series database first and then do a query. But this comes at the cost of added latency...
Does that make sense to you?
@srebhan Thanks for you reply, this makes sense. What if I set flush interval to 50s which is smaller than interval(60s)? Will this fix the issue?
No I think you cannot fix this in general. A global order can never be guaranteed by Telegraf, so I recommend to handle the issue on the receiver side (see my second option above).
I'm going to go ahead and close this for the reasons srebhan laid out.
I think I just found the root of the issue. The disorder is all about how to adjust hyper parameters in agent configuration. In my scenario, interval=60s, collection_jitter = 5s, while flush_interval=60s and flush_jitter = 10s, so collection can happen at any time between 60s-65s, and flush can happen at anytime between 60s-70s. Lets say data is collected at 65s, but agent flush the data at 64s, so the data is still in the buffer and has not been flushed because the flush happened before the collection. Then lets say next collection happens at 121s, and flush happens 122s, in this case, the previous data and current data will be flushed together to the output while telegraf does not guarantee the oder in the buffer to the output. Therefore we will see the disorder in our output.
This issue can be simply fixed by adjust the flush_interval + flush_jitter < interval to make sure flush will always happen before the collection.