Add telemetry for TRAP cache cleanup

This commit is contained in:
Henry Mercer 2024-05-22 12:38:40 +01:00
parent 59af9fc5ab
commit 4f2b1826e9
6 changed files with 57 additions and 25 deletions

View file

@ -35,6 +35,7 @@ import {
import {
cleanupTrapCaches,
getTotalCacheSize,
TrapCacheCleanupStatusReport,
uploadTrapCaches,
} from "./trap-caching";
import * as uploadLib from "./upload-lib";
@ -65,6 +66,7 @@ async function sendStatusReport(
trapCacheUploadTime: number | undefined,
dbCreationTimings: DatabaseCreationTimings | undefined,
didUploadTrapCaches: boolean,
trapCacheCleanup: TrapCacheCleanupStatusReport | undefined,
logger: Logger,
) {
const status = getActionsStatus(error, stats?.analyze_failure_language);
@ -83,6 +85,7 @@ async function sendStatusReport(
...statusReportBase,
...(stats || {}),
...(dbCreationTimings || {}),
...(trapCacheCleanup || {}),
};
if (config && didUploadTrapCaches) {
const trapCacheUploadStatusReport: FinishWithTrapUploadStatusReport = {
@ -193,6 +196,8 @@ async function run() {
let uploadResult: UploadResult | undefined = undefined;
let runStats: QueriesStatusReport | undefined = undefined;
let config: Config | undefined = undefined;
let trapCacheCleanupTelemetry: TrapCacheCleanupStatusReport | undefined =
undefined;
let trapCacheUploadTime: number | undefined = undefined;
let dbCreationTimings: DatabaseCreationTimings | undefined = undefined;
let didUploadTrapCaches = false;
@ -316,7 +321,7 @@ async function run() {
trapCacheUploadTime = performance.now() - trapCacheUploadStartTime;
// Clean up TRAP caches
await cleanupTrapCaches(config, logger);
trapCacheCleanupTelemetry = await cleanupTrapCaches(config, logger);
// We don't upload results in test mode, so don't wait for processing
if (util.isInTestMode()) {
@ -357,6 +362,7 @@ async function run() {
trapCacheUploadTime,
dbCreationTimings,
didUploadTrapCaches,
trapCacheCleanupTelemetry,
logger,
);
} else {
@ -368,6 +374,7 @@ async function run() {
trapCacheUploadTime,
dbCreationTimings,
didUploadTrapCaches,
trapCacheCleanupTelemetry,
logger,
);
}
@ -387,6 +394,7 @@ async function run() {
trapCacheUploadTime,
dbCreationTimings,
didUploadTrapCaches,
trapCacheCleanupTelemetry,
logger,
);
} else if (runStats) {
@ -398,6 +406,7 @@ async function run() {
trapCacheUploadTime,
dbCreationTimings,
didUploadTrapCaches,
trapCacheCleanupTelemetry,
logger,
);
} else {
@ -409,6 +418,7 @@ async function run() {
trapCacheUploadTime,
dbCreationTimings,
didUploadTrapCaches,
trapCacheCleanupTelemetry,
logger,
);
}

View file

@ -9,7 +9,7 @@ import { CodeQL } from "./codeql";
import type { Config } from "./config-utils";
import { Language } from "./languages";
import { Logger } from "./logging";
import { isHTTPError, tryGetFolderBytes, withTimeout } from "./util";
import { isHTTPError, tryGetFolderBytes, withTimeout, wrapError } from "./util";
// This constant should be bumped if we make a breaking change
// to how the CodeQL Action stores or retrieves the TRAP cache,
@ -161,10 +161,24 @@ export async function uploadTrapCaches(
return true;
}
export async function cleanupTrapCaches(config: Config, logger: Logger) {
if (!(await actionsUtil.isAnalyzingDefaultBranch())) return;
export interface TrapCacheCleanupStatusReport {
trap_cache_cleanup_error?: string;
trap_cache_cleanup_size_bytes?: number;
trap_cache_cleanup_skipped_because?: string;
}
export async function cleanupTrapCaches(
config: Config,
logger: Logger,
): Promise<TrapCacheCleanupStatusReport> {
if (!(await actionsUtil.isAnalyzingDefaultBranch())) {
return {
trap_cache_cleanup_skipped_because: "not analyzing default branch",
};
}
try {
let totalBytesCleanedUp = 0;
for (const language of config.languages) {
if (config.trapCaches[language]) {
const cachesToRemove = await getTrapCachesForLanguage(language, logger);
@ -179,19 +193,18 @@ export async function cleanupTrapCaches(config: Config, logger: Logger) {
logger.debug(`Deleting old TRAP cache (${JSON.stringify(cache)})`);
await apiClient.deleteActionsCache(cache.id);
}
const totalBytesCleanedUp = cachesToRemove.reduce(
const bytesCleanedUp = cachesToRemove.reduce(
(acc, item) => acc + item.size_in_bytes,
0,
);
const totalMegabytesCleanedUp = (
totalBytesCleanedUp /
(1024 * 1024)
).toFixed(2);
totalBytesCleanedUp += bytesCleanedUp;
const megabytesCleanedUp = (bytesCleanedUp / (1024 * 1024)).toFixed(2);
logger.info(
`Cleaned up ${totalMegabytesCleanedUp} MiB of old TRAP caches for ${language}.`,
`Cleaned up ${megabytesCleanedUp} MiB of old TRAP caches for ${language}.`,
);
}
}
return { trap_cache_cleanup_size_bytes: totalBytesCleanedUp };
} catch (e) {
if (isHTTPError(e) && e.status === 403) {
logger.warning(
@ -202,6 +215,7 @@ export async function cleanupTrapCaches(config: Config, logger: Logger) {
} else {
logger.info(`Failed to cleanup TRAP caches, continuing. Details: ${e}`);
}
return { trap_cache_cleanup_error: wrapError(e).message };
}
}