From bc886e12f3a53c3c5832bd2241a41a615cd0bf8a Mon Sep 17 00:00:00 2001 From: Katerina Patticha Date: Thu, 4 May 2023 12:11:35 +0200 Subject: [PATCH] [APM] co-locate system and cgroup memory filter and script (#156654) follow up: https://github.com/elastic/kibana/pull/145348#discussion_r1184135139 --- .../metrics/by_agent/shared/memory/index.ts | 64 ++++++++++--------- .../get_service_map_service_node_info.ts | 19 ++---- ...vice_instances_system_metric_statistics.ts | 18 +++--- 3 files changed, 47 insertions(+), 54 deletions(-) diff --git a/x-pack/plugins/apm/server/routes/metrics/by_agent/shared/memory/index.ts b/x-pack/plugins/apm/server/routes/metrics/by_agent/shared/memory/index.ts index 2afc82181cbfb..be477cb6b3a79 100644 --- a/x-pack/plugins/apm/server/routes/metrics/by_agent/shared/memory/index.ts +++ b/x-pack/plugins/apm/server/routes/metrics/by_agent/shared/memory/index.ts @@ -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; @@ -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. @@ -100,7 +101,8 @@ export const percentCgroupMemoryUsedScript = { return used / total; `, -} as const; + }, +}; export async function getMemoryChartData({ environment, @@ -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', @@ -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', diff --git a/x-pack/plugins/apm/server/routes/service_map/get_service_map_service_node_info.ts b/x-pack/plugins/apm/server/routes/service_map/get_service_map_service_node_info.ts index d17811c8aea39..f9db8bdeb09fb 100644 --- a/x-pack/plugins/apm/server/routes/service_map/get_service_map_service_node_info.ts +++ b/x-pack/plugins/apm/server/routes/service_map/get_service_map_service_node_info.ts @@ -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'; @@ -312,9 +307,7 @@ function getMemoryStats({ script, }: { additionalFilters: ESFilter[]; - script: - | typeof percentCgroupMemoryUsedScript - | typeof percentSystemMemoryUsedScript; + script: typeof cgroupMemory.script | typeof systemMemory.script; }): Promise => { const response = await apmEventClient.search( 'get_avg_memory_for_service_map_node', @@ -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], }); } diff --git a/x-pack/plugins/apm/server/routes/services/get_service_instances/get_service_instances_system_metric_statistics.ts b/x-pack/plugins/apm/server/routes/services/get_service_instances/get_service_instances_system_metric_statistics.ts index 6a5f57cfe9d2f..b2925af05946a 100644 --- a/x-pack/plugins/apm/server/routes/services/get_service_instances/get_service_instances_system_metric_statistics.ts +++ b/x-pack/plugins/apm/server/routes/services/get_service_instances/get_service_instances_system_metric_statistics.ts @@ -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'; @@ -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, @@ -144,8 +142,8 @@ export async function getServiceInstancesSystemMetricStatistics< { bool: { should: [ - cgroupMemoryFilter, - systemMemoryFilter, + cgroupMemory.filter, + systemMemory.filter, cpuUsageFilter, ], minimum_should_match: 1,