Edit on GitHub
Report issue
Page history

Monitoring Ghost on App Engine flexible environment - part 2

Author(s): @jmdobry ,   Published: 2016-05-26

Contributed by Google employees.

This tutorial explains how to monitor a Ghost blog deployed on App Engine flexible environment.

Objectives

  • Understand Cloud Monitoring with Ghost.
  • Understand Cloud Logging with Ghost.
  • Activate Cloud Trace.

Before you begin

Complete the tutorial Ghost on App Engine Part 1 - Deploying.

Understanding Cloud Monitoring

Monitoring is powered by Cloud Monitoring.

You don't have to install the monitoring agent on App Engine flexible environment because Cloud Monitoring support is built-in.

You can view the monitoring dashboard for your Ghost blog in the Cloud Console.

Understanding Cloud Logging

Logging is powered by Cloud Logging.

You don't have to install the logging agent on Google App Engine Flexible Environment because Cloud Logging support is built-in. See Cloud Logging in App Engine Apps for more information.

You can view the logs for your Ghost blog in the Cloud Console.

Using Cloud Trace

Tracing of HTTP requests and RPC calls in your Ghost blog is powered by Cloud Trace.

To begin tracing what goes on in your Ghost blog you must import the Node.js trace agent into the application.

Enable Trace

  1. To install the @google-cloud/trace-agent module during deployment, edit the package.json file and add a postinstall script:

    "scripts": {
      "preinstall": "...",
      "postinstall": "npm install @google-cloud/trace-agent",
      "start": "...",
      "test": "..."
    }
    

    We use a postinstall script because it allows us to avoid messing with Ghost's npm-shrinkwrap.json file.

  2. Create a trace.js file with the following contents:

    if (process.env.NODE_ENV === 'production') {
      require('@google-cloud/trace-agent').start({
        enhancedDatabaseReporting: true
      });
    }
    
  3. To start Cloud Trace when the deployed application starts, the @google-cloud/trace-agent module must be imported as the very first thing the application does. Add the following to the very first line of index.js:

    require('./trace');
    

    The application will now use Cloud Trace when it is deployed to App Engine.

  4. Re-deploy the application:

    gcloud app deploy
    
  5. After a few minutes, activity in your application causes traces to appear in the Trace Dashboard.

Error Reporting

Error reporting in your Ghost blog is powered by Cloud Error Reporting.

Error reporting works by capturing logs written to a certain location. We will use the winston library to write the logs to the right location.

Enable Error Reporting

  1. To install the winston module during deployment, edit the package.json file and add winston to the postinstall script you added earlier:

    "scripts": {
      "preinstall": "...",
      "postinstall": "npm install @google-cloud/trace-agent winston",
      "start": "...",
      "test": "..."
    }
    
  2. Create an errorreporting.js file with the following contents:

    var logFile = '/var/log/app_engine/custom_logs/ghost.errors.log.json';
    var winston = require('winston');
    
    winston.add(winston.transports.File, {
      filename: logFile
    });
    
    function report (err, req) {
      var payload = {
        serviceContext: {
          service: 'ghost'
        },
        message: err ? err.stack : '',
        context: {
          httpRequest: {
            url: req.originalUrl,
            method: req.method,
            referrer: req.header('Referer'),
            userAgent: req.header('User-Agent'),
            remoteIp: req.ip,
            responseStatusCode: 500
          }
        }
      };
      winston.error(payload);
    }
    
    function skip (req, res) {
      if (res.statusCode >= 400) {
        report(null, req);
      }
      return false
    }
    
    exports.logging = {
      skip: skip
    };
    
  3. To start collecting errors when the deployed application starts, the error reporting code needs to be added to the Express application. Add the following logging setting to config.json:

    production: {
      // Other settings hidden
    
      logging: require('./errorreporting').logging
    }
    
  4. Re-deploy the application:

    gcloud app deploy
    
  5. Any request errors will now be reported in the Error Reporting Dashboard.

Debugging

Debugging your Ghost blog is powered by Cloud Debugger.

To make Cloud Debugger available to your Ghost blog you must import the Node.js debugger agent into the application.

Enable Cloud Debugger

  1. To install the @google-cloud/debug-agent module during deployment, edit the package.json file and add @google-cloud/debug-agent to the postinstall script you added earlier:

    "scripts": {
      "preinstall": "...",
      "postinstall": "npm install @google/cloud-trace winston @google-cloud/debug-agent",
      "start": "...",
      "test": "..."
    }
    
  2. Create a debug.js file with the following contents:

    if (process.env.NODE_ENV === 'production') {
      require('@google-cloud/debug-agent').start();
    }
    
  3. To make Cloud Debugger available to the deployed application, the @google-cloud/debug-agent module must be imported as the second thing the application does (right after where Trace is imported). Add the following to the top of index.js after require('./trace');:

    require('./debug');
    

    The application will now be able to use Cloud Debugger when it is deployed to App Engine.

  4. Re-deploy the application:

    gcloud app deploy
    
  5. You can debug the application using the Cloud Debugger dashboard.

Cleanup

See the cleanup guide for the Bookshelf Node.js tutorial.

Submit a tutorial

Share step-by-step guides

Submit a tutorial

Request a tutorial

Ask for community help

Submit a request

View tutorials

Search Google Cloud tutorials

View tutorials

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.