Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1e7b3b0
Stage errors for repo integration
cristiancavalli Feb 23, 2017
33255b1
bring syntax into repo linting pattern
cristiancavalli Feb 28, 2017
7567bb8
Remove koa injection switch since 0.12 is no longer officially supported
cristiancavalli Feb 28, 2017
dc81d2c
start jscs linting
cristiancavalli Mar 1, 2017
d12de53
remove all trailing spaces
cristiancavalli Mar 1, 2017
d437090
Replace occurences of non-compliant function declaration syntax
cristiancavalli Mar 1, 2017
3c0efb4
remove spacing in function parens
cristiancavalli Mar 1, 2017
2c692ce
remove all leading colon spaces
cristiancavalli Mar 1, 2017
7122245
remove function paren spacing
cristiancavalli Mar 1, 2017
86569c5
both linters be linting
cristiancavalli Mar 1, 2017
abb9b35
use new common.Service class. Remove testConfigCredentials file since…
cristiancavalli Mar 1, 2017
e674ef8
remove debug log calls
cristiancavalli Mar 1, 2017
19d89af
fix linting errors
cristiancavalli Mar 1, 2017
b664f6d
remove dependency on cloud-diagnostics-common
cristiancavalli Mar 1, 2017
93a940c
fix ternary operator spacing
cristiancavalli Mar 10, 2017
bf4e6b0
remove comment from jshintrc
cristiancavalli Mar 10, 2017
e2f0ed9
remove 0.12 node runtime from travis.yml
cristiancavalli Mar 10, 2017
4785e7a
fix ternary statement in fuzzer
cristiancavalli Mar 13, 2017
9ca6f90
rename module; README tweaks
ofrobots Mar 20, 2017
217c3a5
start addressing pr feedback (round 2)
cristiancavalli Mar 24, 2017
b27025f
address pr feedback
cristiancavalli Mar 30, 2017
245563f
pr feedback, fix issue with system tests
cristiancavalli Mar 30, 2017
fbf933d
fix spacing on README
cristiancavalli Mar 30, 2017
58af011
address documentation feedback, move index into ./src/
cristiancavalli Apr 4, 2017
ace6a15
fix lint
cristiancavalli Apr 4, 2017
f3f6134
change function syntax in examples because jshint is complianing
cristiancavalli Apr 5, 2017
a403a34
fix snippet test
cristiancavalli Apr 5, 2017
4485f5e
remove errors requires from example comments
cristiancavalli Apr 6, 2017
475eedb
fix snippet tests; add dependencies for errors snippet tests.
cristiancavalli Apr 6, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/error-reporting/.clang-format.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BasedOnStyle: Google
Language: Javascript
9 changes: 9 additions & 0 deletions packages/error-reporting/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules
coverage
npm-debug.log
.DS_Store
.eslintrc.js
docs
tests/configuration
.nyc_output
*.patch
4 changes: 4 additions & 0 deletions packages/error-reporting/.jshintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
test/e2e/node_modules
test/fixtures
coverage
27 changes: 27 additions & 0 deletions packages/error-reporting/.jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"bitwise": true,
"curly": true,
"eqeqeq": true,
"esnext": true,
"freeze": true,
"immed": true,
"indent": 2,
"latedef": "nofunc",
"maxlen": 100,
"newcap": true,
"node": true,
"noarg": true,
"quotmark": "single",
"strict": true,
"trailing": true,
"undef": true,
"unused": "vars",
"globals": {
"describe": false,
"it": false,
"before": false,
"beforeEach": false,
"after": false,
"afterEach": false
}
}
230 changes: 230 additions & 0 deletions packages/error-reporting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
# Node.js module for Stackdriver Error Reporting

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


[![NPM Version][npm-image]][npm-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]

> **This is not an official Google product.** This module is experimental and may not be ready for use.
> This module uses APIs that may be undocumented and are subject to change without notice.

This module provides Stackdriver Error Reporting support for Node.js applications.
[Stackdriver Error Reporting](https://cloud.google.com/error-reporting/) is a feature of
Google Cloud Platform that allows in-depth monitoring and viewing of errors reported by
applications running in almost any environment. Here's an introductory video:

[![Learn about Error Reporting in Stackdriver](https://img.youtube.com/vi/cVpWVD75Hs8/0.jpg)](https://www.youtube.com/watch?v=cVpWVD75Hs8)

## Prerequisites

1. Your application needs to use Node.js version 4.x or greater.
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.
1. [Enable the Stackdriver Error Reporting API](https://console.cloud.google.com/apis/api/clouderrorreporting.googleapis.com/overview) for your project.
1. The module will only send errors when the `NODE_ENV` environment variable is
set to `production` or the `ignoreEnvironmentCheck` property given in the
runtime configuration object is set to `true`.

## Quick Start

1. **Install the module:**

In your project, on the command line:

```
# Install through npm while saving to the local 'package.json'
npm install --save @google-cloud/error-reporting
```
1. **Instrument your application:**

```js
// Require the library and initialize the error handler
var errors = require('@google-cloud/error-reporting')({
serviceContext: {service: 'my-service'} // not needed on Google Cloud
});

// Report an error to the Stackdriver Error Reporting API
errors.report(new Error('Something broke!'));
```

1. **View reported errors:**

Open Stackdriver Error Reporting at https://console.cloud.google.com/errors to view the reported errors.

## Running on Google Cloud Platform

### Google App Engine Flexible environment

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.

### Google Compute Engine

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.

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).

### Google Container Engine

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).

## Running Elsewhere

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.

1. You will need to specify your project ID when starting the errors agent.

GCLOUD_PROJECT=particular-future-12345 node myapp.js

1. You need to provide service account credentials to your application.
* The recommended way is via [Application Default Credentials][app-default-credentials].
1. [Create a new JSON service account key][service-account].
1. Copy the key somewhere your application can access it. Be sure not to expose the key publicly.
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.
* 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.
* 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:

```js
// Require and start the agent with configuration options
var errors = require('@google-cloud/error-reporting')({
// The path to your key file:
keyFilename: '/path/to/keyfile.json',

// Or the contents of the key file:
credentials: require('./path/to/keyfile.json')
});
```

When running on Google Cloud Platform, we handle these for you automatically.

## Configuration

The following code snippet lists all available configuration options. All configuration options are optional.

```js
var errors = require('@google-cloud/error-reporting')({
projectId: 'my-project-id',
keyFilename: '/path/to/keyfile.json',
credentials: require('./path/to/keyfile.json'),
// if true library will attempt to report errors to the service regardless
// of the value of NODE_ENV
// defaults to false
ignoreEnvironmentCheck: false,
// determines if the library will attempt to report uncaught exceptions
// defaults to true
reportUncaughtExceptions: true,
// determines the logging level internal to the library; levels range 0-5
// defaults to 2 (warnings)
logLevel: 2,
serviceContext: {
service: 'my-service',
version: 'my-service-version'
}
});
```

## Examples

### Reporting Manually

```js
var errors = require('@google-cloud/error-reporting')();
// Use the error message builder to custom set all message fields
var errorEvt = errors.event()
.setMessage('My error message')
.setUser('root@nexus');
errors.report(errorEvt, () => console.log('done!'));
// Or just use a regular error
errors.report(new Error('My error message'), () => console.log('done!'));
// One can even just use a string
errors.report('My error message');
```

### Using Express

```js
var express = require('express');
var app = express();
// Will create an errors instance based off env variables
var errors = require('@google-cloud/error-reporting')();

app.get('/error', (req, res, next) => {
res.send('Something broke!');
next(new Error('Custom error message'));
});

app.get('/exception', () => {
JSON.parse('{\"malformedJson\": true');
});

app.use(errors.express);

app.listen(3000);
```

### Using Hapi

```js
var hapi = require('hapi');
var errors = require('@google-cloud/error-reporting')();

var server = new hapi.Server();
server.connection({ port: 3000 });
server.start();

server.route({
method: 'GET',
path: '/error',
handler: (request, reply) => {
reply('Something broke!');
throw new Error('Custom error message');
}
});

server.register({ register: errors.hapi });
```

### Using Koa

```js
var errors = require('@google-cloud/error-reporting')();
var koa = require('koa');
var app = koa();

app.use(errors.koa);

app.use(function *(next) {
//This will set status and message
this.throw('Error Message', 500);
});

// response
app.use(function *(){
this.body = 'Hello World';
});

app.listen(3000);
```

### Using Restify

```js
function respond(req, res, next) {
next(new Error('this is a restify error'));
}

var restify = require('restify');
var errors = require('@google-cloud/error-reporting')();

var server = restify.createServer();

server.use(errors.restify(server));
server.get('/hello/:name', respond);
server.head('/hello/:name', respond);

server.listen(8080);
```

[gcloud-sdk]: https://cloud.google.com/sdk/gcloud/
[app-default-credentials]: https://developers.google.com/identity/protocols/application-default-credentials
[service-account]: https://console.developers.google.com/apis/credentials/serviceaccountkey
[npm-image]: https://badge.fury.io/js/%40google-cloud%2Ferror-reporting.svg
[npm-url]: https://npmjs.org/package/@google-cloud/error-reporting
[snyk-image]: https://snyk.io/test/npm/@google-cloud/error-reporting/badge.svg
[snyk-url]: https://snyk.io/test/npm/@google-cloud/error-reporting
Binary file added packages/error-reporting/config.tar.enc
Binary file not shown.
53 changes: 53 additions & 0 deletions packages/error-reporting/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

"name": "@google-cloud/error-reporting",
"description": "Stackdriver Error Reporting Client Library for Node.js",
"main": "./src/index.js",
"repository": "GoogleCloudPlatform/google-cloud-node",
"scripts": {
"test": "nyc --exclude=\"fuzzer.js\" mocha ./test/unit/*.js",
"system-test": "nyc --exclude=\"error-message.js\" mocha ./system-test/*.js",
"lint": "jshint src/ index.js",
"publish-module": "node ../../scripts/publish.js error-reporting"
},
"author": "Google Inc.",
"license": "Apache-2.0",
"devDependencies": {
"body-parser": "^1.15.1",
"express": "^4.13.4",
"hapi": "^16.1.0",
"jshint": "^2.9.2",
"koa": "^1.2.0",
"lodash.assign": "^4.2.0",
"lodash.foreach": "^4.5.0",
"lodash.indexof": "^4.0.5",
"lodash.maxby": "^4.6.0",
"lodash.merge": "^4.6.0",
"lodash.omit": "^4.5.0",
"lodash.omitby": "^4.6.0",
"lodash.random": "^3.2.0",
"lodash.without": "^4.4.0",
"mocha": "^3.2.0",
"nock": "^9.0.0",
"nyc": "^10.0.0",
"restify": "^4.1.0"
},
"dependencies": {
"@google-cloud/common": "^0.12.0",
"extend": "^3.0.0",
"is": "^3.2.0",
"lodash.has": "^4.5.2"

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

},
"nyc": {
"exclude": [
"./utils/fuzzer.js"
]
},
"engines": {
"node": ">=4.0"
},
"files": [
"src",
"utils",
"index.js"
]
}
Loading