Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(instrumentation/report): add problems to reports #28042

Merged
merged 19 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions lib/instrumentation/reporting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import {
addBranchStats,
addExtractionStats,
exportStats,
finalizeReport,
getReport,
} from './reporting';

jest.mock('../util/fs', () => mockDeep());
jest.mock('../util/s3', () => mockDeep());
jest.mock('../logger', () => mockDeep());
secustor marked this conversation as resolved.
Show resolved Hide resolved

describe('instrumentation/reporting', () => {
const branchInformation: Partial<BranchCache>[] = [
Expand Down Expand Up @@ -52,8 +54,10 @@ describe('instrumentation/reporting', () => {
};

const expectedReport = {
problems: [],
repositories: {
'myOrg/myRepo': {
problems: [],
branches: branchInformation,
packageFiles,
},
Expand All @@ -70,6 +74,7 @@ describe('instrumentation/reporting', () => {
});

expect(getReport()).toEqual({
problems: [],
repositories: {},
});
});
Expand Down Expand Up @@ -174,4 +179,49 @@ describe('instrumentation/reporting', () => {
fs.writeSystemFile.mockRejectedValue(null);
await expect(exportStats(config)).toResolve();
});

it('should add problems to report', () => {
const config: RenovateConfig = {
repository: 'myOrg/myRepo',
reportType: 'logging',
};
const expectedReport = {
problems: [
{
level: 30,
msg: 'a root problem',
},
],
repositories: {
'myOrg/myRepo': {
problems: [
{
level: 30,
msg: 'a repo problem',
},
],
branches: branchInformation,
packageFiles,
},
},
};

addBranchStats(config, branchInformation);
addExtractionStats(config, { branchList: [], branches: [], packageFiles });

logger.getProblems.mockReturnValue([
{
repository: 'myOrg/myRepo',
level: 30,
msg: 'a repo problem',
},
{
level: 30,
msg: 'a root problem',
},
]);
finalizeReport();

expect(getReport()).toEqual(expectedReport);
});
});
20 changes: 19 additions & 1 deletion lib/instrumentation/reporting.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { PutObjectCommand, PutObjectCommandInput } from '@aws-sdk/client-s3';
import is from '@sindresorhus/is';
import type { RenovateConfig } from '../config/types';
import { logger } from '../logger';
import { getProblems, logger } from '../logger';
import type { BranchCache } from '../util/cache/repository/types';
import { writeSystemFile } from '../util/fs';
import { getS3Client, parseS3Url } from '../util/s3';
import type { ExtractResult } from '../workers/repository/process/extract-update';
import type { Report } from './types';

const report: Report = {
problems: [],
repositories: {},
};

Expand Down Expand Up @@ -37,6 +38,22 @@ export function addExtractionStats(
extractResult.packageFiles;
}

export function finalizeReport(): void {
const allProblems = structuredClone(getProblems());
for (const problem of allProblems) {
const repository = problem.repository;
delete problem.repository;

// if the problem can be connected to a repository add it their else add to the root list
if (repository) {
coerceRepo(repository);
report.repositories[repository].problems.push(problem);
} else {
report.problems.push(problem);
}
}
}

export async function exportStats(config: RenovateConfig): Promise<void> {
try {
if (is.nullOrUndefined(config.reportType)) {
Expand Down Expand Up @@ -91,6 +108,7 @@ function coerceRepo(repository: string): void {
}

report.repositories[repository] = {
problems: [],
branches: [],
packageFiles: {},
};
Expand Down
3 changes: 3 additions & 0 deletions lib/instrumentation/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Attributes, SpanKind } from '@opentelemetry/api';
import type { BunyanRecord } from '../logger/types';
import type { PackageFile } from '../modules/manager/types';
import type { BranchCache } from '../util/cache/repository/types';

Expand Down Expand Up @@ -28,10 +29,12 @@ export interface SpanParameters {
}

export interface Report {
problems: BunyanRecord[];
repositories: Record<string, RepoReport>;
}

interface RepoReport {
problems: BunyanRecord[];
branches: Partial<BranchCache>[];
packageFiles: Record<string, PackageFile[]>;
}
6 changes: 3 additions & 3 deletions lib/workers/global/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const initPlatform = jest.spyOn(platform, 'initPlatform');

describe('workers/global/index', () => {
beforeEach(() => {
logger.getProblems.mockImplementationOnce(() => []);
logger.getProblems.mockImplementation(() => []);
secustor marked this conversation as resolved.
Show resolved Hide resolved
initPlatform.mockImplementation((input) => Promise.resolve(input));
delete process.env.AWS_SECRET_ACCESS_KEY;
delete process.env.AWS_SESSION_TOKEN;
Expand Down Expand Up @@ -149,7 +149,7 @@ describe('workers/global/index', () => {
repositories: [],
});
logger.getProblems.mockReset();
logger.getProblems.mockImplementationOnce(() => [
logger.getProblems.mockImplementation(() => [
{
level: ERROR,
msg: 'meh',
Expand All @@ -166,7 +166,7 @@ describe('workers/global/index', () => {
repositories: [],
});
logger.getProblems.mockReset();
logger.getProblems.mockImplementationOnce(() => [
logger.getProblems.mockImplementation(() => [
{
level: WARN,
msg: 'meh',
Expand Down
3 changes: 2 additions & 1 deletion lib/workers/global/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
import { CONFIG_PRESETS_INVALID } from '../../constants/error-messages';
import { pkg } from '../../expose.cjs';
import { instrument } from '../../instrumentation';
import { exportStats } from '../../instrumentation/reporting';
import { exportStats, finalizeReport } from '../../instrumentation/reporting';
import { getProblems, logger, setMeta } from '../../logger';
import { setGlobalLogLevelRemaps } from '../../logger/remap';
import * as hostRules from '../../util/host-rules';
Expand Down Expand Up @@ -212,6 +212,7 @@ export async function start(): Promise<number> {
);
}

finalizeReport();
await exportStats(config);
} catch (err) /* istanbul ignore next */ {
if (err.message.startsWith('Init: ')) {
Expand Down
Loading