nodejs 在 google-cloud/logging 中编写 protopayload

nodejs writing protopayload in google-cloud/logging

我们如何在 NodeJS 中使用 google-cloud/logging 包传递 protopayload 联合标签?

到目前为止,我可以在日志中写入 textPayload 和 jsonPayload。

尝试使用 google-protobuf 和 protobufjs 创建协议缓冲区,甚至尝试了协议缓冲区包,但是当我执行 log.entry 和 [=28 时,在日志中看不到有效负载=].

这是写日志的代码
//writelogs.js

function writeLogEntryAdvanced (logName, options, callback) {
  var logging = Logging();
  var log = logging.log(logName);
var protobuf = require('protocol-buffers');
var fs = require('file-system');
// pass a proto file as a buffer/string or pass a parsed protobuf-schema object
var messages = protobuf(fs.readFileSync('test.proto'));
var buf = messages.Test.encode({
  num: 42,
  payload: 'hello world'
});
console.log(buf);
var entry = log.entry({ resource: options.resource }, buf);
  // See https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/log?method=write
  log.write(entry, function (err, apiResponse) {
    if (err) {
      return callback(err);
    }
    console.log('Wrote entry to log: %s', logName);
    return callback(null, apiResponse);
});
}

//test.proto

message Test {
  required float num  = 1;
  required string payload = 2;
}

编写代码是从 github 的 nodejs-doc-samples 复制的,我自己对 protopayload 进行了一些更改。 示例日志写入仅针对 jsonPayload 和 textPayload。

为了完整起见,我将发布我的最后一条评论作为答案。

使用 Stackdriver Logs Viewer 时,确保您查看的是写入日志的正确 'logName' 和 'resource.type'。

还要确保您的 'test.proto' 遵循 Google Protocol Buffer language guide. If you are seeing the log, but not the payload, the issue is most likely due to your encoder. You can try using google-protobuf 作为 'protocol-buffers' 的替代方法。

不允许在日志记录 API 中对 protoPayload 字段使用任意协议缓冲区。只有少数 Google 具有众所周知的描述符的云平台服务才被允许在此字段中使用。

对于任意结构化日志记录,您应该使用 jsonPayload 字段。

有关这些字段的更多信息记录在:https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry