0
votes

I have a NodeJS project hosted on GKE with trace agent and Stackdriver logging enabled. The project is logging to stdout using winston like this:

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

I can see traceId appear in Stackdriver and consistent across the logs within same trace. But they are all individual log entries instead of collapsed under the first entry.

I checked the request log has header x-cloud-trace-context: "a54d7110fc59c879b7ae67fb481fb89b/113593995793831;o=1" as well.

enter image description here

Also, I'm able to see in the tracing done properly trace list console. And when I deploy the same to GAE I can see logs associated and collapsed under the first entry. Any ideas?

1
You may want to take a look at this, there seems to already be a report of the same behaviour you are getting along with a solution in that thread - rsalinas
Thanks. I didn't use @google-cloud/logging-winston. Not really worth it for just adding traceId and http request formatting (unless I missed something.) Did it manually instead. The problem for me is that traceIds were attached but Stackdriver UI didn't fold them. - xiangxin
The two entries that you shared have different severity and that may have something to do with the reason that they are not being grouped together. I understand that this does not happen in App Engine, could you share an entry of the logs from there? - rsalinas

1 Answers

0
votes

Use logName app instead of stdout