[8.x] Register resident set size and limit bytes metrics in APM (#215458) (#215616)

# Backport

This will backport the following commits from `main` to `8.x`:
- [Register resident set size and limit bytes metrics in APM
(#215458)](https://github.com/elastic/kibana/pull/215458)

<!--- Backport version: 9.6.6 -->

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

<!--BACKPORT [{"author":{"name":"Jesus
Wahrman","email":"41008968+jesuswr@users.noreply.github.com"},"sourceCommit":{"committedDate":"2025-03-23T21:22:30Z","message":"Register
resident set size and limit bytes metrics in APM (#215458)\n\n##
Summary\n\nResolves
https://github.com/elastic/kibana-team/issues/1565\n\nRegister resident
set size and limit bytes metrics in APM.\n\n<img width=\"1614\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/d5f1caf5-da4f-4acd-a63f-2b5256a91307\"\n/>\n\n\n###
Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers
should verify this PR satisfies this list as well.\n\n- [x] [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- [x] 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)","sha":"04772fcc456aa6b03d9ecec540cc927c23c0dd9a","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Core","release_note:skip","backport:prev-major","backport:current-major","v9.1.0"],"title":"Register
resident set size and limit bytes metrics in
APM","number":215458,"url":"https://github.com/elastic/kibana/pull/215458","mergeCommit":{"message":"Register
resident set size and limit bytes metrics in APM (#215458)\n\n##
Summary\n\nResolves
https://github.com/elastic/kibana-team/issues/1565\n\nRegister resident
set size and limit bytes metrics in APM.\n\n<img width=\"1614\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/d5f1caf5-da4f-4acd-a63f-2b5256a91307\"\n/>\n\n\n###
Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers
should verify this PR satisfies this list as well.\n\n- [x] [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- [x] 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)","sha":"04772fcc456aa6b03d9ecec540cc927c23c0dd9a"}},"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/215458","number":215458,"mergeCommit":{"message":"Register
resident set size and limit bytes metrics in APM (#215458)\n\n##
Summary\n\nResolves
https://github.com/elastic/kibana-team/issues/1565\n\nRegister resident
set size and limit bytes metrics in APM.\n\n<img width=\"1614\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/d5f1caf5-da4f-4acd-a63f-2b5256a91307\"\n/>\n\n\n###
Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers
should verify this PR satisfies this list as well.\n\n- [x] [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- [x] 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)","sha":"04772fcc456aa6b03d9ecec540cc927c23c0dd9a"}}]}]
BACKPORT-->

Co-authored-by: Jesus Wahrman <41008968+jesuswr@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2025-03-24 00:16:05 +01:00 committed by GitHub
parent 4d53520eea
commit 95782f6d3a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 55 additions and 2 deletions

View file

@ -10,6 +10,7 @@
import v8, { HeapInfo } from 'v8';
import { mockEventLoopDelayMonitor, mockEventLoopUtilizationMonitor } from './process.test.mocks';
import { ProcessMetricsCollector } from './process';
import apm from 'elastic-apm-node';
describe('ProcessMetricsCollector', () => {
let collector: ProcessMetricsCollector;
@ -103,4 +104,24 @@ describe('ProcessMetricsCollector', () => {
expect(mockEventLoopUtilizationMonitor.reset).toBeCalledTimes(1);
});
});
describe('register metrics in apm', () => {
it('calls registerMetric in the constructor', () => {
const apmSpy = jest.spyOn(apm, 'registerMetric');
collector.registerMetrics();
expect(apmSpy).toHaveBeenCalledTimes(2);
expect(apmSpy).toHaveBeenNthCalledWith(
1,
'nodejs.memory.resident_set_size.bytes',
expect.any(Function)
);
expect(apmSpy).toHaveBeenNthCalledWith(
2,
'nodejs.heap.size_limit.bytes',
expect.any(Function)
);
});
});
});

View file

@ -9,6 +9,7 @@
import v8 from 'v8';
import type { OpsProcessMetrics, MetricsCollector } from '@kbn/core-metrics-server';
import apm from 'elastic-apm-node';
import { EventLoopDelaysMonitor } from './event_loop_delays_monitor';
import { EventLoopUtilizationMonitor } from './event_loop_utilization_monitor';
@ -54,6 +55,14 @@ export class ProcessMetricsCollector implements MetricsCollector<OpsProcessMetri
return [this.getCurrentPidMetrics()];
}
public registerMetrics() {
apm.registerMetric('nodejs.memory.resident_set_size.bytes', () => process.memoryUsage().rss);
apm.registerMetric(
'nodejs.heap.size_limit.bytes',
() => v8.getHeapStatistics().heap_size_limit
);
}
public reset() {
this.eventLoopDelayMonitor.reset();
this.eventLoopUtilizationMonitor.reset();

View file

@ -21,7 +21,13 @@ const createMock = () => {
return mocked;
};
const createMockProcessMetricsCollector = () => ({
...createMock(),
registerMetrics: jest.fn().mockResolvedValue({}),
});
export const collectorMock = {
create: createMock,
createOpsProcessMetrics: createMockOpsProcessMetrics,
createProcessMetricsCollector: createMockProcessMetricsCollector,
};

View file

@ -9,7 +9,10 @@
import { collectorMock } from '@kbn/core-metrics-collectors-server-mocks';
export const mockOpsCollector = collectorMock.create();
export const mockOpsCollector = {
...collectorMock.create(),
registerMetrics: jest.fn(),
};
jest.doMock('./ops_metrics_collector', () => ({
OpsMetricsCollector: jest.fn().mockImplementation(() => mockOpsCollector),

View file

@ -78,6 +78,8 @@ export class MetricsService
}
);
this.metricsCollector.registerMetrics();
await this.refreshMetrics();
this.collectInterval = setInterval(() => {

View file

@ -10,7 +10,7 @@
import { collectorMock } from '@kbn/core-metrics-collectors-server-mocks';
export const mockOsCollector = collectorMock.create();
export const mockProcessCollector = collectorMock.create();
export const mockProcessCollector = collectorMock.createProcessMetricsCollector();
export const mockServerCollector = collectorMock.create();
export const mockEsClientCollector = collectorMock.create();

View file

@ -80,4 +80,12 @@ describe('OpsMetricsCollector', () => {
expect(mockServerCollector.reset).toHaveBeenCalledTimes(2);
});
});
describe('#registerMetrics', () => {
it('call registerMetrics on the underlying collectors', () => {
collector.registerMetrics();
expect(mockProcessCollector.registerMetrics).toHaveBeenCalledTimes(1);
});
});
});

View file

@ -64,6 +64,10 @@ export class OpsMetricsCollector implements MetricsCollector<OpsMetrics> {
};
}
public registerMetrics() {
this.processCollector.registerMetrics();
}
public reset() {
this.processCollector.reset();
this.osCollector.reset();