Node.js - Google Cloud Stackdriver Error Reporting

Errors usually happen on applications. Developers need to keep track the errors in order to find the cause and fix them if needed. Printing the errors is what usually we done. But imagine if we have a large applications with various kinds of errors. It's not an easy thing to read the error one by one and we may also skip some errors.

That's why we need error reporting system that can collect errors and display them in a more organized way, so that we can read the errors easily. One of available error reporting services is Google Cloud Error Reporting, which is a part of their Stackdriver service. Basically, it collects and displays all the errors sent by application. This tutorial is about how to use Google Cloud Error Reporting service for your Node.js application.

Preparation

1. Create or select a Google Cloud project

A Google Cloud project is required to use this service. Open Google Cloud console, then create a new project or select existing project

2. Enable billing for the project

Like other cloud platforms, Google requires you to enable billing for your project. If you haven't set up billing, open billing page.

3. Enable Cloud Error Reporting API

To use an API, you must enable it first. Open this page to enable Cloud Error Reporting API.

4. Set up service account for authentication

As for authentication, you need to create a new service account. Create a new one on the service account management page and download the credentials, or you can use your already created service account.

In your .env file, you have to add a new variable

GOOGLE_APPLICATION_CREDENTIALS=/path/to/the/credentials

Dependencies

This tutorial uses @google-cloud/error-reporting and also dotenv for loading environment. Add the following dependencies to your package.json and run npm install

  "@google-cloud/error-reporting": "~0.5.1"
  "dotenv": "~4.0.0"

Code Examples

Let's start to use error-reporting in Node.js application. As we use @google-cloud/error-reporting, first we create an instane of ErrorReporting.

  require('dotenv').config();
  
  const { ErrorReporting } = require('@google-cloud/error-reporting');
  
  // Create an instance of ErrorReporting
  const errors = new ErrorReporting({
    ignoreEnvironmentCheck: true,
  });

The ignoreEnvironmentCheck option above is set to true. If false or not set, it will only communiate with the Google service if NODE_ENV equals production. Below is the list of supported configs you can pass to the constructor.

Name Type Description
projectId String The Google Cloud project ID.
keyFilename String Path to the file containing secret key.
lovLevel {String|Number} The allowed values are integer including and between 0-5 (also supports in the String form)
key String API key for communication with the service.
serviceContext
{
  service: String,
  version: String,
}
  • service: The service the application is running on
  • version: The application version
ignoreEnvironmentCheck Boolean If false (default), it will only connect to Stackdrvier if NODE_ENV equals production

In case an error occurs, we can send it to Google either as an event or a conventional Error object.

  // Report an error event
  const errorEvent = errors.event();
  errorEvent.setMessage('My error message');
  errorEvent.setUser('user1');
  errors.report(errorEvent);
  
  // Report an Error object
  errors.report(new Error('My error message'));

In real cases, we usually have a catch block in order to handle if any error happens and to prevent the app crashes. Therefore, we can add the code for sending error to the Google service inside the catch block. Here is the example.

  const errorFunction = () => {
    throw new Error('This is an error');
  };

  try {
    errorFunction();
  } catch (error) {
    errors.report(error);
  }

And if we use Promise, we can do it as well.

  const errorFunctionPromise = () => Promise.reject(new Error('This is a Promise error'));
  
  errorFunctionPromise()
    .catch(errors.report);

View the errors on Google Cloud Error Reporting Console

The errors have been sent can be viewed on the Error Reporting Console. There, you can view the list of errors. The same errors are grouped together and counted how many times it occurs withtin a specified time range. Currently, you can choose to show errors in the last 1 hours, 6 hours, 1 day, 7 days or 30 days. There are four resolution status for an error: Open, Acknowledged, Resolved and Muted. The default status is Open. For every sent errors, you can change the status via Error Reporting console. That's make us easy to filter the errors to distinguishes which errors are already known, already resolved and not important. You can also filter by service name and version.

Google Cloud Stackdriver Error Reporting Console

To get the details of an error, just click on it. It will open a new page showing the graph on what times that error occurs. You can also view every stack trace.

Google Cloud Stackdriver Error Reporting - Error Details

That's all about how to integrate Google Stackdriver Error Reporting with your Node.js application. To compliment this service, you can also use Stackdriver Logging, which is a log management system, and Stackdriver Trace for monitoring latency. As for online debugging, Google also has Stackdriver Debugger service.