Skip to content

Commit 96dbf08

Browse files
committed
feat(cli-repl): add configuration to set max log files size MONGOSH-1985
1 parent 33f85ff commit 96dbf08

File tree

9 files changed

+124
-6
lines changed

9 files changed

+124
-6
lines changed

package-lock.json

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

packages/cli-repl/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"is-recoverable-error": "^1.0.3",
8686
"js-yaml": "^4.1.0",
8787
"mongodb-connection-string-url": "^3.0.1",
88-
"mongodb-log-writer": "^2.1.0",
88+
"mongodb-log-writer": "^2.3.0",
8989
"numeral": "^2.0.6",
9090
"pretty-repl": "^4.0.1",
9191
"semver": "^7.5.4",

packages/cli-repl/src/cli-repl.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ describe('CliRepl', function () {
326326
'logRetentionDays',
327327
'logMaxFileCount',
328328
'logCompressionEnabled',
329+
'logRetentionGB',
329330
] satisfies (keyof CliUserConfig)[]);
330331
});
331332

@@ -1460,6 +1461,19 @@ describe('CliRepl', function () {
14601461
);
14611462
});
14621463

1464+
it('can set log retention GB', async function () {
1465+
const testLogRetentionGB = 10;
1466+
cliRepl.config.logRetentionGB = testLogRetentionGB;
1467+
await cliRepl.start(await testServer.connectionString(), {});
1468+
1469+
expect(cliRepl.getConfig('logRetentionGB')).equals(
1470+
testLogRetentionGB
1471+
);
1472+
expect(cliRepl.logManager?._options.logRetentionGB).equals(
1473+
testLogRetentionGB
1474+
);
1475+
});
1476+
14631477
it('can set log max file count', async function () {
14641478
const testMaxFileCount = 123;
14651479
cliRepl.config.logMaxFileCount = testMaxFileCount;

packages/cli-repl/src/cli-repl.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ export class CliRepl implements MongoshIOProvider {
261261
this.shellHomeDirectory.localPath('.'),
262262
retentionDays: await this.getConfig('logRetentionDays'),
263263
gzip: await this.getConfig('logCompressionEnabled'),
264+
retentionGB: await this.getConfig('logRetentionGB'),
264265
maxLogFileCount: +(
265266
process.env.MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT ||
266267
(await this.getConfig('logMaxFileCount'))

packages/e2e-tests/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"strip-ansi": "^6.0.0"
3434
},
3535
"devDependencies": {
36-
"mongodb-log-writer": "^2.1.0",
36+
"mongodb-log-writer": "^2.3.0",
3737
"@mongodb-js/eslint-config-mongosh": "^1.0.0",
3838
"@mongodb-js/oidc-mock-provider": "^0.10.2",
3939
"@mongodb-js/prettier-config-devtools": "^1.0.1",

packages/e2e-tests/test/e2e.spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,59 @@ describe('e2e', function () {
18421842
});
18431843
});
18441844

1845+
describe('with custom log retention max logs size', function () {
1846+
const customLogDir = useTmpdir();
1847+
1848+
it('should delete files once it is above the logs retention GB', async function () {
1849+
const globalConfig = path.join(homedir, 'globalconfig.conf');
1850+
await fs.writeFile(
1851+
globalConfig,
1852+
// Set logRetentionGB to 4 KB
1853+
`mongosh:\n logLocation: ${JSON.stringify(
1854+
customLogDir.path
1855+
)}\n logRetentionGB: ${4 / 1024 / 1024}`
1856+
);
1857+
const paths: string[] = [];
1858+
const offset = Math.floor(Date.now() / 1000);
1859+
1860+
// Create 10 log files, 1kb each
1861+
for (let i = 9; i >= 0; i--) {
1862+
const filename = path.join(
1863+
customLogDir.path,
1864+
ObjectId.createFromTime(offset - i).toHexString() + '_log'
1865+
);
1866+
await fs.writeFile(filename, '0'.repeat(1024));
1867+
paths.push(filename);
1868+
}
1869+
1870+
// All 10 existing log files exist.
1871+
expect(await getFilesState(paths)).to.equal('1111111111');
1872+
shell = this.startTestShell({
1873+
args: ['--nodb'],
1874+
env: {
1875+
...env,
1876+
MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT: '',
1877+
MONGOSH_GLOBAL_CONFIG_FILE_FOR_TESTING: globalConfig,
1878+
},
1879+
forceTerminal: true,
1880+
});
1881+
1882+
await shell.waitForPrompt();
1883+
1884+
// Add the newly created log to the file list.
1885+
paths.push(
1886+
path.join(customLogDir.path, `${shell.logId as string}_log`)
1887+
);
1888+
1889+
expect(
1890+
await shell.executeLine('config.get("logRetentionGB")')
1891+
).contains(`${4 / 1024 / 1024}`);
1892+
1893+
// Expect 6 files to be deleted and 5 to remain (including the new log file)
1894+
expect(await getFilesState(paths)).to.equal('00000011111');
1895+
});
1896+
});
1897+
18451898
it('creates a log file that keeps track of session events', async function () {
18461899
expect(await shell.executeLine('print(123 + 456)')).to.include('579');
18471900
const log = await readLogFile();

packages/logging/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"@mongosh/errors": "2.4.0",
2222
"@mongosh/history": "2.4.2",
2323
"@mongosh/types": "3.2.0",
24-
"mongodb-log-writer": "^2.1.0",
24+
"mongodb-log-writer": "^2.3.0",
2525
"mongodb-redact": "^1.1.5"
2626
},
2727
"devDependencies": {

packages/types/src/index.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ describe('config validation', function () {
3131
expect(await validate('logRetentionDays', -1)).to.equal(
3232
'logRetentionDays must be a positive integer'
3333
);
34+
expect(await validate('logRetentionGB', 'foo')).to.equal(
35+
'logRetentionGB must be a positive integer'
36+
);
37+
expect(await validate('logRetentionDays', -1)).to.equal(
38+
'logRetentionGB must be a positive integer'
39+
);
3440
expect(await validate('logMaxFileCount', 'foo')).to.equal(
3541
'logMaxFileCount must be a positive integer'
3642
);

packages/types/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ export class CliUserConfig extends SnippetShellUserConfig {
512512
logRetentionDays = 30;
513513
logMaxFileCount = 100;
514514
logCompressionEnabled = false;
515+
logRetentionGB: number | undefined = undefined;
515516
}
516517

517518
export class CliUserConfigValidator extends SnippetShellUserConfigValidator {
@@ -540,6 +541,11 @@ export class CliUserConfigValidator extends SnippetShellUserConfigValidator {
540541
return `${key} must be a positive integer`;
541542
}
542543
return null;
544+
case 'logRetentionGB':
545+
if (value !== undefined && (typeof value !== 'number' || value < 0)) {
546+
return `${key} must be a positive number or empty`;
547+
}
548+
return null;
543549
case 'disableLogging':
544550
case 'forceDisableTelemetry':
545551
case 'showStackTraces':

0 commit comments

Comments
 (0)