Skip to content

Commit

Permalink
[APM] co-locate system and cgroup memory filter and script (#156654)
Browse files Browse the repository at this point in the history
  • Loading branch information
kpatticha authored May 4, 2023
1 parent 5a65112 commit bc886e1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ const chartBase: ChartBase = {
series,
};

export const systemMemoryFilter = {
bool: {
filter: [
{ exists: { field: METRIC_SYSTEM_FREE_MEMORY } },
{ exists: { field: METRIC_SYSTEM_TOTAL_MEMORY } },
],
export const systemMemory = {
filter: {
bool: {
filter: [
{ exists: { field: METRIC_SYSTEM_FREE_MEMORY } },
{ exists: { field: METRIC_SYSTEM_TOTAL_MEMORY } },
],
},
},
};

export const percentSystemMemoryUsedScript = {
lang: 'painless',
source: `
script: {
lang: 'painless',
source: `
if(doc.containsKey('${METRIC_SYSTEM_FREE_MEMORY}') && doc.containsKey('${METRIC_SYSTEM_TOTAL_MEMORY}')){
double freeMemoryValue = doc['${METRIC_SYSTEM_FREE_MEMORY}'].value;
double totalMemoryValue = doc['${METRIC_SYSTEM_TOTAL_MEMORY}'].value;
Expand All @@ -67,22 +67,23 @@ export const percentSystemMemoryUsedScript = {
return null;
`,
} as const;

export const cgroupMemoryFilter = {
bool: {
filter: [{ exists: { field: METRIC_CGROUP_MEMORY_USAGE_BYTES } }],
should: [
{ exists: { field: METRIC_CGROUP_MEMORY_LIMIT_BYTES } },
{ exists: { field: METRIC_SYSTEM_TOTAL_MEMORY } },
],
minimum_should_match: 1,
},
};

export const percentCgroupMemoryUsedScript = {
lang: 'painless',
source: `
export const cgroupMemory = {
filter: {
bool: {
filter: [{ exists: { field: METRIC_CGROUP_MEMORY_USAGE_BYTES } }],
should: [
{ exists: { field: METRIC_CGROUP_MEMORY_LIMIT_BYTES } },
{ exists: { field: METRIC_SYSTEM_TOTAL_MEMORY } },
],
minimum_should_match: 1,
},
},
script: {
lang: 'painless',
source: `
/*
When no limit is specified in the container, docker allows the app as much memory / swap memory as it wants.
This number represents the max possible value for the limit field.
Expand All @@ -100,7 +101,8 @@ export const percentCgroupMemoryUsedScript = {
return used / total;
`,
} as const;
},
};

export async function getMemoryChartData({
environment,
Expand Down Expand Up @@ -163,11 +165,11 @@ export async function getMemoryChartData({
end,
chartBase,
aggs: {
memoryUsedAvg: { avg: { script: percentCgroupMemoryUsedScript } },
memoryUsedMax: { max: { script: percentCgroupMemoryUsedScript } },
memoryUsedAvg: { avg: { script: cgroupMemory.script } },
memoryUsedMax: { max: { script: cgroupMemory.script } },
},
additionalFilters: [
cgroupMemoryFilter,
cgroupMemory.filter,
...termQuery(FAAS_ID, serverlessId),
],
operationName: 'get_cgroup_memory_metrics_charts',
Expand All @@ -185,11 +187,11 @@ export async function getMemoryChartData({
end,
chartBase,
aggs: {
memoryUsedAvg: { avg: { script: percentSystemMemoryUsedScript } },
memoryUsedMax: { max: { script: percentSystemMemoryUsedScript } },
memoryUsedAvg: { avg: { script: systemMemory.script } },
memoryUsedMax: { max: { script: systemMemory.script } },
},
additionalFilters: [
systemMemoryFilter,
systemMemory.filter,
...termQuery(FAAS_ID, serverlessId),
],
operationName: 'get_system_memory_metrics_charts',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ import {
} from '../../lib/helpers/transactions';
import { getFailedTransactionRate } from '../../lib/transaction_groups/get_failed_transaction_rate';
import { withApmSpan } from '../../utils/with_apm_span';
import {
percentCgroupMemoryUsedScript,
percentSystemMemoryUsedScript,
systemMemoryFilter,
cgroupMemoryFilter,
} from '../metrics/by_agent/shared/memory';
import { systemMemory, cgroupMemory } from '../metrics/by_agent/shared/memory';
import { APMEventClient } from '../../lib/helpers/create_es_client/create_apm_event_client';
import { ApmDocumentType } from '../../../common/document_type';
import { RollupInterval } from '../../../common/rollup';
Expand Down Expand Up @@ -312,9 +307,7 @@ function getMemoryStats({
script,
}: {
additionalFilters: ESFilter[];
script:
| typeof percentCgroupMemoryUsedScript
| typeof percentSystemMemoryUsedScript;
script: typeof cgroupMemory.script | typeof systemMemory.script;
}): Promise<NodeStats['memoryUsage']> => {
const response = await apmEventClient.search(
'get_avg_memory_for_service_map_node',
Expand Down Expand Up @@ -357,14 +350,14 @@ function getMemoryStats({
};

let memoryUsage = await getMemoryUsage({
script: percentCgroupMemoryUsedScript,
additionalFilters: [cgroupMemoryFilter],
script: cgroupMemory.script,
additionalFilters: [cgroupMemory.filter],
});

if (!memoryUsage) {
memoryUsage = await getMemoryUsage({
script: percentSystemMemoryUsedScript,
additionalFilters: [systemMemoryFilter],
script: systemMemory.script,
additionalFilters: [systemMemory.filter],
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import { environmentQuery } from '../../../../common/utils/environment_query';
import { getBucketSize } from '../../../../common/utils/get_bucket_size';
import { APMEventClient } from '../../../lib/helpers/create_es_client/create_apm_event_client';
import {
percentCgroupMemoryUsedScript,
percentSystemMemoryUsedScript,
systemMemoryFilter,
cgroupMemoryFilter,
systemMemory,
cgroupMemory,
} from '../../metrics/by_agent/shared/memory';
import { getOffsetInMs } from '../../../../common/utils/get_offset_in_ms';

Expand Down Expand Up @@ -109,12 +107,12 @@ export async function getServiceInstancesSystemMetricStatistics<

const subAggs = {
memory_usage_cgroup: {
filter: cgroupMemoryFilter,
aggs: withTimeseries({ script: percentCgroupMemoryUsedScript }),
filter: cgroupMemory.filter,
aggs: withTimeseries({ script: cgroupMemory.script }),
},
memory_usage_system: {
filter: systemMemoryFilter,
aggs: withTimeseries({ script: percentSystemMemoryUsedScript }),
filter: systemMemory.filter,
aggs: withTimeseries({ script: systemMemory.script }),
},
cpu_usage: {
filter: cpuUsageFilter,
Expand Down Expand Up @@ -144,8 +142,8 @@ export async function getServiceInstancesSystemMetricStatistics<
{
bool: {
should: [
cgroupMemoryFilter,
systemMemoryFilter,
cgroupMemory.filter,
systemMemory.filter,
cpuUsageFilter,
],
minimum_should_match: 1,
Expand Down

0 comments on commit bc886e1

Please sign in to comment.