Skip to content

Commit eb6fa66

Browse files
cindy-pengcindy-penggcf-owl-bot[bot]
authored
feat: enable custom json fields truncation for bunyan (#732)
* 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 <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 8baf645 commit eb6fa66

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ export function getCurrentTraceFromAgent() {
150150
* attempted before returning the error.
151151
* @param {constructor} [options.promise] Custom promise module to use instead
152152
* of native Promises.
153-
*
153+
* @param {constructor} [options.promise] Custom promise module to use instead
154+
* of native Promises.
155+
* @param {number} [options.maxEntrySize] Max size limit of a log entry.
156+
* @param {string[]} [options.jsonFieldsToTruncate] A list of JSON properties at the given full path to be truncated.
154157
* @example Import the client library
155158
* ```
156159
* const {LoggingBunyan} = require('@google-cloud/logging-bunyan');
@@ -199,6 +202,7 @@ export class LoggingBunyan extends Writable {
199202
// 250,000 has been chosen to keep us comfortably within the
200203
// 256,000 limit.
201204
maxEntrySize: options.maxEntrySize || 250000,
205+
jsonFieldsToTruncate: options.jsonFieldsToTruncate,
202206
});
203207
} else {
204208
const logSyncOptions: LogSyncOptions = {

src/types/core.d.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,16 @@ export interface Options {
7878
* Defaults to `logging.googleapis.com`.
7979
*/
8080
apiEndpoint?: string;
81-
// An attempt will be made to truncate messages larger than maxEntrySize.
82-
// Please note that this parameter is ignored when redirectToStdout is set.
81+
/**
82+
* An attempt will be made to truncate messages larger than maxEntrySize.
83+
* Please note that this parameter is ignored when redirectToStdout is set.
84+
*/
8385
maxEntrySize?: number;
86+
/**
87+
* A list of JSON properties at the given full path to be truncated.
88+
* Received values will be prepended to predefined list in the order received and duplicates discarded.
89+
*/
90+
jsonFieldsToTruncate?: string[];
8491
// A default global callback to be used for {@link LoggingBunyan} write calls
8592
// when callback is not supplied by caller in function parameters
8693
defaultCallback?: ApiResponseCallback;

test/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ interface Options {
2626
service: string;
2727
};
2828
apiEndpoint: string;
29+
jsonFieldsToTruncate: string[];
2930
}
3031
interface FakeLogType {
3132
entry?: () => void;
@@ -106,13 +107,17 @@ describe('logging-bunyan', () => {
106107
// eslint-disable-next-line @typescript-eslint/no-explicit-any
107108
let loggingBunyan: any;
108109

110+
const TRUNCATE_FIELD =
111+
'jsonPayload.fields.metadata.structValue.fields.custom.stringValue';
112+
109113
const OPTIONS = {
110114
logName: 'log-name',
111115
resource: {},
112116
serviceContext: {
113117
service: 'fake-service',
114118
},
115119
apiEndpoint: 'fake.local',
120+
jsonFieldsToTruncate: [TRUNCATE_FIELD],
116121
};
117122

118123
const RECORD = {
@@ -149,7 +154,15 @@ describe('logging-bunyan', () => {
149154
assert.strictEqual(fakeLogName_, OPTIONS.logName);
150155
});
151156

152-
it('should localize Log instance using default name, options', () => {
157+
it('should localize Log instance using provided jsonFieldsToTruncate in options', () => {
158+
assert.strictEqual(fakeLoggingOptions_, OPTIONS);
159+
assert.strictEqual(
160+
fakeLogOptions_.jsonFieldsToTruncate,
161+
OPTIONS.jsonFieldsToTruncate
162+
);
163+
});
164+
165+
it('should localize Log instance using default name, removeCircular and maxEntrySize options', () => {
153166
const optionsWithoutLogName: Options = Object.assign({}, OPTIONS);
154167
delete optionsWithoutLogName.logName;
155168
new loggingBunyanLib.LoggingBunyan(optionsWithoutLogName);
@@ -158,6 +171,7 @@ describe('logging-bunyan', () => {
158171
assert.deepStrictEqual(fakeLogOptions_, {
159172
removeCircular: true,
160173
maxEntrySize: 250000,
174+
jsonFieldsToTruncate: [TRUNCATE_FIELD],
161175
});
162176
});
163177

0 commit comments

Comments
 (0)