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: enable custom json fields truncation for bunyan #732

Merged
merged 3 commits into from
Dec 8, 2023
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
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ export function getCurrentTraceFromAgent() {
* attempted before returning the error.
* @param {constructor} [options.promise] Custom promise module to use instead
* of native Promises.
*
* @param {constructor} [options.promise] Custom promise module to use instead
* of native Promises.
* @param {number} [options.maxEntrySize] Max size limit of a log entry.
* @param {string[]} [options.jsonFieldsToTruncate] A list of JSON properties at the given full path to be truncated.
* @example Import the client library
* ```
* const {LoggingBunyan} = require('@google-cloud/logging-bunyan');
Expand Down Expand Up @@ -199,6 +202,7 @@ export class LoggingBunyan extends Writable {
// 250,000 has been chosen to keep us comfortably within the
// 256,000 limit.
maxEntrySize: options.maxEntrySize || 250000,
jsonFieldsToTruncate: options.jsonFieldsToTruncate,
});
} else {
const logSyncOptions: LogSyncOptions = {
Expand Down
11 changes: 9 additions & 2 deletions src/types/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,16 @@ export interface Options {
* Defaults to `logging.googleapis.com`.
*/
apiEndpoint?: string;
// An attempt will be made to truncate messages larger than maxEntrySize.
// Please note that this parameter is ignored when redirectToStdout is set.
/**
* An attempt will be made to truncate messages larger than maxEntrySize.
* Please note that this parameter is ignored when redirectToStdout is set.
*/
maxEntrySize?: number;
/**
* A list of JSON properties at the given full path to be truncated.
* Received values will be prepended to predefined list in the order received and duplicates discarded.
*/
jsonFieldsToTruncate?: string[];
// A default global callback to be used for {@link LoggingBunyan} write calls
// when callback is not supplied by caller in function parameters
defaultCallback?: ApiResponseCallback;
Expand Down
16 changes: 15 additions & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface Options {
service: string;
};
apiEndpoint: string;
jsonFieldsToTruncate: string[];
}
interface FakeLogType {
entry?: () => void;
Expand Down Expand Up @@ -106,13 +107,17 @@ describe('logging-bunyan', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let loggingBunyan: any;

const TRUNCATE_FIELD =
'jsonPayload.fields.metadata.structValue.fields.custom.stringValue';

const OPTIONS = {
logName: 'log-name',
resource: {},
serviceContext: {
service: 'fake-service',
},
apiEndpoint: 'fake.local',
jsonFieldsToTruncate: [TRUNCATE_FIELD],
};

const RECORD = {
Expand Down Expand Up @@ -149,7 +154,15 @@ describe('logging-bunyan', () => {
assert.strictEqual(fakeLogName_, OPTIONS.logName);
});

it('should localize Log instance using default name, options', () => {
it('should localize Log instance using provided jsonFieldsToTruncate in options', () => {
assert.strictEqual(fakeLoggingOptions_, OPTIONS);
assert.strictEqual(
fakeLogOptions_.jsonFieldsToTruncate,
OPTIONS.jsonFieldsToTruncate
);
});

it('should localize Log instance using default name, removeCircular and maxEntrySize options', () => {
const optionsWithoutLogName: Options = Object.assign({}, OPTIONS);
delete optionsWithoutLogName.logName;
new loggingBunyanLib.LoggingBunyan(optionsWithoutLogName);
Expand All @@ -158,6 +171,7 @@ describe('logging-bunyan', () => {
assert.deepStrictEqual(fakeLogOptions_, {
removeCircular: true,
maxEntrySize: 250000,
jsonFieldsToTruncate: [TRUNCATE_FIELD],
});
});

Expand Down
Loading