Edit on GitHub
Report issue
Page history

Submitted by @{[ TutorialCtrl.tutorial.github_username ]} {[ TutorialCtrl.tutorial.date_published | date:'mediumDate' ]}

title: Ghost on App Engine Flexible Environment Part 2 - Monitoring description: Learn how to monitor a Ghost blog running on Google App Engine flexible environment. author: jmdobry tags: App Engine, Ghost, Node.js, Stackdriver date_published: 2016-05-26


This tutorial explains how to monitor a Ghost blog deployed on Google App Engine Flexible Environment.

Objectives

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

Before you begin

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

Understanding Stackdriver Monitoring

Monitoring is powered by Stackdriver Monitoring.

You don't have to install the monitoring agent on Google App Engine Flexible Environment because Stackdriver monitoring support is built-in.

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

Understanding Stackdriver Logging

Logging is powered by Stackdriver Logging.

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

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

Using Stackdriver Trace

Tracing of HTTP requests and RPC calls in your Ghost blog is powered by Stackdriver 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 Stackdriver 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 Stackdriver 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 Stackdriver 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 Stackdriver Debugger.

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

Enable 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 Stackdriver 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 Stackdriver Debugger when it is deployed to App Engine.

  4. Re-deploy the application:

    gcloud app deploy
    
  5. You can debug the application using the Stackdriver Debugger Dashboard.

Cleanup

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

See more by @{[ TutorialCtrl.tutorial.github_username ]} and more tagged {[ tag ]}{[ $last ? '' : ', ' ]}

Submit a Tutorial

Share step-by-step guides

SUBMIT A TUTORIAL

Request a Tutorial

Ask for community help

SUBMIT A REQUEST

GCP Tutorials

Tutorials published by GCP

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.