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(NODE-4810): define the new Logger #3475

Merged
merged 51 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
9eb25b1
feat: finish Logger env options parsing
andymina Nov 21, 2022
ff792fd
feat: implement new Logger constructor
andymina Nov 22, 2022
fae8706
feat: restructure Logger options
andymina Nov 22, 2022
b9d915c
chore: remove sandbox file
andymina Nov 22, 2022
c7f28f0
test: add test for MongoLogger
andymina Nov 22, 2022
6cdb0c3
refactor: use global process instead of importing
andymina Nov 22, 2022
36ee2ad
refactor: rename logger to MongoLogger and add docs
andymina Nov 23, 2022
007b109
test: use MongoLogger name in tests
andymina Nov 23, 2022
de11ba4
docs: add remarks tag
andymina Nov 23, 2022
5dd9b90
fix: correct typo in test
andymina Nov 23, 2022
07b8129
fix: address comments from review
andymina Nov 23, 2022
e62f042
test: restructure tests
andymina Nov 23, 2022
c24a6ec
test: add more tests for MongoLogger
andymina Nov 28, 2022
84a116f
refactor: revert changes to getInt and getUint
andymina Nov 28, 2022
6788243
fix: remove .only from tests
andymina Nov 28, 2022
24010a6
refactor: ignore invalid env vars
andymina Nov 29, 2022
7dafcfa
fix: remove unused imports
andymina Nov 29, 2022
d41bcda
refactor: only accept out or err for logDestination
andymina Nov 29, 2022
893e464
feat: add logger feature flag
andymina Nov 29, 2022
0b7b6e3
test: add feature flag tests
andymina Nov 29, 2022
0cfda96
fix: remove unused import in mongo_logger
andymina Nov 29, 2022
f66f973
fix: manually add feature flag to test
andymina Nov 29, 2022
159aac0
test: reformat testing structure
andymina Nov 30, 2022
c1c0421
test: restructure tests to use context
andymina Nov 30, 2022
1a022fe
refactor: address comments
andymina Dec 1, 2022
dca9796
refactor: make mongoLogger non-existent
andymina Dec 2, 2022
a69b493
test: refactor feature flag tests
andymina Dec 2, 2022
8db2be5
fix: adjust typo in tests
andymina Dec 2, 2022
88b42dd
Merge branch 'main' into NODE-4810
andymina Dec 2, 2022
c676b21
fix: remove .only from test
andymina Dec 2, 2022
3e1e33d
Update test/unit/mongo_logger.test.ts
baileympearson Dec 6, 2022
f236d55
refactor: clean up mongo_logger tests
baileympearson Dec 6, 2022
18db449
better name
baileympearson Dec 6, 2022
2ca2cac
add mongo client test
baileympearson Dec 6, 2022
73b3535
add feature flag tests
baileympearson Dec 6, 2022
a52493b
add severity helper stream tests when helpers are off
baileympearson Dec 6, 2022
e749110
clean up mongo logger
baileympearson Dec 6, 2022
81b62fd
Update test/integration/node-specific/feature_flags.test.ts
baileympearson Dec 7, 2022
a6eee37
Update test/integration/node-specific/feature_flags.test.ts
baileympearson Dec 7, 2022
1c86ce6
Update test/integration/node-specific/feature_flags.test.ts
baileympearson Dec 7, 2022
36bd259
cleanup test wording
baileympearson Dec 7, 2022
bd10d81
rework mongodb log destination tests
baileympearson Dec 7, 2022
67fd13d
add tests for componentSeverities
baileympearson Dec 8, 2022
85d598e
restructure mongo_logger file slightly
baileympearson Dec 8, 2022
fdcfc3e
chore: address Daria's comments in feature flags tests
baileympearson Dec 12, 2022
32f1f26
chore: address Daria's comments in mongo logger tests
baileympearson Dec 12, 2022
7b07723
remove unnecessary block
baileympearson Dec 13, 2022
5b18ef0
chore: add todo for writable in mongo_logger
baileympearson Dec 14, 2022
5262ca0
refactor uint parsing
baileympearson Dec 14, 2022
e72a8c1
fix imports
baileympearson Dec 14, 2022
997a7f2
better naming for parseInt and parseUInt
baileympearson Dec 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 26 additions & 9 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ import {
DEFAULT_PK_FACTORY,
emitWarning,
emitWarningOnce,
getInt,
getUint,
getInt as getIntFromOptions,
getUint as getUIntFromOptions,
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
HostAddress,
isRecord,
makeClientMetadata,
parseInt,
setDifference
} from './utils';
import { W, WriteConcern } from './write_concern';
Expand Down Expand Up @@ -202,6 +203,22 @@ function getBoolean(name: string, value: unknown): boolean {
throw new MongoParseError(`Expected ${name} to be stringified boolean value, got: ${value}`);
}

function getIntFromOptions(name: string, value: unknown): number {
const parsedInt = parseInt(value);
if (parsedInt != null) {
return parsedInt;
}
throw new MongoParseError(`Expected ${name} to be stringified int value, got: ${value}`);
}

function getUIntFromOptions(name: string, value: unknown): number {
const parsedValue = getIntFromOptions(name, value);
if (parsedValue < 0) {
throw new MongoParseError(`${name} can only be a positive int value, got: ${value}`);
}
return parsedValue;
}

function* entriesFromString(value: string): Generator<[string, string]> {
const keyValuePairs = value.split(',');
for (const keyValue of keyValuePairs) {
Expand Down Expand Up @@ -573,10 +590,10 @@ function setOption(
mongoOptions[name] = getBoolean(name, values[0]);
break;
case 'int':
mongoOptions[name] = getInt(name, values[0]);
mongoOptions[name] = getIntFromOptions(name, values[0]);
break;
case 'uint':
mongoOptions[name] = getUint(name, values[0]);
mongoOptions[name] = getUIntFromOptions(name, values[0]);
break;
case 'string':
if (values[0] == null) {
Expand Down Expand Up @@ -782,7 +799,7 @@ export const OPTIONS = {
enableUtf8Validation: { type: 'boolean', default: true },
family: {
transform({ name, values: [value] }): 4 | 6 {
const transformValue = getInt(name, value);
const transformValue = getIntFromOptions(name, value);
if (transformValue === 4 || transformValue === 6) {
return transformValue;
}
Expand Down Expand Up @@ -881,7 +898,7 @@ export const OPTIONS = {
maxConnecting: {
default: 2,
transform({ name, values: [value] }): number {
const maxConnecting = getUint(name, value);
const maxConnecting = getUIntFromOptions(name, value);
if (maxConnecting === 0) {
throw new MongoInvalidArgumentError('maxConnecting must be > 0 if specified');
}
Expand All @@ -899,7 +916,7 @@ export const OPTIONS = {
maxStalenessSeconds: {
target: 'readPreference',
transform({ name, options, values: [value] }) {
const maxStalenessSeconds = getUint(name, value);
const maxStalenessSeconds = getUIntFromOptions(name, value);
if (options.readPreference) {
return ReadPreference.fromOptions({
readPreference: { ...options.readPreference, maxStalenessSeconds }
Expand Down Expand Up @@ -1218,7 +1235,7 @@ export const OPTIONS = {
const wc = WriteConcern.fromOptions({
writeConcern: {
...options.writeConcern,
wtimeout: getUint('wtimeout', value)
wtimeout: getUIntFromOptions('wtimeout', value)
}
});
if (wc) return wc;
Expand All @@ -1231,7 +1248,7 @@ export const OPTIONS = {
const wc = WriteConcern.fromOptions({
writeConcern: {
...options.writeConcern,
wtimeoutMS: getUint('wtimeoutMS', value)
wtimeoutMS: getUIntFromOptions('wtimeoutMS', value)
}
});
if (wc) return wc;
Expand Down
21 changes: 3 additions & 18 deletions src/mongo_logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Writable } from 'stream';

import { getUint } from './utils';
import { getUint, parseUInt } from './utils';

/** @internal */
export const SeverityLevel = Object.freeze({
Expand Down Expand Up @@ -93,21 +93,6 @@ function parseSeverityFromString(s?: string): SeverityLevel | null {
return null;
}

/**
* Parses a string to be a number greater than or equal to 0 for maxDocumentLength.
*
* @param s - the value to be parsed
* @returns the int value parsed or 1000 if the value could not be parsed
*/
function parseMaxDocumentLength(s?: string): number {
try {
const maxDocumentLength = getUint('MONGODB_LOG_MAX_DOCUMENT_LENGTH', s);
return maxDocumentLength;
} catch {
return 1000;
}
}

/**
* resolves the MONGODB_LOG_PATH and mongodbLogPath options from the environment and the
* mongo client options respectively.
Expand All @@ -128,7 +113,7 @@ function resolveLogPath(
return mongodbLogPath.toLowerCase() === 'stderr' ? process.stderr : process.stdout;
}

// TODO(NODE-4886): check for minimal interface instead of instanceof Writable
// TODO(NODE-4813): check for minimal interface instead of instanceof Writable
if (typeof mongodbLogPath === 'object' && mongodbLogPath instanceof Writable) {
dariakp marked this conversation as resolved.
Show resolved Hide resolved
return mongodbLogPath;
}
Expand Down Expand Up @@ -208,7 +193,7 @@ export class MongoLogger {
parseSeverityFromString(combinedOptions.MONGODB_LOG_CONNECTION) ?? defaultSeverity,
default: defaultSeverity
},
maxDocumentLength: parseMaxDocumentLength(combinedOptions.MONGODB_LOG_MAX_DOCUMENT_LENGTH),
maxDocumentLength: parseUInt(combinedOptions.MONGODB_LOG_MAX_DOCUMENT_LENGTH) ?? 1000,
logDestination: combinedOptions.mongodbLogPath
};
}
Expand Down
16 changes: 7 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1422,17 +1422,15 @@ export function compareObjectId(oid1?: ObjectId | null, oid2?: ObjectId | null):
return oid1.id.compare(oid2.id);
}

export function getInt(name: string, value: unknown): number {
export function parseInt(value: unknown): number | null {
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
if (typeof value === 'number') return Math.trunc(value);
const parsedValue = Number.parseInt(String(value), 10);
if (!Number.isNaN(parsedValue)) return parsedValue;
throw new MongoParseError(`Expected ${name} to be stringified int value, got: ${value}`);

return Number.isNaN(parsedValue) ? null : parsedValue;
}

export function getUint(name: string, value: unknown): number {
const parsedValue = getInt(name, value);
if (parsedValue < 0) {
throw new MongoParseError(`${name} can only be a positive int value, got: ${value}`);
}
return parsedValue;
export function parseUInt(value: unknown): number | null {
const parsedInt = parseInt(value);

return parsedInt != null && parsedInt >= 0 ? parsedInt : null;
}