From 4a5828f889e8825d44826da250b3fdea03abef1f Mon Sep 17 00:00:00 2001 From: cindy-peng Date: Wed, 6 Dec 2023 13:40:43 -0600 Subject: [PATCH 1/2] feat: enable custom json fields truncation. --- src/index.ts | 6 +++++- src/types/core.d.ts | 11 +++++++++-- test/index.ts | 13 ++++++++++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index d8e88314..46ef5e24 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'); @@ -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 = { diff --git a/src/types/core.d.ts b/src/types/core.d.ts index f79823ef..1b2dd2e3 100644 --- a/src/types/core.d.ts +++ b/src/types/core.d.ts @@ -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; diff --git a/test/index.ts b/test/index.ts index 005cb559..53b0fc77 100644 --- a/test/index.ts +++ b/test/index.ts @@ -26,6 +26,7 @@ interface Options { service: string; }; apiEndpoint: string; + jsonFieldsToTruncate: string[]; } interface FakeLogType { entry?: () => void; @@ -106,6 +107,9 @@ 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: {}, @@ -113,6 +117,7 @@ describe('logging-bunyan', () => { service: 'fake-service', }, apiEndpoint: 'fake.local', + jsonFieldsToTruncate: [TRUNCATE_FIELD], }; const RECORD = { @@ -148,8 +153,13 @@ describe('logging-bunyan', () => { assert.strictEqual(fakeLoggingOptions_, OPTIONS); assert.strictEqual(fakeLogName_, OPTIONS.logName); }); + + 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, options', () => { + 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); @@ -158,6 +168,7 @@ describe('logging-bunyan', () => { assert.deepStrictEqual(fakeLogOptions_, { removeCircular: true, maxEntrySize: 250000, + jsonFieldsToTruncate: [TRUNCATE_FIELD], }); }); From 3741fbd1cdd250ebaac0d00ee83ec831a2a31854 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Wed, 6 Dec 2023 19:46:23 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20po?= =?UTF-8?q?st-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- test/index.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/index.ts b/test/index.ts index 53b0fc77..6ebd30b0 100644 --- a/test/index.ts +++ b/test/index.ts @@ -153,10 +153,13 @@ describe('logging-bunyan', () => { assert.strictEqual(fakeLoggingOptions_, OPTIONS); assert.strictEqual(fakeLogName_, OPTIONS.logName); }); - + it('should localize Log instance using provided jsonFieldsToTruncate in options', () => { assert.strictEqual(fakeLoggingOptions_, OPTIONS); - assert.strictEqual(fakeLogOptions_.jsonFieldsToTruncate, OPTIONS.jsonFieldsToTruncate); + assert.strictEqual( + fakeLogOptions_.jsonFieldsToTruncate, + OPTIONS.jsonFieldsToTruncate + ); }); it('should localize Log instance using default name, removeCircular and maxEntrySize options', () => {