-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Cloud Functions Error Reporting sample #140
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f1ced92
Add Cloud Functions Error Reporting sample.
jmdobry c0b092a
Couple of tweaks.
jmdobry a3d2f54
Tweak error reporting sample code.
jmdobry 353a621
Tweak samples.
jmdobry d138093
Added tests for latest GCF samples.
jmdobry abe6d4b
Update readme
jmdobry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,141 @@ | ||
// Copyright 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. | ||
|
||
'use strict'; | ||
|
||
// [START setup] | ||
var gcloud = require('gcloud'); | ||
|
||
// Get a reference to the StackDriver Logging component | ||
var logging = gcloud.logging(); | ||
// [END setup] | ||
|
||
// [START reportDetailedError] | ||
var reportDetailedError = require('./report'); | ||
// [END reportDetailedError] | ||
|
||
// [START helloSimpleErrorReport] | ||
/** | ||
* Report an error to StackDriver Error Reporting. Writes the minimum data | ||
* required for the error to be picked up by StackDriver Error Reporting. | ||
* | ||
* @param {Error} err The Error object to report. | ||
* @param {Function} callback Callback function. | ||
*/ | ||
function reportError (err, callback) { | ||
// This is the name of the StackDriver log stream that will receive the log | ||
// entry. This name can be any valid log stream name, but must contain "err" | ||
// in order for the error to be picked up by StackDriver Error Reporting. | ||
var logName = 'errors'; | ||
var log = logging.log(logName); | ||
|
||
// https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource | ||
var monitoredResource = { | ||
type: 'cloud_function', | ||
labels: { | ||
function_name: process.env.FUNCTION_NAME | ||
} | ||
}; | ||
|
||
// https://cloud.google.com/error-reporting/reference/rest/v1beta1/ErrorEvent | ||
var errorEvent = { | ||
message: err.stack, | ||
serviceContext: { | ||
service: 'cloud_function:' + process.env.FUNCTION_NAME, | ||
version: require('./package.json').version || 'unknown' | ||
} | ||
}; | ||
|
||
// Write the error log entry | ||
log.write(log.entry(monitoredResource, errorEvent), callback); | ||
} | ||
// [END helloSimpleErrorReport] | ||
|
||
// [START helloSimpleError] | ||
/** | ||
* HTTP Cloud Function. | ||
* | ||
* @param {Object} req Cloud Function request object. | ||
* @param {Object} res Cloud Function response object. | ||
*/ | ||
exports.helloSimpleError = function helloSimpleError (req, res) { | ||
try { | ||
if (req.method !== 'GET') { | ||
var error = new Error('Only GET requests are accepted!'); | ||
error.code = 405; | ||
throw error; | ||
} | ||
// All is good, respond to the HTTP request | ||
return res.send('Hello World!'); | ||
} catch (err) { | ||
// Report the error | ||
return reportError(err, function () { | ||
// Now respond to the HTTP request | ||
return res.status(error.code || 500).send(err.message); | ||
}); | ||
} | ||
}; | ||
// [END helloSimpleError] | ||
|
||
// [START helloHttpError] | ||
/** | ||
* HTTP Cloud Function. | ||
* | ||
* @param {Object} req Cloud Function request object. | ||
* @param {Object} res Cloud Function response object. | ||
*/ | ||
exports.helloHttpError = function helloHttpError (req, res) { | ||
try { | ||
if (req.method !== 'POST' && req.method !== 'GET') { | ||
var error = new Error('Only POST and GET requests are accepted!'); | ||
error.code = 405; | ||
throw error; | ||
} | ||
// All is good, respond to the HTTP request | ||
return res.send('Hello ' + (req.body.message || 'World') + '!'); | ||
} catch (err) { | ||
// Set the response status code before reporting the error | ||
res.status(err.code || 500); | ||
// Report the error | ||
return reportDetailedError(err, req, res, function () { | ||
// Now respond to the HTTP request | ||
return res.send(err.message); | ||
}); | ||
} | ||
}; | ||
// [END helloHttpError] | ||
|
||
// [START helloBackgroundError] | ||
/** | ||
* Background Cloud Function. | ||
* | ||
* @param {Object} context Cloud Function context object. | ||
* @param {Object} data Request data, provided by a trigger. | ||
* @param {string} data.message Message, provided by the trigger. | ||
*/ | ||
exports.helloBackgroundError = function helloBackgroundError (context, data) { | ||
try { | ||
if (!data.message) { | ||
throw new Error('"message" is required!'); | ||
} | ||
// All is good, respond with a message | ||
return context.success('Hello World!'); | ||
} catch (err) { | ||
// Report the error | ||
return reportDetailedError(err, function () { | ||
// Now finish mark the execution failure | ||
return context.failure(err.message); | ||
}); | ||
} | ||
}; | ||
// [END helloBackgroundError] |
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,15 @@ | ||
{ | ||
"name": "nodejs-docs-samples-functions-errorreporting", | ||
"description": "Node.js samples found on https://cloud.google.com", | ||
"version": "0.0.1", | ||
"private": true, | ||
"license": "Apache Version 2.0", | ||
"author": "Google Inc.", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" | ||
}, | ||
"dependencies": { | ||
"gcloud": "^0.36.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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright 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. | ||
|
||
'use strict'; | ||
|
||
var gcloud = require('gcloud'); | ||
var logging = gcloud.logging(); | ||
|
||
// [START helloHttpError] | ||
/** | ||
* Report an error to StackDriver Error Reporting. Writes up to the maximum data | ||
* accepted by StackDriver Error Reporting. | ||
* | ||
* @param {Error} err The Error object to report. | ||
* @param {Object} [req] Request context, if any. | ||
* @param {Object} [res] Response context, if any. | ||
* @param {Object} [options] Additional context, if any. | ||
* @param {Function} callback Callback function. | ||
*/ | ||
function reportDetailedError (err, req, res, options, callback) { | ||
if (typeof req === 'function') { | ||
callback = req; | ||
req = null; | ||
res = null; | ||
options = {}; | ||
} else if (typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
options || (options = {}); | ||
|
||
var FUNCTION_NAME = process.env.FUNCTION_NAME; | ||
var log = logging.log('errors'); | ||
|
||
// MonitoredResource | ||
// See https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource | ||
var resource = { | ||
// MonitoredResource.type | ||
type: 'cloud_function', | ||
// MonitoredResource.labels | ||
labels: { | ||
function_name: FUNCTION_NAME | ||
} | ||
}; | ||
if (typeof options.region === 'string') { | ||
resource.labels.region = options.region; | ||
} | ||
if (typeof options.projectId === 'string') { | ||
resource.labels.projectId = options.projectId; | ||
} | ||
|
||
var context = {}; | ||
if (typeof options.user === 'string') { | ||
// ErrorEvent.context.user | ||
context.user = options.user; | ||
} | ||
if (req && res) { | ||
// ErrorEvent.context.httpRequest | ||
context.httpRequest = { | ||
method: req.method, | ||
url: req.originalUrl, | ||
userAgent: typeof req.get === 'function' ? req.get('user-agent') : 'unknown', | ||
referrer: '', | ||
remoteIp: req.ip | ||
}; | ||
if (typeof res.statusCode === 'number') { | ||
context.httpRequest.responseStatusCode = res.statusCode; | ||
} | ||
} | ||
if (!(err instanceof Error) || typeof err.stack !== 'string') { | ||
// ErrorEvent.context.reportLocation | ||
context.reportLocation = { | ||
filePath: typeof options.filePath === 'string' ? options.filePath : 'unknown', | ||
lineNumber: typeof options.lineNumber === 'number' ? options.lineNumber : 0, | ||
functionName: typeof options.functionName === 'string' ? options.functionName : 'unknown' | ||
}; | ||
} | ||
|
||
try { | ||
if (options.version === undefined) { | ||
var pkg = require('./package.json'); | ||
options.version = pkg.version; | ||
} | ||
} catch (err) {} | ||
if (options.version === undefined) { | ||
options.version = 'unknown'; | ||
} | ||
|
||
// ErrorEvent | ||
// See https://cloud.google.com/error-reporting/reference/rest/v1beta1/ErrorEvent | ||
var structPayload = { | ||
// ErrorEvent.serviceContext | ||
serviceContext: { | ||
// ErrorEvent.serviceContext.service | ||
service: 'cloud_function:' + FUNCTION_NAME, | ||
// ErrorEvent.serviceContext.version | ||
version: '' + options.version | ||
}, | ||
// ErrorEvent.context | ||
context: context | ||
}; | ||
|
||
// ErrorEvent.message | ||
if (err instanceof Error && typeof err.stack === 'string') { | ||
structPayload.message = err.stack; | ||
} else if (typeof err === 'string') { | ||
structPayload.message = err; | ||
} else if (typeof err.message === 'string') { | ||
structPayload.message = err.message; | ||
} | ||
|
||
log.write(log.entry(resource, structPayload), callback); | ||
} | ||
// [END helloHttpError] | ||
|
||
module.exports = reportDetailedError; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this perhaps be upstreamed into gcloud?