Monitoring Ghost on App Engine flexible environment - part 2
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
To install the
@google-cloud/trace-agent
module during deployment, edit thepackage.json
file and add apostinstall
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'snpm-shrinkwrap.json
file.Create a
trace.js
file with the following contents:if (process.env.NODE_ENV === 'production') { require('@google-cloud/trace-agent').start({ enhancedDatabaseReporting: true }); }
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 ofindex.js
:require('./trace');
The application will now use Cloud Trace when it is deployed to App Engine.
Re-deploy the application:
gcloud app deploy
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
To install the
winston
module during deployment, edit thepackage.json
file and addwinston
to thepostinstall
script you added earlier:"scripts": { "preinstall": "...", "postinstall": "npm install @google-cloud/trace-agent winston", "start": "...", "test": "..." }
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 };
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 toconfig.json
:production: { // Other settings hidden logging: require('./errorreporting').logging }
Re-deploy the application:
gcloud app deploy
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
To install the
@google-cloud/debug-agent
module during deployment, edit thepackage.json
file and add@google-cloud/debug-agent
to thepostinstall
script you added earlier:"scripts": { "preinstall": "...", "postinstall": "npm install @google/cloud-trace winston @google-cloud/debug-agent", "start": "...", "test": "..." }
Create a
debug.js
file with the following contents:if (process.env.NODE_ENV === 'production') { require('@google-cloud/debug-agent').start(); }
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 ofindex.js
afterrequire('./trace');
:require('./debug');
The application will now be able to use Cloud Debugger when it is deployed to App Engine.
Re-deploy the application:
gcloud app deploy
You can debug the application using the Cloud Debugger dashboard.
Cleanup
See the cleanup guide for the Bookshelf Node.js tutorial.
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.