未关联具有相同跟踪ID的Stackdriver日志条目

时间:2019-12-17 04:04:31

标签: google-cloud-platform stackdriver winston google-cloud-stackdriver

我在GKE上托管了一个trace agent且已启用Stackdriver日志记录的NodeJS项目。该项目正在使用stdout记录到winston,如下所示:

const { createLogger, format, transports } = require('winston');
const { combine, json } = format;

const addTraceId = format(info => {
  const agent = global._google_trace_agent;
  if (agent) {
    const traceProjectId = agent.getWriterProjectId();
    const traceId = agent.getCurrentContextId();
    if (traceProjectId && traceId) {
      info['logging.googleapis.com/trace'] = `projects/${traceProjectId}/traces/${traceId}`;
    }
  }
});

createLogger({
  level: 'debug',
  transports: new transports.Console()
  format: combine(
    addTraceId(),
    json()
  );
});

我可以看到traceId出现在Stackdriver中,并且在同一跟踪中的所有日志中保持一致。但是它们都是单独的日志条目,而不是折叠在第一个条目下。

我检查了请求日志中是否也有标题x-cloud-trace-context: "a54d7110fc59c879b7ae67fb481fb89b/113593995793831;o=1"

enter image description here

此外,我还能在跟踪列表控制台中正确完成的跟踪中看到。 当我将其部署到GAE时,我可以在第一个条目下看到关联的日志并折叠起来。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

使用logName app代替stdout