UnhandledPromiseRejectionWarning:错误:大小为 903.1K 的日志条目超出最大大小 256.0K

问题描述 投票:0回答:2

我遇到以下错误(仅在过去 12 小时内出现两次):(节点:7)UnhandledPromiseRejectionWarning:错误:大小为 903.1K 的日志条目超出了最大大小 256.0K

我在 nodeJS 8.16.1 上使用 @google-cloud/logging-bunyan": "^1.2.3" 库来记录谷歌地图自动完成响应。
有时日志可能大于 256K,这是 Stackdriver 的最大配额。因此,它会导致以下问题:

(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)  

根据此链接,问题出在 Stackdriver 配额中:https://github.com/googleapis/nodejs-logging/issues/520

但是在访问文档后我发现日志大小的最大值是256K:https://cloud.google.com/logging/quotas

有办法解决这个问题吗?

logging google-cloud-platform grpc stackdriver bunyan
2个回答
0
投票

Stackdriver 日志条目大小构成硬性限制,已从 100KB 扩展到 256KB(请参阅公共问题跟踪器发行说明上的帖子)。

为了方便这种情况,我建议您排除不必要的日志,以尽量减少文件的大小。 (请参阅日志排除防止日志浪费)。


0
投票

我遇到了同样的问题,我解决了这个问题。 首先,我们需要了解为什么会遇到这个错误。

错误消息“INVALID_ARGUMENT:大小为 abc.dK 的日志条目超出了 xyz.0K 的最大大小”表示您尝试记录的条目超出了 Google Cloud Logging 中日志条目允许的最大大小。

在 Google Cloud Logging 中,每个日志条目都有大小限制,默认最大大小为 256 KB。如果您尝试记录超过此限制的条目,您将遇到您提到的“INVALID_ARGUMENT”错误。

解决方案:

根据您的具体日志记录设置和语言环境调整代码。关键是确保您的日志条目在允许的大小限制内。

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;
}
}

通过此链接了解更多详情

© www.soinside.com 2019 - 2024. All rights reserved.