Skip to content

Commit 508d024

Browse files
Benjamin E. Coecallmehiphop
authored andcommitted
fix: truncate additional fields set by winston/bunyan (#609)
1 parent 1329da1 commit 508d024

3 files changed

Lines changed: 52 additions & 16 deletions

File tree

handwritten/logging/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"@google-cloud/promisify": "^1.0.0",
5959
"@opencensus/propagation-stackdriver": "0.0.17",
6060
"arrify": "^2.0.0",
61+
"dot-prop": "^5.1.0",
6162
"eventid": "^0.1.2",
6263
"extend": "^3.0.2",
6364
"gcp-metadata": "^3.1.0",

handwritten/logging/src/log.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17+
import arrify = require('arrify');
1718
import {DeleteCallback} from '@google-cloud/common';
1819
import {callbackifyAll} from '@google-cloud/promisify';
19-
import arrify = require('arrify');
20+
import * as dotProp from 'dot-prop';
2021
import * as extend from 'extend';
2122
import {CallOptions} from 'google-gax';
2223
import {Response} from 'teeny-request';
@@ -886,27 +887,33 @@ class Log implements LogSeverityFunctions {
886887
const payloadSize = JSON.stringify(entry).length;
887888
if (payloadSize < this.maxEntrySize) return;
888889

889-
const delta = payloadSize - this.maxEntrySize;
890+
let delta = payloadSize - this.maxEntrySize;
890891
if (entry.textPayload) {
891892
entry.textPayload = entry.textPayload.slice(
892893
0,
893894
Math.max(entry.textPayload.length - delta, 0)
894895
);
895896
} else {
896-
// Stackdriver Log Viewer picks up the summary line from the
897-
// 'message' field.
898-
if (
899-
entry.jsonPayload &&
900-
entry.jsonPayload.fields &&
901-
entry.jsonPayload.fields.message &&
902-
entry.jsonPayload.fields.message.stringValue
903-
) {
904-
const text: string | null | undefined =
905-
entry.jsonPayload.fields.message.stringValue;
906-
entry.jsonPayload.fields.message.stringValue = text.slice(
907-
0,
908-
Math.max(text.length - delta, 0)
909-
);
897+
const fieldsToTruncate = [
898+
// Winston:
899+
'jsonPayload.fields.metadata.structValue.fields.stack.stringValue',
900+
// Bunyan:
901+
'jsonPayload.fields.msg.stringValue',
902+
'jsonPayload.fields.err.structValue.fields.stack.stringValue',
903+
'jsonPayload.fields.err.structValue.fields.message.stringValue',
904+
// All:
905+
'jsonPayload.fields.message.stringValue',
906+
];
907+
for (const field of fieldsToTruncate) {
908+
const msg: string = dotProp.get(entry, field, '');
909+
if (msg !== '') {
910+
dotProp.set(
911+
entry,
912+
field,
913+
msg.slice(0, Math.max(msg.length - delta, 0))
914+
);
915+
delta -= Math.min(msg.length, delta);
916+
}
910917
}
911918
}
912919
});

handwritten/logging/test/log.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,34 @@ describe('Log', () => {
499499

500500
await truncatingLogger.write(entry);
501501
});
502+
503+
it('should truncate stack trace', async () => {
504+
const truncatingLogger = createLogger(300);
505+
const entry = new Entry(
506+
{},
507+
{
508+
message: 'hello world'.padEnd(2000, '.'),
509+
metadata: {
510+
stack: 'hello world'.padEnd(2000, '.'),
511+
},
512+
}
513+
);
514+
515+
truncatingLogger.logging.loggingService.writeLogEntries = (
516+
reqOpts,
517+
_gaxOpts
518+
) => {
519+
const message =
520+
reqOpts.entries[0].jsonPayload.fields.message.stringValue;
521+
const stack = reqOpts.entries[0].jsonPayload.fields.metadata
522+
.structValue!.fields!.stack.stringValue;
523+
assert.strictEqual(stack, '');
524+
assert.ok(message.startsWith('hello world'));
525+
assert.ok(message.length < 400);
526+
};
527+
528+
await truncatingLogger.write(entry);
529+
});
502530
});
503531

504532
describe('severity shortcuts', () => {

0 commit comments

Comments
 (0)