|
| 1 | +# Node.js module for Stackdriver Error Reporting |
| 2 | + |
| 3 | +[![NPM Version][npm-image]][npm-url] |
| 4 | +[![Known Vulnerabilities][snyk-image]][snyk-url] |
| 5 | + |
| 6 | +> **This is not an official Google product.** This module is experimental and may not be ready for use. |
| 7 | +> This module uses APIs that may be undocumented and are subject to change without notice. |
| 8 | +
|
| 9 | +This module provides Stackdriver Error Reporting support for Node.js applications. |
| 10 | +[Stackdriver Error Reporting](https://cloud.google.com/error-reporting/) is a feature of |
| 11 | +Google Cloud Platform that allows in-depth monitoring and viewing of errors reported by |
| 12 | +applications running in almost any environment. Here's an introductory video: |
| 13 | + |
| 14 | +[](https://www.youtube.com/watch?v=cVpWVD75Hs8) |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +1. Your application needs to use Node.js version 4.x or greater. |
| 19 | +1. You need a [Google Cloud project](https://console.cloud.google.com). Your application can run anywhere, but errors are reported to a particular project. |
| 20 | +1. [Enable the Stackdriver Error Reporting API](https://console.cloud.google.com/apis/api/clouderrorreporting.googleapis.com/overview) for your project. |
| 21 | +1. The module will only send errors when the `NODE_ENV` environment variable is |
| 22 | +set to `production` or the `ignoreEnvironmentCheck` property given in the |
| 23 | +runtime configuration object is set to `true`. |
| 24 | + |
| 25 | +## Quick Start |
| 26 | + |
| 27 | +1. **Install the module:** |
| 28 | + |
| 29 | + In your project, on the command line: |
| 30 | + |
| 31 | + ``` |
| 32 | + # Install through npm while saving to the local 'package.json' |
| 33 | + npm install --save @google-cloud/error-reporting |
| 34 | + ``` |
| 35 | +1. **Instrument your application:** |
| 36 | + |
| 37 | +```js |
| 38 | +// Require the library and initialize the error handler |
| 39 | +var errors = require('@google-cloud/error-reporting')({ |
| 40 | + serviceContext: {service: 'my-service'} // not needed on Google Cloud |
| 41 | +}); |
| 42 | + |
| 43 | +// Report an error to the Stackdriver Error Reporting API |
| 44 | +errors.report(new Error('Something broke!')); |
| 45 | +``` |
| 46 | + |
| 47 | +1. **View reported errors:** |
| 48 | + |
| 49 | + Open Stackdriver Error Reporting at https://console.cloud.google.com/errors to view the reported errors. |
| 50 | + |
| 51 | +## Running on Google Cloud Platform |
| 52 | + |
| 53 | +### Google App Engine Flexible environment |
| 54 | + |
| 55 | +If you are using [Google App Engine flexible environment](https://cloud.google.com/appengine/docs/flexible/), you do not have to do any additional configuration. |
| 56 | + |
| 57 | +### Google Compute Engine |
| 58 | + |
| 59 | +Your VM instances need to be created with the `https://www.googleapis.com/auth/cloud-platform` scope if created via the [gcloud](https://cloud.google.com/sdk) CLI or the Google Cloud Platform API, or by enabling at least one of the Stackdriver APIs if created through the browser-based console. |
| 60 | + |
| 61 | +If you already have VMs that were created without API access and do not wish to recreate it, you can follow the instructions for using a service account under [running elsewhere](#running-elsewhere). |
| 62 | + |
| 63 | +### Google Container Engine |
| 64 | + |
| 65 | +Container Engine nodes need to also be created with the `https://www.googleapis.com/auth/cloud-platform` scope, which is configurable during cluster creation. Alternatively, you can follow the instructions for using a service account under [running elsewhere](#running-elsewhere). It's recommended that you store the service account credentials as [Kubernetes Secret](http://kubernetes.io/v1.1/docs/user-guide/secrets.html). |
| 66 | + |
| 67 | +## Running Elsewhere |
| 68 | + |
| 69 | +If your application is running outside of Google Cloud Platform, such as locally, on-premise, or on another cloud provider, you can still use Stackdriver Errors. |
| 70 | + |
| 71 | +1. You will need to specify your project ID when starting the errors agent. |
| 72 | + |
| 73 | + GCLOUD_PROJECT=particular-future-12345 node myapp.js |
| 74 | + |
| 75 | +1. You need to provide service account credentials to your application. |
| 76 | + * The recommended way is via [Application Default Credentials][app-default-credentials]. |
| 77 | + 1. [Create a new JSON service account key][service-account]. |
| 78 | + 1. Copy the key somewhere your application can access it. Be sure not to expose the key publicly. |
| 79 | + 1. Set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` to the full path to the key. The trace agent will automatically look for this environment variable. |
| 80 | + * If you are running your application on a development machine or test environment where you are using the [`gcloud` command line tools][gcloud-sdk], and are logged using `gcloud beta auth application-default login`, you already have sufficient credentials, and a service account key is not required. |
| 81 | + * Alternatively, you may set the `keyFilename` or `credentials` configuration field to the full path or contents to the key file, respectively. Setting either of these fields will override either setting `GOOGLE_APPLICATION_CREDENTIALS` or logging in using `gcloud`. For example: |
| 82 | + |
| 83 | +```js |
| 84 | + // Require and start the agent with configuration options |
| 85 | + var errors = require('@google-cloud/error-reporting')({ |
| 86 | + // The path to your key file: |
| 87 | + keyFilename: '/path/to/keyfile.json', |
| 88 | + |
| 89 | + // Or the contents of the key file: |
| 90 | + credentials: require('./path/to/keyfile.json') |
| 91 | + }); |
| 92 | +``` |
| 93 | + |
| 94 | +When running on Google Cloud Platform, we handle these for you automatically. |
| 95 | + |
| 96 | +## Configuration |
| 97 | + |
| 98 | +The following code snippet lists all available configuration options. All configuration options are optional. |
| 99 | + |
| 100 | +```js |
| 101 | +var errors = require('@google-cloud/error-reporting')({ |
| 102 | + projectId: 'my-project-id', |
| 103 | + keyFilename: '/path/to/keyfile.json', |
| 104 | + credentials: require('./path/to/keyfile.json'), |
| 105 | + // if true library will attempt to report errors to the service regardless |
| 106 | + // of the value of NODE_ENV |
| 107 | + // defaults to false |
| 108 | + ignoreEnvironmentCheck: false, |
| 109 | + // determines if the library will attempt to report uncaught exceptions |
| 110 | + // defaults to true |
| 111 | + reportUncaughtExceptions: true, |
| 112 | + // determines the logging level internal to the library; levels range 0-5 |
| 113 | + // defaults to 2 (warnings) |
| 114 | + logLevel: 2, |
| 115 | + serviceContext: { |
| 116 | + service: 'my-service', |
| 117 | + version: 'my-service-version' |
| 118 | + } |
| 119 | +}); |
| 120 | +``` |
| 121 | + |
| 122 | +## Examples |
| 123 | + |
| 124 | +### Reporting Manually |
| 125 | + |
| 126 | +```js |
| 127 | +var errors = require('@google-cloud/error-reporting')(); |
| 128 | +// Use the error message builder to custom set all message fields |
| 129 | +var errorEvt = errors.event() |
| 130 | + .setMessage('My error message') |
| 131 | + .setUser('root@nexus'); |
| 132 | +errors.report(errorEvt, () => console.log('done!')); |
| 133 | +// Or just use a regular error |
| 134 | +errors.report(new Error('My error message'), () => console.log('done!')); |
| 135 | +// One can even just use a string |
| 136 | +errors.report('My error message'); |
| 137 | +``` |
| 138 | + |
| 139 | +### Using Express |
| 140 | + |
| 141 | +```js |
| 142 | +var express = require('express'); |
| 143 | +var app = express(); |
| 144 | +// Will create an errors instance based off env variables |
| 145 | +var errors = require('@google-cloud/error-reporting')(); |
| 146 | + |
| 147 | +app.get('/error', (req, res, next) => { |
| 148 | + res.send('Something broke!'); |
| 149 | + next(new Error('Custom error message')); |
| 150 | +}); |
| 151 | + |
| 152 | +app.get('/exception', () => { |
| 153 | + JSON.parse('{\"malformedJson\": true'); |
| 154 | +}); |
| 155 | + |
| 156 | +app.use(errors.express); |
| 157 | + |
| 158 | +app.listen(3000); |
| 159 | +``` |
| 160 | + |
| 161 | +### Using Hapi |
| 162 | + |
| 163 | +```js |
| 164 | +var hapi = require('hapi'); |
| 165 | +var errors = require('@google-cloud/error-reporting')(); |
| 166 | + |
| 167 | +var server = new hapi.Server(); |
| 168 | +server.connection({ port: 3000 }); |
| 169 | +server.start(); |
| 170 | + |
| 171 | +server.route({ |
| 172 | + method: 'GET', |
| 173 | + path: '/error', |
| 174 | + handler: (request, reply) => { |
| 175 | + reply('Something broke!'); |
| 176 | + throw new Error('Custom error message'); |
| 177 | + } |
| 178 | +}); |
| 179 | + |
| 180 | +server.register({ register: errors.hapi }); |
| 181 | +``` |
| 182 | + |
| 183 | +### Using Koa |
| 184 | + |
| 185 | +```js |
| 186 | +var errors = require('@google-cloud/error-reporting')(); |
| 187 | +var koa = require('koa'); |
| 188 | +var app = koa(); |
| 189 | + |
| 190 | +app.use(errors.koa); |
| 191 | + |
| 192 | +app.use(function *(next) { |
| 193 | + //This will set status and message |
| 194 | + this.throw('Error Message', 500); |
| 195 | +}); |
| 196 | + |
| 197 | +// response |
| 198 | +app.use(function *(){ |
| 199 | + this.body = 'Hello World'; |
| 200 | +}); |
| 201 | + |
| 202 | +app.listen(3000); |
| 203 | +``` |
| 204 | + |
| 205 | +### Using Restify |
| 206 | + |
| 207 | +```js |
| 208 | +function respond(req, res, next) { |
| 209 | + next(new Error('this is a restify error')); |
| 210 | +} |
| 211 | + |
| 212 | +var restify = require('restify'); |
| 213 | +var errors = require('@google-cloud/error-reporting')(); |
| 214 | + |
| 215 | +var server = restify.createServer(); |
| 216 | + |
| 217 | +server.use(errors.restify(server)); |
| 218 | +server.get('/hello/:name', respond); |
| 219 | +server.head('/hello/:name', respond); |
| 220 | + |
| 221 | +server.listen(8080); |
| 222 | +``` |
| 223 | + |
| 224 | +[gcloud-sdk]: https://cloud.google.com/sdk/gcloud/ |
| 225 | +[app-default-credentials]: https://developers.google.com/identity/protocols/application-default-credentials |
| 226 | +[service-account]: https://console.developers.google.com/apis/credentials/serviceaccountkey |
| 227 | +[npm-image]: https://badge.fury.io/js/%40google-cloud%2Ferror-reporting.svg |
| 228 | +[npm-url]: https://npmjs.org/package/@google-cloud/error-reporting |
| 229 | +[snyk-image]: https://snyk.io/test/npm/@google-cloud/error-reporting/badge.svg |
| 230 | +[snyk-url]: https://snyk.io/test/npm/@google-cloud/error-reporting |
0 commit comments