Skip to content

Commit 11480e3

Browse files
committed
Add telemetry for restoring dependency caches
1 parent 665891b commit 11480e3

File tree

5 files changed

+78
-14
lines changed

5 files changed

+78
-14
lines changed

lib/init-action.js

Lines changed: 22 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dependency-caching.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,44 @@ async function makeGlobber(patterns: string[]): Promise<glob.Globber> {
8484
return glob.create(patterns.join("\n"));
8585
}
8686

87+
/** Enumerates possible outcomes for cache hits. */
88+
export enum CacheHitResult {
89+
/** We were unable to calculate a hash for the key. */
90+
NoHash = "no-hash",
91+
/** No cache was found. */
92+
Miss = "miss",
93+
/** The primary cache key matched. */
94+
Exact = "exact",
95+
/** A restore key matched. */
96+
Partial = "partial",
97+
}
98+
99+
/** Represents results of trying to restore a dependency cache for a language. */
100+
export interface DependencyCacheRestoreStatus {
101+
hit: CacheHitResult;
102+
download_size_bytes?: number;
103+
download_duration_ms?: number;
104+
}
105+
106+
/** A partial mapping from languages to the results of restoring dependency caches for them. */
107+
export type DependencyCacheRestoreStatusReport = Partial<
108+
Record<Language, DependencyCacheRestoreStatus>
109+
>;
110+
87111
/**
88112
* Attempts to restore dependency caches for the languages being analyzed.
89113
*
90114
* @param languages The languages being analyzed.
91115
* @param logger A logger to record some informational messages to.
92116
* @param minimizeJavaJars Whether the Java extractor should rewrite downloaded JARs to minimize their size.
93-
* @returns A list of languages for which dependency caches were restored.
117+
* @returns A partial mapping of languages to results of restoring dependency caches for them.
94118
*/
95119
export async function downloadDependencyCaches(
96120
languages: Language[],
97121
logger: Logger,
98122
minimizeJavaJars: boolean,
99-
): Promise<Language[]> {
100-
const restoredCaches: Language[] = [];
123+
): Promise<DependencyCacheRestoreStatusReport> {
124+
const status: DependencyCacheRestoreStatusReport = {};
101125

102126
for (const language of languages) {
103127
const cacheConfig = getDefaultCacheConfig()[language];
@@ -114,6 +138,7 @@ export async function downloadDependencyCaches(
114138
const globber = await makeGlobber(cacheConfig.hash);
115139

116140
if ((await globber.glob()).length === 0) {
141+
status[language] = { hit: CacheHitResult.NoHash };
117142
logger.info(
118143
`Skipping download of dependency cache for ${language} as we cannot calculate a hash for the cache key.`,
119144
);
@@ -131,21 +156,29 @@ export async function downloadDependencyCaches(
131156
)}`,
132157
);
133158

159+
const start = performance.now();
134160
const hitKey = await actionsCache.restoreCache(
135161
cacheConfig.paths,
136162
primaryKey,
137163
restoreKeys,
138164
);
165+
const download_duration_ms = Math.round(performance.now() - start);
166+
const download_size_bytes = Math.round(
167+
await getTotalCacheSize(cacheConfig.paths, logger),
168+
);
139169

140170
if (hitKey !== undefined) {
141171
logger.info(`Cache hit on key ${hitKey} for ${language}.`);
142-
restoredCaches.push(language);
172+
const hit =
173+
hitKey === primaryKey ? CacheHitResult.Exact : CacheHitResult.Partial;
174+
status[language] = { hit, download_duration_ms, download_size_bytes };
143175
} else {
176+
status[language] = { hit: CacheHitResult.Miss };
144177
logger.info(`No suitable cache found for ${language}.`);
145178
}
146179
}
147180

148-
return restoredCaches;
181+
return status;
149182
}
150183

151184
/**

src/init-action.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import {
2323
} from "./caching-utils";
2424
import { CodeQL } from "./codeql";
2525
import * as configUtils from "./config-utils";
26-
import { downloadDependencyCaches } from "./dependency-caching";
26+
import {
27+
DependencyCacheRestoreStatusReport,
28+
downloadDependencyCaches,
29+
} from "./dependency-caching";
2730
import {
2831
addDiagnostic,
2932
flushDiagnostics,
@@ -102,6 +105,7 @@ async function sendCompletedStatusReport(
102105
toolsSource: ToolsSource,
103106
toolsVersion: string,
104107
overlayBaseDatabaseStats: OverlayBaseDatabaseDownloadStats | undefined,
108+
dependencyCachingResults: DependencyCacheRestoreStatusReport | undefined,
105109
logger: Logger,
106110
error?: Error,
107111
) {
@@ -151,6 +155,7 @@ async function sendCompletedStatusReport(
151155
await getTotalCacheSize(Object.values(config.trapCaches), logger),
152156
),
153157
overlayBaseDatabaseStats,
158+
dependencyCachingResults,
154159
);
155160
await sendStatusReport({
156161
...initWithConfigStatusReport,
@@ -351,6 +356,7 @@ async function run() {
351356
}
352357

353358
let overlayBaseDatabaseStats: OverlayBaseDatabaseDownloadStats | undefined;
359+
let dependencyCachingResults: DependencyCacheRestoreStatusReport | undefined;
354360
try {
355361
if (
356362
config.overlayDatabaseMode === OverlayDatabaseMode.Overlay &&
@@ -562,7 +568,7 @@ async function run() {
562568
codeql,
563569
);
564570
if (shouldRestoreCache(config.dependencyCachingEnabled)) {
565-
await downloadDependencyCaches(
571+
dependencyCachingResults = await downloadDependencyCaches(
566572
config.languages,
567573
logger,
568574
minimizeJavaJars,
@@ -714,6 +720,7 @@ async function run() {
714720
toolsSource,
715721
toolsVersion,
716722
overlayBaseDatabaseStats,
723+
dependencyCachingResults,
717724
logger,
718725
error,
719726
);
@@ -736,6 +743,7 @@ async function run() {
736743
toolsSource,
737744
toolsVersion,
738745
overlayBaseDatabaseStats,
746+
dependencyCachingResults,
739747
logger,
740748
);
741749
}

src/status-report.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ const testCreateInitWithConfigStatusReport = test.macro({
286286
undefined,
287287
1024,
288288
undefined,
289+
undefined,
289290
);
290291

291292
if (t.truthy(initWithConfigStatusReport)) {

src/status-report.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from "./actions-util";
1414
import { getAnalysisKey, getApiClient } from "./api-client";
1515
import { parseRegistriesWithoutCredentials, type Config } from "./config-utils";
16+
import { DependencyCacheRestoreStatusReport } from "./dependency-caching";
1617
import { DocUrl } from "./doc-url";
1718
import { EnvVar } from "./environment";
1819
import { getRef } from "./git-utils";
@@ -497,6 +498,8 @@ export interface InitWithConfigStatusReport extends InitStatusReport {
497498
overlay_base_database_download_size_bytes?: number;
498499
/** Time taken to download the overlay-base database, in milliseconds. */
499500
overlay_base_database_download_duration_ms?: number;
501+
/** Stringified JSON object representing information about the results of restoring dependency caches. */
502+
dependency_caching_restore_results: string;
500503
/** Stringified JSON array of registry configuration objects, from the 'registries' config field
501504
or workflow input. **/
502505
registries: string;
@@ -522,6 +525,7 @@ export async function createInitWithConfigStatusReport(
522525
configFile: string | undefined,
523526
totalCacheSize: number,
524527
overlayBaseDatabaseStats: OverlayBaseDatabaseDownloadStats | undefined,
528+
dependencyCachingResults: DependencyCacheRestoreStatusReport | undefined,
525529
): Promise<InitWithConfigStatusReport> {
526530
const languages = config.languages.join(",");
527531
const paths = (config.originalUserInput.paths || []).join(",");
@@ -570,6 +574,9 @@ export async function createInitWithConfigStatusReport(
570574
overlayBaseDatabaseStats?.databaseSizeBytes,
571575
overlay_base_database_download_duration_ms:
572576
overlayBaseDatabaseStats?.databaseDownloadDurationMs,
577+
dependency_caching_restore_results: JSON.stringify(
578+
dependencyCachingResults ?? {},
579+
),
573580
query_filters: JSON.stringify(
574581
config.originalUserInput["query-filters"] ?? [],
575582
),

0 commit comments

Comments
 (0)