Skip to content

Commit

Permalink
fix: add log ID to logs during image processing
Browse files Browse the repository at this point in the history
The snyk-monitor can receive hundreds of requests to process K8s workloads and logs are very hard to follow.
Prepend a log ID when processing images so that we can connect the sequence of executed steps.
  • Loading branch information
ivanstanev committed Jul 2, 2019
1 parent a3f44ae commit bc3059b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
26 changes: 16 additions & 10 deletions src/lib/kube-scanner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,32 @@ import { pullImages } from '../images';
import { constructHomebaseWorkloadPayloads, scanImages, ScanResult } from './image-scanner';

export = class WorkloadWorker {
public static async process(workloadMetadata: IKubeImage[]): Promise<IScanResponse> {
private readonly logId: string;

constructor(logId: string) {
this.logId = logId;
}

public async process(workloadMetadata: IKubeImage[]): Promise<IScanResponse> {
const allImages = workloadMetadata.map((meta) => meta.imageName);
console.log(`Queried ${allImages.length} workloads: ${allImages}.`);
console.log(`${this.logId}: Queried ${allImages.length} workloads: ${allImages}.`);
const uniqueImages = [...new Set<string>(allImages)];

console.log('Begin pulling images...');
console.log(`${this.logId}: Begin pulling images...`);
const pulledImages = await pullImages(uniqueImages);
console.log(`Pulled ${pulledImages.length} images: ${pulledImages}.`);
console.log(`${this.logId}: Pulled ${pulledImages.length} images: ${pulledImages}.`);

console.log('Begin scanning images...');
console.log(`${this.logId}: Begin scanning images...`);
const scannedImages: ScanResult[] = await scanImages(pulledImages);
console.log(`Scanned ${scannedImages.length} images: ${scannedImages.map((image) => image.image)}.`);
console.log(`${this.logId}: Scanned ${scannedImages.length} images: ${scannedImages.map((image) => image.image)}.`);

console.log('Begin constructing payloads...');
console.log(`${this.logId}: Begin constructing payloads...`);
const homebasePayloads: IDepGraphPayload[] = constructHomebaseWorkloadPayloads(scannedImages, workloadMetadata);
console.log(`Constructed ${homebasePayloads.length} payloads.`);
console.log(`${this.logId}: Constructed ${homebasePayloads.length} payloads.`);

console.log('Sending dep-graphs to homebase...');
console.log(`${this.logId}: Sending dep-graphs to homebase...`);
await sendDepGraph(...homebasePayloads);
console.log('Done!');
console.log(`${this.logId}: Done!`);

const pulledImageMetadata = workloadMetadata.filter((meta) =>
pulledImages.includes(meta.imageName));
Expand Down
16 changes: 10 additions & 6 deletions src/lib/kube-scanner/watchers/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { V1Pod } from '@kubernetes/client-node';
import * as uuidv4 from 'uuid/v4';
import WorkloadWorker = require('../../../lib/kube-scanner');
import { buildMetadataForWorkload } from '../metadata-extractor';
import { WatchEventType } from './types';

export async function podWatchHandler(eventType: string, pod: V1Pod) {
const logId = uuidv4().substring(0, 8);

if (eventType === WatchEventType.Deleted) {
return;
}
Expand All @@ -13,22 +16,23 @@ export async function podWatchHandler(eventType: string, pod: V1Pod) {

if (workloadMetadata === undefined || workloadMetadata.length === 0) {
const imageNames = pod.spec.containers.map((container) => container.image);
console.log(`Could not process the images for Pod ${pod.metadata.name}! The workload is possibly unsupported.` +
`The pod's spec has the following images: ${imageNames}`);
console.log(`${logId}: Could not process the images for Pod ${pod.metadata.name}!` +
`The workload is possibly unsupported. The pod's spec has the following images: ${imageNames}`);
return;
}

const processedImages = await WorkloadWorker.process(workloadMetadata);
const workloadWorker = new WorkloadWorker(logId);
const processedImages = await workloadWorker.process(workloadMetadata);

const processedImageNames = processedImages.imageMetadata.map((image) => image.imageName);
console.log(`Processed the following images: ${processedImageNames}.`);
console.log(`${logId}: Processed the following images: ${processedImageNames}.`);
} catch (error) {
const errorMessage = error.response
? `${error.response.statusCode} ${error.response.statusMessage}`
: error.message;
const imageNames = pod.spec.containers.map((container) => container.image);

console.log(`Could not build image metadata for pod ${pod.metadata.name}: ${errorMessage}`);
console.log(`The pod uses the following images: ${imageNames}`);
console.log(`${logId}: Could not build image metadata for pod ${pod.metadata.name}: ${errorMessage}`);
console.log(`${logId}: The pod uses the following images: ${imageNames}`);
}
}
3 changes: 2 additions & 1 deletion src/transmitter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export async function sendDepGraph(...payloads: IDepGraphPayload[]) {
},
);
} catch (error) {
console.log(`Could not send data to Homebase: ${error.message}`);
const errorMessage = error.message ? error.message : error;
console.log(`Could not send data to Homebase: ${errorMessage}`);
}
}
}

0 comments on commit bc3059b

Please sign in to comment.