Skip to content

Commit

Permalink
feat: enable custom json fields truncation for bunyan (#732)
Browse files Browse the repository at this point in the history
* feat: enable custom json fields truncation.

* 馃 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: cindy-peng <cindypeng@google.com>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Dec 8, 2023
1 parent 8baf645 commit eb6fa66
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/index.ts
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
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
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

0 comments on commit eb6fa66

Please sign in to comment.