k8sgpt-ai / k8sgpt

Giving Kubernetes Superpowers to everyone
http://k8sgpt.ai
Apache License 2.0
5.53k stars 639 forks source link

[Feature]: Improve the latency of analysis when there are some errors in cluster #1236

Open jxs1211 opened 3 weeks ago

jxs1211 commented 3 weeks ago

Checklist

Is this feature request related to a problem?

None

Problem Description

Recently I encountered some latency issues like the one below which caused a long wait for the response if there were some errors (around 30+ errors)in the cluster:

k8sgpt analyze -o json --kubecontext   1.10s user 0.10s system 3% cpu 30.487 total
jq .  0.04s user 0.01s system 0% cpu 30.495 total

Solution Description

I hope the latency will be limited to seconds under good network connectivity.

Benefits

It will improve the user experience, and make the analysis process more efficient.

Potential Drawbacks

No response

Additional Information

No response

matthisholleville commented 2 weeks ago

Hi @jxs1211

I can reproduce the issue. The problem seems to come from the Service analyzer.

(matthisholleville) ➜  k8sgpt git:(main) ✗ time ./bin/k8sgpt analyze
2024/08/26 13:32:47 Analyzer Ingress took 57.249625ms
2024/08/26 13:32:47 Analyzer PersistentVolumeClaim took 57.525541ms
2024/08/26 13:32:47 Analyzer CronJob took 64.764125ms
2024/08/26 13:32:47 Analyzer Node took 162.466084ms
2024/08/26 13:32:47 Analyzer Deployment took 168.8345ms
2024/08/26 13:32:47 Analyzer ReplicaSet took 281.76075ms
2024/08/26 13:32:47 Analyzer StatefulSet took 501.487167ms
2024/08/26 13:32:48 Analyzer Pod took 1.263196583s
2024/08/26 13:33:23 Analyzer Service took 36.287712708s

I'm looking into optimizing it and adding a flag to display stats for each analyzer.

matthisholleville commented 2 weeks ago

In my case, the Service analyzer needs to analyze 182 items. By adding more logs, I can see that each item takes approximately 1 second to be analyzed. Concurrency is managed at the analyzer level, but we might want to consider applying it within each analyzer as well. What do you think, @AlexsJones ?

Also, do you think it would be useful to have more detailed statistics, such as the number of items analyzed and the P90 of the execution time per item?

AlexsJones commented 2 weeks ago

In my case, the Service analyzer needs to analyze 182 items. By adding more logs, I can see that each item takes approximately 1 second to be analyzed. Concurrency is managed at the analyzer level, but we might want to consider applying it within each analyzer as well. What do you think, @AlexsJones ?

Also, do you think it would be useful to have more detailed statistics, such as the number of items analyzed and the P90 of the execution time per item?

I think we should have the ability to either be selective on logging or turn it off. If we split out analysers it will absolutely hammer the K8s API in/out of cluster

matthisholleville commented 2 weeks ago

In this PR I've proposed a new option for displaying stats only.

It seems to me that it might be interesting to have information such as the number of items analyzed, for example.