-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from GoogleCloudPlatform/errorreporting
Add App Engine error reporting example.
- Loading branch information
Showing
6 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Node.js error reporting sample for Google App Engine | ||
|
||
This sample demonstrates error reporting in a Node.js app for | ||
[Google App Engine Managed VMs](https://cloud.google.com/appengine). | ||
|
||
## Running locally | ||
|
||
Refer to the [appengine/README.md](../README.md) file for instructions on | ||
running and deploying. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/*jshint unused:false*/ | ||
// Copyright 2015-2016, Google, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// [START app] | ||
'use strict'; | ||
|
||
var express = require('express'); | ||
var winston = require('winston'); | ||
|
||
var app = express(); | ||
var logFile = '/var/log/app_engine/custom_logs/myapp.errors.log.json'; | ||
|
||
winston.add(winston.transports.File, { | ||
filename: logFile | ||
}); | ||
|
||
function report(err, req) { | ||
var payload = { | ||
serviceContext: { | ||
service: 'myapp', | ||
}, | ||
message: 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); | ||
} | ||
|
||
app.get('/', function (req, res, next) { | ||
next(new Error('something is wrong!')); | ||
}); | ||
|
||
app.use(function (err, req, res, next) { | ||
report(err, req); | ||
res.status(500).send(err.message || 'Something broke!'); | ||
}); | ||
|
||
var server = app.listen(process.env.PORT || '8080', function () { | ||
console.log('App listening on port %s', server.address().port); | ||
console.log('Press Ctrl+C to quit.'); | ||
}); | ||
// [END app] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2015-2016, Google, Inc. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# [START app_yaml] | ||
runtime: nodejs | ||
vm: true | ||
|
||
skip_files: | ||
- ^(.*/)?.*/node_modules/.*$ | ||
# [END app_yaml] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "appengine-error-reporting", | ||
"description": "Node.js error reporting sample for Google App Engine", | ||
"version": "0.0.1", | ||
"private": true, | ||
"license": "Apache Version 2.0", | ||
"author": "Google Inc.", | ||
"engines": { | ||
"node": "~4.2" | ||
}, | ||
"scripts": { | ||
"start": "node app.js", | ||
"monitor": "nodemon app.js", | ||
"deploy": "gcloud preview app deploy" | ||
}, | ||
"dependencies": { | ||
"express": "^4.13.4", | ||
"winston": "^2.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ | |
}, | ||
"ava": { | ||
"files": [ | ||
"test/**/*.test.js" | ||
"test/appengine/**/*.test.js" | ||
] | ||
}, | ||
"devDependencies": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters