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: introduce maxEntrySize, for enabling error message truncation #607

Merged
merged 5 commits into from
Oct 16, 2019
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
50 changes: 48 additions & 2 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {Response} from 'teeny-request';
import {google} from '../proto/logging';

import {GetEntriesCallback, GetEntriesResponse, Logging} from '.';
import {Entry, LogEntry} from './entry';
import {Entry, EntryJson, LogEntry} from './entry';
import {getDefaultResource} from './metadata';

const snakeCaseKeys = require('snakecase-keys');
Expand All @@ -44,6 +44,7 @@ export interface GetEntriesRequest {

export interface LogOptions {
removeCircular?: boolean;
maxEntrySize?: number; // see: https://cloud.google.com/logging/quotas
}

export type ApiResponse = [Response];
Expand Down Expand Up @@ -103,12 +104,14 @@ type LogSeverityFunctions = {
class Log implements LogSeverityFunctions {
formattedName_: string;
removeCircular_: boolean;
maxEntrySize?: number;
logging: Logging;
name: string;
constructor(logging: Logging, name: string, options?: LogOptions) {
options = options || {};
this.formattedName_ = Log.formatName_(logging.projectId, name);
this.removeCircular_ = options.removeCircular === true;
this.maxEntrySize = options.maxEntrySize;
this.logging = logging;
/**
* @name Log#name
Expand Down Expand Up @@ -828,6 +831,7 @@ class Log implements LogSeverityFunctions {
} catch (err) {
// Ignore errors (the API will speak up if it has an issue).
}
self.truncateEntries(decoratedEntries);
const projectId = await self.logging.auth.getProjectId();
self.formattedName_ = Log.formatName_(projectId, self.name);
const reqOpts = extend(
Expand Down Expand Up @@ -855,7 +859,7 @@ class Log implements LogSeverityFunctions {
* @returns {object[]} Serialized entries.
* @throws if there is an error during serialization.
*/
decorateEntries_(entries: Entry[]) {
decorateEntries_(entries: Entry[]): EntryJson[] {
return entries.map(entry => {
if (!(entry instanceof Entry)) {
entry = this.entry(entry);
Expand All @@ -866,6 +870,48 @@ class Log implements LogSeverityFunctions {
});
}

/**
* Truncate log entries at maxEntrySize, so that error is not thrown, see:
* https://cloud.google.com/logging/quotas
*
* @private
*
* @param {object|string} the JSON log entry.
* @returns {object|string} truncated JSON log entry.
*/
private truncateEntries(entries: EntryJson[]) {
return entries.forEach(entry => {
bcoe marked this conversation as resolved.
Show resolved Hide resolved
if (this.maxEntrySize === undefined) return;

const payloadSize = JSON.stringify(entry).length;
if (payloadSize < this.maxEntrySize) return;

const delta = payloadSize - this.maxEntrySize;
if (entry.textPayload) {
entry.textPayload = entry.textPayload.slice(
0,
Math.max(entry.textPayload.length - delta, 0)
);
} else {
// Stackdriver Log Viewer picks up the summary line from the
// 'message' field.
if (
entry.jsonPayload &&
entry.jsonPayload.fields &&
entry.jsonPayload.fields.message &&
entry.jsonPayload.fields.message.stringValue
) {
const text: string | null | undefined =
entry.jsonPayload.fields.message.stringValue;
entry.jsonPayload.fields.message.stringValue = text.slice(
0,
Math.max(text.length - delta, 0)
);
}
}
});
}

/**
* Return an array of log entries with the desired severity assigned.
*
Expand Down
62 changes: 60 additions & 2 deletions test/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const fakeCallbackify = extend({}, callbackify, {

import {Entry} from '../src';
import {EntryJson} from '../src/entry';
import {LogOptions} from '../src/log';

const originalGetDefaultResource = async () => {
return 'very-fake-resource';
Expand Down Expand Up @@ -76,6 +77,10 @@ describe('Log', () => {
});

beforeEach(() => {
log = createLogger();
});

function createLogger(maxEntrySize?: number) {
assignSeverityToEntriesOverride = null;

LOGGING = {
Expand All @@ -86,8 +91,13 @@ describe('Log', () => {
auth: util.noop,
};

log = new Log(LOGGING, LOG_NAME);
});
const options: LogOptions = {};
if (maxEntrySize) {
options.maxEntrySize = maxEntrySize;
}

return new Log(LOGGING, LOG_NAME, options);
}

describe('instantiation', () => {
it('should callbackify all the things', () => {
Expand Down Expand Up @@ -441,6 +451,54 @@ describe('Log', () => {

await log.write(ENTRY);
});

it('should not truncate entries by default', async () => {
const logger = createLogger();
const entry = new Entry({}, 'hello world'.padEnd(300000, '.'));

logger.logging.loggingService.writeLogEntries = (reqOpts, _gaxOpts) => {
assert.strictEqual(reqOpts.entries[0].textPayload.length, 300000);
};

await logger.write(entry);
});

it('should truncate string entry if maxEntrySize hit', async () => {
const truncatingLogger = createLogger(200);
const entry = new Entry({}, 'hello world'.padEnd(2000, '.'));

truncatingLogger.logging.loggingService.writeLogEntries = (
reqOpts,
_gaxOpts
) => {
const text = reqOpts.entries[0].textPayload;
assert.ok(text.startsWith('hello world'));
assert.ok(text.length < 300);
};

await truncatingLogger.write(entry);
});

it('should truncate message field, on object entry, if maxEntrySize hit', async () => {
const truncatingLogger = createLogger(200);
const entry = new Entry(
{},
{
message: 'hello world'.padEnd(2000, '.'),
}
);

truncatingLogger.logging.loggingService.writeLogEntries = (
reqOpts,
_gaxOpts
) => {
const text = reqOpts.entries[0].jsonPayload.fields.message.stringValue;
assert.ok(text.startsWith('hello world'));
assert.ok(text.length < 300);
};

await truncatingLogger.write(entry);
});
});

describe('severity shortcuts', () => {
Expand Down