Last active
March 21, 2016 19:49
-
-
Save feinoujc/8754b40123cba93636ac to your computer and use it in GitHub Desktop.
Send DynamoDB inserts of elmah logs to slack
This file contains hidden or 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
console.log('Loading function'); | |
const https = require('https'); | |
const url = require('url'); | |
// set up slack inbound webhook url | |
const slack_url = 'XXXXXXXXXXXX'; | |
const slack_req_opts = url.parse(slack_url); | |
const elmah_details_url = 'https://website/elmah.axd/detail?id='; | |
slack_req_opts.method = 'POST'; | |
slack_req_opts.headers = {'Content-Type': 'application/json'}; | |
exports.handler = function(event, context) { | |
//console.log("%j", event); | |
var total = event.Records.length; | |
var count = 0; | |
(event.Records || []).forEach(function (rec) { | |
console.log('received event:', rec.eventID); | |
var req = https.request(slack_req_opts, function (res) { | |
if (res.statusCode === 200) { | |
count++; | |
if(total === count) { | |
console.log('posted all', count, 'records to slack'); | |
context.succeed('posted to slack'); | |
} | |
else { | |
console.log('posted', count, 'of', total, 'records to slack'); | |
} | |
} else { | |
context.fail('status code:', res.statusCode); | |
} | |
}); | |
req.on('error', function(e) { | |
console.log('problem with request:', e.message); | |
context.fail(e.message); | |
}); | |
var dynamodb = rec.dynamodb; | |
error = { | |
errorId: dynamodb.Keys.ErrorId.S, | |
type: dynamodb.NewImage.Type.S, | |
message: dynamodb.NewImage.Message.S, | |
user: (user = dynamodb.NewImage.User) ? user.S : null, | |
link: elmah_details_url + dynamodb.Keys.ErrorId.S | |
}; | |
fields = []; | |
fields.push({ | |
title: 'type', | |
value: error.type, | |
short: true | |
}); | |
if (error.user) { | |
fields.push({ | |
title: 'user', | |
value: error.user, | |
short: true | |
}); | |
} | |
var payload = { | |
attachments: [ | |
{ | |
title: error.message, | |
title_link: error.link, | |
fallback: error.message, | |
fields: fields | |
} | |
] | |
} | |
req.write(JSON.stringify(payload)); | |
req.end(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment