[8.17] [APM] Improve performance in `dependencies` endpoints to prevent high CPU usage (#209999) (#210317)

# Backport

This will backport the following commits from `main` to `8.17`:
- [[APM] Improve performance in `dependencies` endpoints to
prevent high CPU usage
(#209999)](https://github.com/elastic/kibana/pull/209999)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Milosz
Marcinkowski","email":"38698566+miloszmarcinkowski@users.noreply.github.com"},"sourceCommit":{"committedDate":"2025-02-10T09:52:00Z","message":"[APM]
Improve performance in `dependencies` endpoints to prevent high CPU
usage (#209999)\n\n## Summary\r\n\r\nAfter investigating a high CPU
spike in the `dependencies` endpoints for\r\nlarge quantities of data,
we identified the issue as a slow deep merge\r\nof objects. The merge
operation was growing exponentially in size based\r\non the number of
objects found with the same `id`.\r\n\r\nWe improved performance by
excluding the stats object from unnecessary\r\nmerges and instead
joining it using the spread operator. The results\r\nshow a significant
reduction in CPU usage, preventing the event loop\r\nfrom being blocked
and ensuring better resource
availability.\r\n\r\n|Before|After|\r\n|-|-|\r\n|<img width=\"1728\"
alt=\"Screenshot 2025-02-06 at 12 50
41\"\r\nsrc=\"https://github.com/user-attachments/assets/6cd2bd68-6f19-42e2-bc2a-ed01a9f187c0\"\r\n/>|<img
width=\"1728\" alt=\"Screenshot 2025-02-06 at 13 11
20\"\r\nsrc=\"https://github.com/user-attachments/assets/3ab7d9fb-0156-4d7e-a788-95cae11131f6\"\r\n/>|\r\n|<img
width=\"2559\" alt=\"Screenshot 2025-02-06 at 14 34
24\"\r\nsrc=\"https://github.com/user-attachments/assets/3950525c-f5fa-4895-ae38-7927e5497d80\"\r\n/>|<img
width=\"2558\" alt=\"Screenshot 2025-02-06 at 14 29
08\"\r\nsrc=\"https://github.com/user-attachments/assets/f05b6747-077b-49ea-a4ac-8528f6e2eb8f\"\r\n/>|","sha":"0c7fb00dee6ba4cf84e8a4d2d15ab5ab70e404cb","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:enhancement","apm","backport:prev-minor","backport:prev-major","ci:project-deploy-observability","Team:obs-ux-infra_services","v9.1.0"],"title":"[APM]
Improve performance in `dependencies` endpoints to prevent high CPU
usage","number":209999,"url":"https://github.com/elastic/kibana/pull/209999","mergeCommit":{"message":"[APM]
Improve performance in `dependencies` endpoints to prevent high CPU
usage (#209999)\n\n## Summary\r\n\r\nAfter investigating a high CPU
spike in the `dependencies` endpoints for\r\nlarge quantities of data,
we identified the issue as a slow deep merge\r\nof objects. The merge
operation was growing exponentially in size based\r\non the number of
objects found with the same `id`.\r\n\r\nWe improved performance by
excluding the stats object from unnecessary\r\nmerges and instead
joining it using the spread operator. The results\r\nshow a significant
reduction in CPU usage, preventing the event loop\r\nfrom being blocked
and ensuring better resource
availability.\r\n\r\n|Before|After|\r\n|-|-|\r\n|<img width=\"1728\"
alt=\"Screenshot 2025-02-06 at 12 50
41\"\r\nsrc=\"https://github.com/user-attachments/assets/6cd2bd68-6f19-42e2-bc2a-ed01a9f187c0\"\r\n/>|<img
width=\"1728\" alt=\"Screenshot 2025-02-06 at 13 11
20\"\r\nsrc=\"https://github.com/user-attachments/assets/3ab7d9fb-0156-4d7e-a788-95cae11131f6\"\r\n/>|\r\n|<img
width=\"2559\" alt=\"Screenshot 2025-02-06 at 14 34
24\"\r\nsrc=\"https://github.com/user-attachments/assets/3950525c-f5fa-4895-ae38-7927e5497d80\"\r\n/>|<img
width=\"2558\" alt=\"Screenshot 2025-02-06 at 14 29
08\"\r\nsrc=\"https://github.com/user-attachments/assets/f05b6747-077b-49ea-a4ac-8528f6e2eb8f\"\r\n/>|","sha":"0c7fb00dee6ba4cf84e8a4d2d15ab5ab70e404cb"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/209999","number":209999,"mergeCommit":{"message":"[APM]
Improve performance in `dependencies` endpoints to prevent high CPU
usage (#209999)\n\n## Summary\r\n\r\nAfter investigating a high CPU
spike in the `dependencies` endpoints for\r\nlarge quantities of data,
we identified the issue as a slow deep merge\r\nof objects. The merge
operation was growing exponentially in size based\r\non the number of
objects found with the same `id`.\r\n\r\nWe improved performance by
excluding the stats object from unnecessary\r\nmerges and instead
joining it using the spread operator. The results\r\nshow a significant
reduction in CPU usage, preventing the event loop\r\nfrom being blocked
and ensuring better resource
availability.\r\n\r\n|Before|After|\r\n|-|-|\r\n|<img width=\"1728\"
alt=\"Screenshot 2025-02-06 at 12 50
41\"\r\nsrc=\"https://github.com/user-attachments/assets/6cd2bd68-6f19-42e2-bc2a-ed01a9f187c0\"\r\n/>|<img
width=\"1728\" alt=\"Screenshot 2025-02-06 at 13 11
20\"\r\nsrc=\"https://github.com/user-attachments/assets/3ab7d9fb-0156-4d7e-a788-95cae11131f6\"\r\n/>|\r\n|<img
width=\"2559\" alt=\"Screenshot 2025-02-06 at 14 34
24\"\r\nsrc=\"https://github.com/user-attachments/assets/3950525c-f5fa-4895-ae38-7927e5497d80\"\r\n/>|<img
width=\"2558\" alt=\"Screenshot 2025-02-06 at 14 29
08\"\r\nsrc=\"https://github.com/user-attachments/assets/f05b6747-077b-49ea-a4ac-8528f6e2eb8f\"\r\n/>|","sha":"0c7fb00dee6ba4cf84e8a4d2d15ab5ab70e404cb"}}]}]
BACKPORT-->

Co-authored-by: Milosz Marcinkowski <38698566+miloszmarcinkowski@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2025-02-10 22:39:29 +11:00 committed by GitHub
parent 04d922e15a
commit e4c6713f80
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -72,7 +72,7 @@ export function getConnectionStats({
const { stats: statsA, ...itemA } = a;
const { stats: statsB, ...itemB } = b;
return merge({}, itemA, itemB, { stats: statsA.concat(statsB) });
return { ...merge({}, itemA, itemB), stats: statsA.concat(statsB) };
});
const statsItems = statsJoinedById.map((item) => {