Skip to content

Commit 5d04fea

Browse files
committed
fix: collect image size telemetry
1 parent c714ec7 commit 5d04fea

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

src/scanner/images/index.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { unlink } from 'fs';
1+
import { unlink, stat } from 'fs';
2+
import { promisify } from 'util';
23
import { PluginResponse, scan } from 'snyk-docker-plugin';
34
import { DepGraph, legacy } from '@snyk/dep-graph';
45

@@ -12,6 +13,9 @@ import {
1213
extractFactsFromDockerPluginResponse,
1314
LegacyPluginResponse,
1415
} from './docker-plugin-shim';
16+
import type { Telemetry } from '../../transmitter/types';
17+
18+
const statAsync = promisify(stat);
1519

1620
/*
1721
pulled images by skopeo archive repo type:
@@ -105,6 +109,7 @@ export function getImageParts(imageWithTag: string): {
105109

106110
export async function scanImages(
107111
images: IPullableImage[],
112+
telemetry: Partial<Telemetry>,
108113
): Promise<IScanResult[]> {
109114
const scannedImages: IScanResult[] = [];
110115

@@ -127,6 +132,19 @@ export async function scanImages(
127132
throw Error('Unexpected empty result from docker-plugin');
128133
}
129134

135+
try {
136+
const fileStats = await statAsync(fileSystemPath);
137+
if (!telemetry.imageSizeBytes) {
138+
telemetry.imageSizeBytes = 0;
139+
}
140+
telemetry.imageSizeBytes += fileStats.size;
141+
} catch (err) {
142+
logger.warn(
143+
{ error: err, imageName, imageWithDigest, fileSystemPath },
144+
'could not determine archive size',
145+
);
146+
}
147+
130148
const depTree = await getDependencyTreeFromPluginResponse(
131149
pluginResponse,
132150
imageName,

src/scanner/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ async function scanImagesAndSendResults(
120120
telemetry: Partial<Telemetry>,
121121
): Promise<void> {
122122
const imageScanStartTimestampMs = Date.now();
123-
const scannedImages = await scanImages(pulledImages);
123+
const scannedImages = await scanImages(pulledImages, telemetry);
124124
const imageScanDurationMs = Date.now() - imageScanStartTimestampMs;
125125

126126
if (scannedImages.length === 0) {

src/transmitter/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ export interface IRequestError {
101101
export interface Telemetry {
102102
enqueueDurationMs: number;
103103
queueSize: number;
104+
/** This metric captures the total duration to pull all images of a workload. */
104105
imagePullDurationMs: number;
106+
/** This metric captures the total duration to scan all images of a workload. */
105107
imageScanDurationMs: number;
108+
/** This metric captures the combined size of all images of a workload. */
109+
imageSizeBytes: number;
106110
}

test/system/kind.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ test('Kubernetes-Monitor with KinD', async (jestDoneCallback) => {
254254
enqueueDurationMs: expect.any(Number),
255255
imagePullDurationMs: expect.any(Number),
256256
imageScanDurationMs: expect.any(Number),
257+
imageSizeBytes: expect.any(Number),
257258
queueSize: expect.any(Number),
258259
},
259260
imageLocator: expect.objectContaining({
@@ -270,10 +271,7 @@ test('Kubernetes-Monitor with KinD', async (jestDoneCallback) => {
270271
{ type: 'imageOsReleasePrettyName', data: expect.any(String) },
271272
]),
272273
target: { image: 'docker-image|docker.io/library/java' },
273-
identity: {
274-
type: 'deb',
275-
args: { platform: 'linux/amd64' },
276-
},
274+
identity: { type: 'deb', args: { platform: 'linux/amd64' } },
277275
},
278276
{
279277
facts: [

0 commit comments

Comments
 (0)