UnhandledPromiseRejectionWarning: Error: Log entry with size 903.1K exceeds maximum size of 256.0K

1.3k views Asked by At

I am having the following error (appeared twice only in last 12hours): (node:7) UnhandledPromiseRejectionWarning: Error: Log entry with size 903.1K exceeds maximum size of 256.0K

I am using the @google-cloud/logging-bunyan": "^1.2.3" library on nodeJS 8.16.1 to log the google map autocomplete response.
Sometimes the logs can be bigger than 256K which is the max quota for Stackdriver. So, it causes the following problem :

(node:7) UnhandledPromiseRejectionWarning: Error: Log entry with size 903.1K exceeds maximum size of 256.0K
    at Http2CallStream.call.on (/usr/src/app/node_modules/@grpc/grpc-js/build/src/client.js:96:45)
    at Http2CallStream.emit (events.js:203:15)
    at process.nextTick (/usr/src/app/node_modules/@grpc/grpc-js/build/src/call-stream.js:75:22)
    at process._tickCallback (internal/process/next_tick.js:61:11)  

according to this link, the problem is in the Stackdriver quota : https://github.com/googleapis/nodejs-logging/issues/520

But after visiting the documentation i have seen that the max value of log size is 256K : https://cloud.google.com/logging/quotas

Is there a way to resolve this problem?

2

There are 2 answers

0
Artemis Georgakopoulou On

The Stackdriver Log Entry Size constitutes a hard limit, which has already been extended from 100KB to 256KB (See posts on Public Issue Tracker and Release Notes).

To facilitate the situation, I recommend you to exclude unnecessary logs in order to minimize the size of the file. (See Log Exclusions and Preventing Log Waste).

0
Shubham Verma On

I was facing the same issue and I resolved this. First, we need to understand why we are facing this error.

The error message "INVALID_ARGUMENT: Log entry with size abc.dK exceeds the maximum size of xyz.0K" indicates that you are trying to log an entry that exceeds the maximum allowed size for a log entry in Google Cloud Logging.

In Google Cloud Logging, each log entry has a size limit, and the default maximum size is 256 KB. If you attempt to log an entry that surpasses this limit, you will encounter the "INVALID_ARGUMENT" error you mentioned.

Solution:

Adjust the code according to your specific logging setup and language environment. The key is to ensure that your log entries are within the allowed size limits.

class CloudLoggerDTO {
  service?: string;
  userId?: string;
  hostname?: string;
  correlationId?: string;
  requestId?: string;
  timestamp?: string;
  methodName?: string;
  callStack?: string;
  requestMethod?: string;
  requestURI?: string;
}

@Injectable()
export class CloudLoggerService {
  private logging: Logging;
  private severityLog = 'NOTICE';
  private severityError = 'ERROR';
  private severityWarn = 'WARNING';
  private resource: any = { type: 'global' };
  private serviceName: string;
  private logName: string;
  private env: string;
  private projectId: string;
  private logSize: number;

  constructor() {
    this.projectId = 'PROJECT_ID';
    this.logging = new Logging({ projectId: this.projectId });
    this.logName = 'LOG NAME';
    this.logSize = 256;
  }

  log(message: string, other: CloudLoggerDTO = {}): void {
    try {
      const messageInfo = this.getMessageInfo(message, other);
      const metadataLog = this.getMetaData(this.severityLog);
      const log = this.logging.log(this.logName, this.resource);
      const entry = this.logging.entry(metadataLog, messageInfo);
      log.write(entry);
    } catch (err) {
      console.log('Exception in log()', err);
    }
  }

  error(message: string, other: CloudLoggerDTO = {}): any {
    try {
      const messageInfo = this.getMessageInfo(message, other);
      const metadataLog = this.getMetaData(this.severityError);
      const log = this.logging.log(this.logName, this.resource);
      const entry = this.logging.entry(metadataLog, messageInfo);
      log.write(entry);
      return;
    } catch (err) {
      console.log('Exception in info()', err);
    }
  }


  getMessageInfo(message: string, other: CloudLoggerDTO) {
    return {
      message,
      service: this.serviceName,
      env: this.env,
      logName: this.logName,
      projectId: this.projectId,
      timeStamp: new Date(),
      ...this.truncateObject(other),
    };
  }

  getMetaData(severity) {
    return {
      severity,
      resource: this.resource,
    };
  }

  /* This logic will remove the error: INVALID_ARGUMENT: Log entry with size 789.3K exceeds maximum size of 256.0K */

  truncateObject(obj) {
  if (obj && obj.error) {
    obj.error = this.stringifyCircularJSON(obj.error).substring(0, (Number(this.logSize) - 56) * 1024);
  }
  if (obj && obj.request) {
    obj.request = this.stringifyCircularJSON(obj.request).substring(0, 30 * 1024);
  }
  if (obj && obj.callStack) {
    obj.callStack = this.stringifyCircularJSON(obj.callStack).substring(0, 26 * 1024);
  }
  return obj;
}

stringifyCircularJSON(obj: any) {
  const seen = new Set();
  const jsonString = JSON.stringify(obj, (key, value) => { if (typeof value === 'object' && value !== null) { if (seen.has(value)) { return '[Circular Reference]'; } seen.add(value); } return value; });
  return jsonString;
}
}

Go through this link for more details