Skip to content

Commit 1018ac4

Browse files
[8.19] [Fleet] Fix memory and CPU usage query (elastic#223293) (elastic#224964)
# Backport This will backport the following commits from `main` to `8.19`: - [[Fleet] Fix memory and CPU usage query (elastic#223293)](elastic#223293) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Mason Herron","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-06-16T19:07:29Z","message":"[Fleet] Fix memory and CPU usage query (elastic#223293)\n\n## Summary\n\nCloses https://github.com/elastic/kibana/issues/223291\n\n- Changed the aggregation query to use `component.id` instead of\n`elastic_agent.process` in order to make the UI match the actual usage\noutputted by `systemctl` in multipass/linux. Previously, the Fleet UI\nshowed usage as 3-4 times lower than actual usage.\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [ ] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [ ] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n### Identify risks\n\nN/A","sha":"678446e33c22997039e784213442d1c161655319","branchLabelMapping":{"^v9.1.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","backport:prev-minor","backport:prev-major","v9.1.0"],"title":"[Fleet] Fix memory and CPU usage query","number":223293,"url":"https://github.com/elastic/kibana/pull/223293","mergeCommit":{"message":"[Fleet] Fix memory and CPU usage query (elastic#223293)\n\n## Summary\n\nCloses https://github.com/elastic/kibana/issues/223291\n\n- Changed the aggregation query to use `component.id` instead of\n`elastic_agent.process` in order to make the UI match the actual usage\noutputted by `systemctl` in multipass/linux. Previously, the Fleet UI\nshowed usage as 3-4 times lower than actual usage.\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [ ] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [ ] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n### Identify risks\n\nN/A","sha":"678446e33c22997039e784213442d1c161655319"}},"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/223293","number":223293,"mergeCommit":{"message":"[Fleet] Fix memory and CPU usage query (elastic#223293)\n\n## Summary\n\nCloses https://github.com/elastic/kibana/issues/223291\n\n- Changed the aggregation query to use `component.id` instead of\n`elastic_agent.process` in order to make the UI match the actual usage\noutputted by `systemctl` in multipass/linux. Previously, the Fleet UI\nshowed usage as 3-4 times lower than actual usage.\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [ ] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [ ] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n### Identify risks\n\nN/A","sha":"678446e33c22997039e784213442d1c161655319"}}]}] BACKPORT--> Co-authored-by: Mason Herron <[email protected]>
1 parent dfcbdcd commit 1018ac4

File tree

2 files changed

+50
-13
lines changed
  • x-pack

2 files changed

+50
-13
lines changed

x-pack/platform/plugins/shared/fleet/server/services/agents/agent_metrics.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ async function _fetchAndAssignAgentMetrics(esClient: ElasticsearchClient, agents
6464
},
6565
};
6666
});
67-
68-
return agents;
6967
}
7068

7169
const aggregationQueryBuilder = (agentIds: string[]) => ({
@@ -129,7 +127,7 @@ const aggregationQueryBuilder = (agentIds: string[]) => ({
129127
},
130128
processes: {
131129
terms: {
132-
field: 'elastic_agent.process',
130+
field: 'component.id',
133131
size: AGGREGATION_MAX_SIZE,
134132
order: {
135133
_count: 'desc',
@@ -171,9 +169,10 @@ const aggregationQueryBuilder = (agentIds: string[]) => ({
171169
},
172170
script: {
173171
source: `if (params.cpu_total > 0) {
174-
return params.cpu_total / params._interval
175-
}
176-
`,
172+
return params.cpu_total / params._interval;
173+
} else {
174+
return 0;
175+
}`,
177176
lang: 'painless',
178177
params: {
179178
_interval: 10000,

x-pack/test/fleet_api_integration/apis/agents/list.ts

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,15 @@ export default function ({ getService }: FtrProviderContext) {
124124

125125
it('should return metrics if available and called with withMetrics', async () => {
126126
const now = Date.now();
127+
// We need to create data points in precise time buckets to ensure the derivative works properly
128+
// 4 minutes ago (first data point for component1)
129+
const fourMinutesAgo = new Date(now - 4 * 60 * 1000);
130+
fourMinutesAgo.setSeconds(0, 0); // Set to exact minute boundary
127131
await es.index({
128132
index: 'metrics-elastic_agent.elastic_agent-default',
129133
refresh: 'wait_for',
130134
document: {
131-
'@timestamp': new Date(now - 2 * 60 * 1000).toISOString(),
135+
'@timestamp': fourMinutesAgo.toISOString(),
132136
data_stream: {
133137
namespace: 'default',
134138
type: 'metrics',
@@ -143,18 +147,52 @@ export default function ({ getService }: FtrProviderContext) {
143147
},
144148
cpu: {
145149
total: {
146-
value: 500,
150+
value: 500, // Starting value
147151
},
148152
},
149153
},
150154
},
151155
},
152156
});
157+
158+
// 3 minutes ago (second data point for component1)
159+
const threeMinutesAgo = new Date(now - 3 * 60 * 1000);
160+
threeMinutesAgo.setSeconds(0, 0); // Set to exact minute boundary
153161
await es.index({
154162
index: 'metrics-elastic_agent.elastic_agent-default',
155163
refresh: 'wait_for',
156164
document: {
157-
'@timestamp': new Date(now - 1 * 60 * 1000).toISOString(),
165+
'@timestamp': threeMinutesAgo.toISOString(),
166+
data_stream: {
167+
namespace: 'default',
168+
type: 'metrics',
169+
dataset: 'elastic_agent.elastic_agent',
170+
},
171+
elastic_agent: { id: 'agent1', process: 'elastic_agent' },
172+
component: { id: 'component1' },
173+
system: {
174+
process: {
175+
memory: {
176+
size: 25510920,
177+
},
178+
cpu: {
179+
total: {
180+
value: 1200, // Higher value to ensure derivative is positive
181+
},
182+
},
183+
},
184+
},
185+
},
186+
});
187+
188+
// 1 minute ago (data point for component2) - same agent but different component
189+
const oneMinuteAgo = new Date(now - 1 * 60 * 1000);
190+
oneMinuteAgo.setSeconds(0, 0); // Set to exact minute boundary
191+
await es.index({
192+
index: 'metrics-elastic_agent.elastic_agent-default',
193+
refresh: 'wait_for',
194+
document: {
195+
'@timestamp': oneMinuteAgo.toISOString(),
158196
elastic_agent: { id: 'agent1', process: 'elastic_agent' },
159197
component: { id: 'component2' },
160198
data_stream: {
@@ -169,7 +207,7 @@ export default function ({ getService }: FtrProviderContext) {
169207
},
170208
cpu: {
171209
total: {
172-
value: 1500,
210+
value: 2500, // Even higher value
173211
},
174212
},
175213
},
@@ -185,9 +223,9 @@ export default function ({ getService }: FtrProviderContext) {
185223
expect(apiResponse.total).to.eql(4);
186224

187225
const agent1: Agent = apiResponse.items.find((agent: any) => agent.id === 'agent1');
188-
189-
expect(agent1.metrics?.memory_size_byte_avg).to.eql('25510920');
190-
expect(agent1.metrics?.cpu_avg).to.eql('0.01666');
226+
// As both of the indexed items have the same agent id, and each one has its own memory/cpu item, the metrics should include both values combined as each is now uniquely counted towards total memory/cpu usage
227+
expect(agent1.metrics?.memory_size_byte_avg).to.eql('51021840');
228+
expect(agent1.metrics?.cpu_avg).to.eql('0.01166');
191229

192230
const agent2: Agent = apiResponse.items.find((agent: any) => agent.id === 'agent2');
193231
expect(agent2.metrics?.memory_size_byte_avg).equal(undefined);

0 commit comments

Comments
 (0)