Skip to content

Commit

Permalink
[Monitoring] Added missing cgroup memory (elastic#99602) (elastic#99956)
Browse files Browse the repository at this point in the history
* Added missing cgroup memory

* Fixed tests

Co-authored-by: Kibana Machine <[email protected]>
# Conflicts:
#	x-pack/plugins/monitoring/server/lib/apm/_apm_stats.js
#	x-pack/plugins/monitoring/server/lib/apm/get_apms.ts
#	x-pack/test/api_integration/apis/monitoring/apm/instances_mb.js
#	x-pack/test/api_integration/apis/monitoring/cluster/fixtures/multicluster.json
#	x-pack/test/api_integration/apis/monitoring/cluster/fixtures/overview.json
  • Loading branch information
igoristic authored May 12, 2021
1 parent 7672e52 commit ab19019
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 50 deletions.
13 changes: 13 additions & 0 deletions x-pack/plugins/monitoring/common/types/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,19 @@ export interface ElasticsearchLegacySource {
};
metrics?: {
beat?: {
cgroup?: {
memory: {
id: string;
mem: {
limit: {
bytes: number;
};
usage: {
bytes: number;
};
};
};
};
memstats?: {
memory_alloc?: number;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,22 @@ import { SetupModeBadge } from '../../setup_mode/badge';
import { isSetupModeFeatureEnabled } from '../../../lib/setup_mode';
import { SetupModeFeature } from '../../../../common/enums';

function getColumns(alerts, setupMode) {
function getColumns(alerts, setupMode, cgroup) {
const memoryField = cgroup
? {
name: i18n.translate('xpack.monitoring.apm.instances.cgroupMemoryUsageTitle', {
defaultMessage: 'Memory Usage (cgroup)',
}),
field: 'cgroup_memory',
render: (value) => formatMetric(value, 'byte'),
}
: {
name: i18n.translate('xpack.monitoring.apm.instances.allocatedMemoryTitle', {
defaultMessage: 'Allocated Memory',
}),
field: 'memory',
render: (value) => formatMetric(value, 'byte'),
};
return [
{
name: i18n.translate('xpack.monitoring.apm.instances.nameTitle', {
Expand Down Expand Up @@ -112,13 +127,7 @@ function getColumns(alerts, setupMode) {
},
}),
},
{
name: i18n.translate('xpack.monitoring.apm.instances.allocatedMemoryTitle', {
defaultMessage: 'Allocated Memory',
}),
field: 'memory',
render: (value) => formatMetric(value, 'byte'),
},
memoryField,
{
name: i18n.translate('xpack.monitoring.apm.instances.versionTitle', {
defaultMessage: 'Version',
Expand Down Expand Up @@ -166,7 +175,7 @@ export function ApmServerInstances({ apms, alerts, setupMode }) {
<EuiMonitoringTable
className="apmInstancesTable"
rows={data.apms}
columns={getColumns(alerts, setupMode)}
columns={getColumns(alerts, setupMode, data.cgroup)}
sorting={sorting}
pagination={pagination}
setupMode={setupMode}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export function ApmPanel(props) {
<EuiDescriptionListTitle className="eui-textBreakWord">
<FormattedMessage
id="xpack.monitoring.cluster.overview.apmPanel.memoryUsageLabel"
defaultMessage="Memory Usage"
defaultMessage="Memory Usage (delta)"
/>
</EuiDescriptionListTitle>
<EuiDescriptionListDescription data-test-subj="apmMemoryUsage">
Expand Down
47 changes: 19 additions & 28 deletions x-pack/plugins/monitoring/server/lib/apm/_apm_stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

import { get } from 'lodash';

const getMemPath = (cgroup) =>
cgroup
? 'beats_stats.metrics.beat.cgroup.memory.mem.usage.bytes'
: 'beats_stats.metrics.beat.memstats.rss';

export const getDiffCalculation = (max, min) => {
// no need to test max >= 0, but min <= 0 which is normal for a derivative after restart
// because we are aggregating/collapsing on ephemeral_ids
Expand All @@ -21,13 +26,11 @@ export const apmAggFilterPath = [
'aggregations.total',
'aggregations.min_events_total.value',
'aggregations.max_events_total.value',
'aggregations.min_mem_rss_total.value',
'aggregations.max_mem_rss_total.value',
'aggregations.max_mem_total_total.value',
'aggregations.min_mem_total.value',
'aggregations.max_mem_total.value',
'aggregations.versions.buckets',
];

export const apmUuidsAgg = (maxBucketSize) => ({
export const apmUuidsAgg = (maxBucketSize, cgroup) => ({
total: {
cardinality: {
field: 'beats_stats.beat.uuid',
Expand Down Expand Up @@ -55,19 +58,14 @@ export const apmUuidsAgg = (maxBucketSize) => ({
field: 'beats_stats.metrics.libbeat.pipeline.events.total',
},
},
min_mem_rss: {
min_mem: {
min: {
field: 'beats_stats.metrics.beat.memstats.rss',
},
},
max_mem_rss: {
max: {
field: 'beats_stats.metrics.beat.memstats.rss',
field: getMemPath(cgroup),
},
},
max_mem_total: {
max_mem: {
max: {
field: 'beats_stats.metrics.beat.memstats.memory_total',
field: getMemPath(cgroup),
},
},
},
Expand All @@ -82,19 +80,14 @@ export const apmUuidsAgg = (maxBucketSize) => ({
buckets_path: 'ephemeral_ids>max_events',
},
},
min_mem_rss_total: {
sum_bucket: {
buckets_path: 'ephemeral_ids>min_mem_rss',
},
},
max_mem_rss_total: {
min_mem_total: {
sum_bucket: {
buckets_path: 'ephemeral_ids>max_mem_rss',
buckets_path: 'ephemeral_ids>min_mem',
},
},
max_mem_total_total: {
max_mem_total: {
sum_bucket: {
buckets_path: 'ephemeral_ids>max_mem_total',
buckets_path: 'ephemeral_ids>max_mem',
},
},
});
Expand All @@ -104,16 +97,14 @@ export const apmAggResponseHandler = (response) => {

const eventsTotalMax = get(response, 'aggregations.max_events_total.value', null);
const eventsTotalMin = get(response, 'aggregations.min_events_total.value', null);
const memRssMax = get(response, 'aggregations.max_mem_rss_total.value', null);
const memRssMin = get(response, 'aggregations.min_mem_rss_total.value', null);
const memTotal = get(response, 'aggregations.max_mem_total_total.value', null);
const memMax = get(response, 'aggregations.max_mem_total.value', null);
const memMin = get(response, 'aggregations.min_mem_total.value', null);
const versions = get(response, 'aggregations.versions.buckets', []).map(({ key }) => key);

return {
apmTotal,
totalEvents: getDiffCalculation(eventsTotalMax, eventsTotalMin),
memRss: getDiffCalculation(memRssMax, memRssMin),
memTotal,
memRss: getDiffCalculation(memMax, memMin),
versions,
};
};
11 changes: 11 additions & 0 deletions x-pack/plugins/monitoring/server/lib/apm/get_apms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export function handleResponse(response: ElasticsearchResponse, start: number, e
bytes_sent_rate: bytesSentRate,
errors,
memory: stats.metrics?.beat?.memstats?.memory_alloc,
cgroup_memory: hit._source.beats_stats?.metrics?.beat?.cgroup?.memory.mem.usage.bytes,
version: stats.beat?.version,
time_of_last_event: hit._source.timestamp,
});
Expand Down Expand Up @@ -115,6 +116,16 @@ export async function getApms(req: LegacyRequest, apmIndexPattern: string, clust
'hits.hits._source.beats_stats.metrics.libbeat.output.read.errors',
'hits.hits._source.beats_stats.metrics.libbeat.output.write.errors',
'hits.hits._source.beats_stats.metrics.beat.memstats.memory_alloc',
'hits.hits._source.beats_stats.metrics.beat.cgroup.memory.mem.usage.bytes',
'hits.hits._source.beat.stats.beat.uuid',
'hits.hits._source.beat.stats.beat.name',
'hits.hits._source.beat.stats.beat.host',
'hits.hits._source.beat.stats.beat.type',
'hits.hits._source.beat.stats.beat.version',
'hits.hits._source.beat.stats.libbeat.output.type',
'hits.hits._source.beat.stats.libbeat.output.read.errors',
'hits.hits._source.beat.stats.libbeat.output.write.errors',
'hits.hits._source.beat.stats.memstats.memory.alloc',

// latest hits for calculating metrics
'hits.hits._source.beats_stats.timestamp',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ import { apmAggResponseHandler, apmUuidsAgg, apmAggFilterPath } from './_apm_sta
import { getTimeOfLastEvent } from './_get_time_of_last_event';

export function handleResponse(clusterUuid, response) {
const { apmTotal, totalEvents, memRss, memTotal, versions } = apmAggResponseHandler(response);
const { apmTotal, totalEvents, memRss, versions } = apmAggResponseHandler(response);

// combine stats
const stats = {
totalEvents,
memRss,
memTotal,
apms: {
total: apmTotal,
},
Expand All @@ -38,6 +37,7 @@ export function getApmsForClusters(req, apmIndexPattern, clusters) {
const end = req.payload.timeRange.max;
const config = req.server.config();
const maxBucketSize = config.get('monitoring.ui.max_bucket_size');
const cgroup = config.get('monitoring.ui.container.apm.enabled');

return Promise.all(
clusters.map(async (cluster) => {
Expand All @@ -54,7 +54,7 @@ export function getApmsForClusters(req, apmIndexPattern, clusters) {
clusterUuid,
metric: ApmMetric.getMetricFields(), // override default of BeatMetric.getMetricFields
}),
aggs: apmUuidsAgg(maxBucketSize),
aggs: apmUuidsAgg(maxBucketSize, cgroup),
},
};

Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/monitoring/server/lib/apm/get_stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export async function getStats(req, apmIndexPattern, clusterUuid) {
const start = moment.utc(req.payload.timeRange.min).valueOf();
const end = moment.utc(req.payload.timeRange.max).valueOf();
const maxBucketSize = config.get('monitoring.ui.max_bucket_size');
const cgroup = config.get('monitoring.ui.container.apm.enabled');

const params = {
index: apmIndexPattern,
Expand All @@ -42,7 +43,7 @@ export async function getStats(req, apmIndexPattern, clusterUuid) {
end,
clusterUuid,
}),
aggs: apmUuidsAgg(maxBucketSize),
aggs: apmUuidsAgg(maxBucketSize, cgroup),
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function apmInstancesRoute(server) {
return {
stats,
apms,
cgroup: req.server.config().get('monitoring.ui.container.apm.enabled'),
};
} catch (err) {
return handleError(err, req);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"stats": {
"totalEvents": 18,
"memRss": 3821568,
"memTotal": 2404475016,
"apms": {
"total": 2
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default function ({ getService }) {
time_of_last_event: '2018-08-31T13:59:21.165Z',
},
],
cgroup: false,
};

expect(body).to.eql(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
"apm": {
"totalEvents": null,
"memRss": null,
"memTotal": null,
"apms": {
"total": null
},
Expand Down Expand Up @@ -211,7 +210,6 @@
"apm": {
"totalEvents": null,
"memRss": null,
"memTotal": null,
"apms": {
"total": null
},
Expand Down Expand Up @@ -327,7 +325,6 @@
"apm": {
"totalEvents": null,
"memRss": null,
"memTotal": null,
"apms": {
"total": null
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@
"apm": {
"totalEvents": null,
"memRss": null,
"memTotal": null,
"apms": {
"total": null
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"apm": {
"totalEvents": 0,
"memRss": 0,
"memTotal": 0,
"apms": {
"total": 0
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
"apm": {
"totalEvents": 0,
"memRss": 0,
"memTotal": 0,
"apms": {
"total": 0
},
Expand Down Expand Up @@ -163,7 +162,6 @@
"apm": {
"totalEvents": 0,
"memRss": 0,
"memTotal": 0,
"apms": {
"total": 0
},
Expand Down

0 comments on commit ab19019

Please sign in to comment.