Last active
January 7, 2016 07:18
-
-
Save okmttdhr/e5b68f59c1aee07afa65 to your computer and use it in GitHub Desktop.
fluct+API Gateway+Lambda+DynamoDBでJavaScriptのエラーログ収集 ref: http://qiita.com/okmttdhr/items/c9b7c2ca81a78fe8af50
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
{ | |
stack: [ | |
{functionName: 'fn', fileName: 'file.js', lineNumber: 32, columnNumber: 1}, | |
{functionName: 'fn2', fileName: 'file.js', lineNumber: 543, columnNumber: 32}, | |
{functionName: 'fn3', fileName: 'file.js', lineNumber: 8, columnNumber: 1} | |
] | |
} |
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
"fluct": { | |
"accountId": "", | |
"restapiId": "", | |
"roleName": "lambdaLoggingApp", | |
"region": "ap-northeast-1" | |
}, |
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
var Promise = require('bluebird'); | |
var uuid = require('uuid'); | |
var AWS = require('aws-sdk'); | |
var dynamodb = new AWS.DynamoDB({region: 'ap-northeast-1'}); | |
function putItem(query) { | |
return new Promise(function(resolve, reject) { | |
var params = { | |
TableName: query.tableName, | |
Item: { | |
'Id': {'S': uuid.v1()}, | |
'Date': {'N': query.date}, | |
'Stack': {'S': query.stack}, | |
}, | |
}; | |
dynamodb.putItem(params, function(err, data) { | |
if (err) return reject(err); | |
return resolve(JSON.stringify(data)); | |
}); | |
}); | |
} | |
exports.handler = function(event, context) { | |
if (!event || !event.requestParameters) { | |
return context.done(null, { | |
'status': 404, | |
'message': 'Invalid Parameters' | |
}); | |
}; | |
var query = { | |
tableName: event.requestParameters.tableName, | |
stack: event.requestParameters.stack, | |
date: event.requestParameters.date, | |
}; | |
putItem(query) | |
.then(function() { | |
context.done(null, { | |
'status': 200, | |
'message': 'OK' | |
}); | |
}) | |
.catch(function(err) { | |
context.done(null, { | |
'status': null, | |
'message': err, | |
}); | |
}) | |
}; |
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
import request from 'superagent'; | |
import Promise from 'bluebird'; | |
import StackTrace from 'stacktrace-js'; | |
import moment from 'moment'; | |
import { LOGGING_DB, LOGGING_ENDPOINT } from 'config'; | |
/** | |
* エラーログをポストする。 | |
* @param {Object} query | |
* @return {Object} | |
*/ | |
function _postLog(query) { | |
return new Promise((resolve, reject)=> { | |
request | |
.post(LOGGING_ENDPOINT) | |
.send(query) | |
.end(function(err, res) { | |
if (err) return reject(err); | |
return resolve(res.text); | |
}); | |
}); | |
} | |
/** | |
* エラーログ収集のlambda関数を実行する | |
*/ | |
export function logError() { | |
window.onerror = function(msg, file, line, col, error) { | |
StackTrace | |
.fromError(error) | |
.then((stackframes) => { | |
const query = { | |
tableName: LOGGING_DB, | |
stack: JSON.stringify(stackframes), | |
date: moment().format('YYYYMMDDHHmmss'), | |
}; | |
return _postLog(query); | |
}) | |
.catch((err) => { | |
console.log(err); | |
}); | |
}; | |
} |
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
import Log from 'error-log-module'; | |
const log = new Log({ | |
LOGGING_DB, | |
LOGGING_ENDPOINT, | |
}); | |
log.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment