tidy up and simplify some of the logic

This commit is contained in:
Jean-Louis Leysens 2022-04-20 10:44:24 +02:00
parent 06f68a541e
commit 94ae13edaa
No known key found for this signature in database
GPG key ID: 28B3B4DFF3677CDC
3 changed files with 20 additions and 8 deletions

View file

@ -5,7 +5,8 @@
* 2.0.
*/
import type { CloudSetup, Logger } from '@kbn/cloud-plugin/server';
import type { CloudSetup } from '@kbn/cloud-plugin/server';
import type { Logger } from '@kbn/core/server';
import { readMemoryLimit } from './read_cgroup_mem_limit';
const MIN_CLOUD_OS_MEM_GB: number = 2;
@ -14,6 +15,7 @@ const MIN_CLOUD_OS_MEM_BYTES: number = MIN_CLOUD_OS_MEM_GB * Math.pow(1024, 3);
/**
* If we are on Cloud we need to ensure that we have sufficient memory available,
* if we do not Chromium cannot start. See {@link MIN_CLOUD_OS_MEM_BYTES}.
*
*/
export function systemHasInsufficientMemory(
cloud: undefined | CloudSetup,
@ -21,6 +23,6 @@ export function systemHasInsufficientMemory(
): boolean {
if (!Boolean(cloud?.isCloudEnabled || cloud?.deploymentId)) return false;
const limit = readMemoryLimit();
logger.info('Memory limit from cgroup (in bytes):', limit);
return typeof limit === 'number' && limit < MIN_CLOUD_OS_MEM_BYTES;
logger.info(`Memory limit from cgroup (in bytes): ${limit}`);
return limit < MIN_CLOUD_OS_MEM_BYTES;
}

View file

@ -24,14 +24,14 @@ const cgroupMemLimitPaths = [
* Cloud guarantees that memory limits will be enforced by cgroups. `os.totalmem` does
* not currently read memory values respecting cgroups.
*
* Until we are able to get the actual instance size from Cloud we must rely on reading
* these cgroup files.
* Until we are able to get the actual instance size from Cloud we rely on reading
* cgroup files.
*
* If successful it will return the memory limit in bytes.
*
* Taken from https://github.com/adobe/node-cgroup-metrics/blob/f43d6cf8a4a71d19c81c15bd4c3478583392cb8a/lib/memory.js#L36
*/
export function readMemoryLimit(): undefined | number {
export function readMemoryLimit(): number {
for (const path of cgroupMemLimitPaths) {
const fileContents = tryReadFile(path);
if (!fileContents) continue;
@ -39,5 +39,5 @@ export function readMemoryLimit(): undefined | number {
if (isNaN(limit)) continue;
return limit;
}
return undefined;
throw new Error('Unable to read memory limit!');
}

View file

@ -232,11 +232,21 @@ export class Screenshots {
);
}
systemHasInsufficientMemory(): boolean {
try {
return systemHasInsufficientMemory(this.cloud, this.logger.get('cloud'));
} catch (e) {
// Best effort, if we are unable to check system memory we should not
// block the ability to generate reports.
return false;
}
}
getScreenshots(options: PngScreenshotOptions): Observable<PngScreenshotResult>;
getScreenshots(options: PdfScreenshotOptions): Observable<PdfScreenshotResult>;
getScreenshots(options: ScreenshotOptions): Observable<ScreenshotResult>;
getScreenshots(options: ScreenshotOptions): Observable<ScreenshotResult> {
if (systemHasInsufficientMemory(this.cloud, this.logger.get('cloud'))) {
if (this.systemHasInsufficientMemory()) {
return throwError(() => new errors.InsufficientMemoryAvailableOnCloudError());
}
const transaction = apm.startTransaction('screenshot-pipeline', 'screenshotting');