-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[APM] Check if metric fields exist (#145348)
Fixes the error ``` "script_stack": [ "[email protected]/org.elasticsearch.index.fielddata.ScriptDocValues.throwIfEmpty(ScriptDocValues.java:92)", "[email protected]/org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:110)", "[email protected]/org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:105)", """total = useCgroupLimit ? doc[limitKey].value : doc['system.memory.total'].value; double """, " ^---- HERE" ``` connected elastic/sdh-apm#765 (internal) --------- Co-authored-by: kibanamachine <[email protected]> Co-authored-by: Søren Louv-Jansen <[email protected]>
- Loading branch information
1 parent
61c82dc
commit 98165d2
Showing
8 changed files
with
263 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 63 additions & 6 deletions
69
x-pack/plugins/apm/server/routes/metrics/__snapshots__/queries.test.ts.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
x-pack/test/apm_api_integration/tests/metrics/memory/generate_data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import { apm, timerange } from '@kbn/apm-synthtrace-client'; | ||
import type { ApmSynthtraceEsClient } from '@kbn/apm-synthtrace'; | ||
|
||
const gigabytesToBytes = (value: number) => value * Math.pow(1024, 3); | ||
|
||
export const config = { | ||
memoryTotal: gigabytesToBytes(64), | ||
memoryFree: gigabytesToBytes(5.5), | ||
cGroupMemoryLimit: gigabytesToBytes(8), | ||
cGroupMemoryUsage: gigabytesToBytes(1.5), | ||
}; | ||
|
||
export const expectedValues = { | ||
expectedMemoryUsedRate: (config.memoryTotal - config.memoryFree) / config.memoryTotal, | ||
expectedMemoryUsed: config.memoryTotal - config.memoryFree, | ||
}; | ||
|
||
export async function generateData({ | ||
synthtraceEsClient, | ||
start, | ||
end, | ||
}: { | ||
synthtraceEsClient: ApmSynthtraceEsClient; | ||
start: number; | ||
end: number; | ||
}) { | ||
const { memoryTotal, memoryFree, cGroupMemoryLimit, cGroupMemoryUsage } = config; | ||
|
||
const systemMetricOnlyInstance = apm | ||
.service({ name: 'system-metric-only-service', environment: 'production', agentName: 'go' }) | ||
.instance('system-metric-only-production'); | ||
|
||
const cGroupMemoryOnlyInstance = apm | ||
.service({ name: 'cgroup-memory-only-service', environment: 'production', agentName: 'go' }) | ||
.instance('cgroup-memory-only-production'); | ||
|
||
const cGroupMemoryWithLimitInstance = apm | ||
.service({ | ||
name: 'cgroup-memory-with-limit-production', | ||
environment: 'production', | ||
agentName: 'go', | ||
}) | ||
.instance('cgroup-memory-with-limit-production'); | ||
|
||
const transactionsEvents = timerange(start, end) | ||
.ratePerMinute(1) | ||
.generator((timestamp) => [ | ||
systemMetricOnlyInstance | ||
.appMetrics({ | ||
'system.memory.actual.free': memoryFree, | ||
'system.memory.total': memoryTotal, | ||
}) | ||
.timestamp(timestamp), | ||
cGroupMemoryOnlyInstance | ||
.appMetrics({ | ||
'system.process.cgroup.memory.mem.usage.bytes': cGroupMemoryUsage, | ||
}) | ||
.timestamp(timestamp), | ||
|
||
cGroupMemoryWithLimitInstance | ||
.appMetrics({ | ||
'system.process.cgroup.memory.mem.usage.bytes': cGroupMemoryUsage, | ||
'system.process.cgroup.memory.mem.limit.bytes': cGroupMemoryLimit, | ||
'system.memory.total': memoryTotal, | ||
'system.memory.actual.free': memoryFree, | ||
}) | ||
.timestamp(timestamp), | ||
]); | ||
|
||
await synthtraceEsClient.index(transactionsEvents); | ||
} |
83 changes: 83 additions & 0 deletions
83
x-pack/test/apm_api_integration/tests/metrics/memory/memory_metrics.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import expect from '@kbn/expect'; | ||
import { FtrProviderContext } from '../../../common/ftr_provider_context'; | ||
import { config, generateData } from './generate_data'; | ||
|
||
export default function ApiTest({ getService }: FtrProviderContext) { | ||
const registry = getService('registry'); | ||
const apmApiClient = getService('apmApiClient'); | ||
const synthtraceEsClient = getService('synthtraceEsClient'); | ||
|
||
const start = new Date('2023-01-01T00:00:00.000Z').getTime(); | ||
const end = new Date('2023-01-01T00:15:00.000Z').getTime() - 1; | ||
|
||
async function callMetricChartsAPI(serviceName: string) { | ||
return await apmApiClient.readUser({ | ||
endpoint: `GET /internal/apm/services/{serviceName}/metrics/charts`, | ||
params: { | ||
path: { serviceName }, | ||
query: { | ||
environment: 'production', | ||
start: new Date(start).toISOString(), | ||
end: new Date(end).toISOString(), | ||
kuery: '', | ||
agentName: 'go', | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
registry.when('Memory', { config: 'trial', archives: [] }, () => { | ||
before(async () => { | ||
await generateData({ start, end, synthtraceEsClient }); | ||
}); | ||
|
||
after(() => synthtraceEsClient.clean()); | ||
|
||
it('returns system memory stats', async () => { | ||
const expectedFreeMemory = 1 - config.memoryFree / config.memoryTotal; | ||
|
||
const { status, body } = await callMetricChartsAPI('system-metric-only-service'); | ||
const memoryChart = body.charts.find(({ key }) => key === 'memory_usage_chart'); | ||
|
||
expect(status).to.be(200); | ||
[ | ||
{ title: 'Max', expectedValue: expectedFreeMemory }, | ||
{ title: 'Average', expectedValue: expectedFreeMemory }, | ||
].map(({ title, expectedValue }) => { | ||
const series = memoryChart?.series.find((item) => item.title === title); | ||
expect(series?.overallValue).to.eql(expectedValue); | ||
}); | ||
}); | ||
|
||
it('returns cgroup memory with system.process.cgroup.memory.mem.limit.bytes', async () => { | ||
const expectedFreeMemory = config.cGroupMemoryUsage / config.cGroupMemoryLimit; | ||
|
||
const { status, body } = await callMetricChartsAPI('cgroup-memory-with-limit-production'); | ||
const memoryChart = body.charts.find(({ key }) => key === 'memory_usage_chart'); | ||
|
||
expect(status).to.be(200); | ||
[ | ||
{ title: 'Max', expectedValue: expectedFreeMemory }, | ||
{ title: 'Average', expectedValue: expectedFreeMemory }, | ||
].map(({ title, expectedValue }) => { | ||
const series = memoryChart?.series.find((item) => item.title === title); | ||
expect(series?.overallValue).to.eql(expectedValue); | ||
}); | ||
}); | ||
|
||
it('handles cgroup memory stats when system.process.cgroup.memory.mem.limit.bytes and system.memory.total are not present', async () => { | ||
const { status, body } = await callMetricChartsAPI('cgroup-memory-only-production'); | ||
expect(status).to.be(200); | ||
|
||
const memoryChart = body.charts.find(({ key }) => key === 'memory_usage_chart'); | ||
expect(memoryChart?.series).to.eql([]); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.