Skip to content

Commit 4fc204c

Browse files
cristiancavallistephenplusplus
authored andcommitted
Stage errors for repo integration (#2017)
1 parent 7ed4291 commit 4fc204c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+7241
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BasedOnStyle: Google
2+
Language: Javascript

packages/error-reporting/.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
node_modules
2+
coverage
3+
npm-debug.log
4+
.DS_Store
5+
.eslintrc.js
6+
docs
7+
tests/configuration
8+
.nyc_output
9+
*.patch
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
test/e2e/node_modules
3+
test/fixtures
4+
coverage

packages/error-reporting/.jshintrc

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"bitwise": true,
3+
"curly": true,
4+
"eqeqeq": true,
5+
"esnext": true,
6+
"freeze": true,
7+
"immed": true,
8+
"indent": 2,
9+
"latedef": "nofunc",
10+
"maxlen": 100,
11+
"newcap": true,
12+
"node": true,
13+
"noarg": true,
14+
"quotmark": "single",
15+
"strict": true,
16+
"trailing": true,
17+
"undef": true,
18+
"unused": "vars",
19+
"globals": {
20+
"describe": false,
21+
"it": false,
22+
"before": false,
23+
"beforeEach": false,
24+
"after": false,
25+
"afterEach": false
26+
}
27+
}

packages/error-reporting/README.md

+230
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
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+
[![Learn about Error Reporting in Stackdriver](https://img.youtube.com/vi/cVpWVD75Hs8/0.jpg)](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
9.02 KB
Binary file not shown.

packages/error-reporting/package.json

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "@google-cloud/error-reporting",
3+
"description": "Stackdriver Error Reporting Client Library for Node.js",
4+
"main": "./src/index.js",
5+
"repository": "GoogleCloudPlatform/google-cloud-node",
6+
"scripts": {
7+
"test": "nyc --exclude=\"fuzzer.js\" mocha ./test/unit/*.js",
8+
"system-test": "nyc --exclude=\"error-message.js\" mocha ./system-test/*.js",
9+
"lint": "jshint src/ index.js",
10+
"publish-module": "node ../../scripts/publish.js error-reporting"
11+
},
12+
"author": "Google Inc.",
13+
"license": "Apache-2.0",
14+
"devDependencies": {
15+
"body-parser": "^1.15.1",
16+
"express": "^4.13.4",
17+
"hapi": "^16.1.0",
18+
"jshint": "^2.9.2",
19+
"koa": "^1.2.0",
20+
"lodash.assign": "^4.2.0",
21+
"lodash.foreach": "^4.5.0",
22+
"lodash.indexof": "^4.0.5",
23+
"lodash.maxby": "^4.6.0",
24+
"lodash.merge": "^4.6.0",
25+
"lodash.omit": "^4.5.0",
26+
"lodash.omitby": "^4.6.0",
27+
"lodash.random": "^3.2.0",
28+
"lodash.without": "^4.4.0",
29+
"mocha": "^3.2.0",
30+
"nock": "^9.0.0",
31+
"nyc": "^10.0.0",
32+
"restify": "^4.1.0"
33+
},
34+
"dependencies": {
35+
"@google-cloud/common": "^0.12.0",
36+
"extend": "^3.0.0",
37+
"is": "^3.2.0",
38+
"lodash.has": "^4.5.2"
39+
},
40+
"nyc": {
41+
"exclude": [
42+
"./utils/fuzzer.js"
43+
]
44+
},
45+
"engines": {
46+
"node": ">=4.0"
47+
},
48+
"files": [
49+
"src",
50+
"utils",
51+
"index.js"
52+
]
53+
}

0 commit comments

Comments
 (0)