【问题标题】:nodejs writing protopayload in google-cloud/loggingnodejs 在 google-cloud/logging 中编写 protopayload
【发布时间】:2017-04-21 09:36:27
【问题描述】:

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

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

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

这是写日志的代码
//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。如果您看到的是日志,但没有看到有效负载,则问题很可能是由于您的编码器造成的。您可以尝试使用 google-protobuf 作为 'protocol-buffers' 的替代品。
  • @jordan 是的,我可以看到日志,但看不到有效负载。我将尝试使用 google-protobuf 作为替代方案。谢谢
  • 您能找到解决问题的方法吗?如果是这样,建议将其作为答案发布在这里,以更好地帮助社区。​​span>

标签: node.js logging google-cloud-logging


【解决方案1】:

不允许对 Logging API 中的 protoPayload 字段使用任意协议缓冲区。仅允许在该字段中使用具有众所周知的描述符的少数 Google Cloud Platform 服务。

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

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

【讨论】:

    【解决方案2】:

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

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

    还要确保您的“test.proto”遵循Google Protocol Buffer language guide。如果您看到的是日志,但没有看到有效负载,则问题很可能是由于您的编码器造成的。您可以尝试使用 google-protobuf 作为 'protocol-buffers' 的替代方案。

    【讨论】:

      猜你喜欢
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 2020-09-12
      • 1970-01-01
      • 2020-01-03
      相关资源
      最近更新 更多