Search code examples
node.jsgoogle-app-enginegoogle-appengine-node

App Engine Node.js: how to link app logs and requests logs


I am using Node.js on App Engine Standard and Flexible.

In the logs viewer, is it possible to display application logs nested inside request logs?


Solution

  • Yes it is possible to correlate application logs and request logs. This is the end result in the Logs Viewer:

    enter image description here

    To achieve this you can either:

    Use both the @google-cloud/trace-agent and @google-cloud/logging-bunyan modules in your application. When you do so, your logs are automatically annotated with the correct Trace ID (see docs for Bunyan).

    Extract the trace ID from the request header If you do not want to use the Trace module, you can extract the trace ID from the request header, use the following code to extract the traceId:

    const traceHeader = req && req.headers ? req.headers['x-cloud-trace-context'] || '' : '';
    const traceId = traceHeader ? traceHeader.split('/')[0] : '';
    

    Then, you need to populate the trace attribute of your log entries. When using the Bunyan logger, use the following code:

    logger.info({
      'logging.googleapis.com/trace': `projects/${project}/traces/${traceId}`
    }, 'your message');