-
Notifications
You must be signed in to change notification settings - Fork 2
/
logsWebtask.js
150 lines (131 loc) · 4.17 KB
/
logsWebtask.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
Gets Auth0 logs
*/
//var Auth0 = require("auth0");
var Auth0 = require("[email protected]");
var request = require("request");
var NestedError = require('nested-error-stacks');
var auth0Api;
var logglyClient;
//default number of logs to fetch at a time from auth0 API to bulk insert in Loggly
var DEFAULT_TAKE = 50;
var MAX_TAKE = 200;
module.exports = function(context, cb) {
console.log("[logsWebtask] Checking context params...");
var required_params = [
"AUTH0_DOMAIN",
"AUTH0_CLIENT_ID",
"AUTH0_CLIENT_SECRET",
"LOGGLY_TOKEN"
];
for (var p in required_params){
if (!context.data[required_params[p]]){
return cb(new Error("The '" + required_params[p] + "' parameter must be provided."));
}
}
//Get optional query string params
var lastLogId = context.data.checkpointId;
var take = context.data.take? parseInt(context.data.take): DEFAULT_TAKE;
if (isNaN(take) || take < 0 || take > MAX_TAKE){
return cb (new Error("The 'take' parameter must be an integer between 0 and 200, but "+context.data.take+" was received instead."));
}
//Initialize Clients
if (!auth0Api){
initAuth0Api();
}
if (!logglyClient){
initLogglyClient();
}
//Get last fetched log id from last successful run
if (context.body && context.body.results && context.body.results.length > 0){
console.log("[logsWebtask] context.body.results",JSON.stringify(context.body.results));
for (var i = 0; i < context.body.results.length; i++) {
if (context.body.results[i].statusCode === 200){
try {
lastLogId = JSON.parse(context.body.results[i].body).lastLogId;
} catch(parseEx){
return cb(new NestedError("Error parsing last execution body: " + context.body.results[i].body, parseEx));
}
break;
}
};
console.log("[logsWebtask] lastLogId",lastLogId);
}
//Get Auth0 Logs
var logsOpts = {
from: lastLogId,
take: take
};
console.log("Fetching logs from Auth0",logsOpts);
auth0Api.getLogs(logsOpts,function(err,result){
if (err){
return cb(new NestedError("Error retrieving Auth0 logs",err));
}
//Send them to Loggly
console.log("got logs!",result);
if (result && result.length > 0){
logglyClient.bulkLog(result,function(err,logResult){
if (err) return cb(err);
return cb(null,{
lastLogId : result[result.length - 1]._id
});
});
}
else {
return cb(null, {
lastLogId : lastLogId //if no results, keep same lastLogId
});
}
});
function initAuth0Api(){
var auth0Opts = {
domain: context.data["AUTH0_DOMAIN"] + ".auth0.com",
clientID: context.data["AUTH0_CLIENT_ID"],
clientSecret: context.data["AUTH0_CLIENT_SECRET"]
};
console.log("[logsWebtask] Initializing Auth0 api for domain ", auth0Opts.domain);
try {
auth0Api = new Auth0(auth0Opts);
} catch(err) {
return cb(new NestedError("Error initializing Auth0 API", err));
}
}
function initLogglyClient(opts){
var logglyOpts = {
token: context.data["LOGGLY_TOKEN"]
};
console.log("[logsWebtask] Initializing loggly client...");
logglyClient = new LogglyClient(logglyOpts);
};
};
//------------- Loggly Client -------------------------------------
var LogglyClient = function (options) {
if (!options || !options.token) {
throw new Error('options.token is required.');
}
this._host = options.host || "logs-01.loggly.com",
this._bulkUrl = "https://" + this._host + "/bulk/" + options.token + "/tag/bulk/";
};
LogglyClient.prototype.bulkLog = function (msgArray,cb) {
if (! Array.isArray(msgArray)){
return cb(new Error("msgArray should be an array"));
}
var messages = msgArray.map(JSON.stringify).join("\n");
var requestOpts = {
url: this._bulkUrl,
method: "POST",
body: messages,
headers: {
"content-type": "application/json"
}
};
request(requestOpts,function(err, res, body){
if (err){
return cb(new NestedError("Error sending logs to Loggly. Url: "+requestOpts.url,err));
}
if (res.statusCode !== 200) {
return cb(new Error("Error sending logs to Loggly. Status: "+r.statusCode+", body: "+body));
}
cb(null, JSON.parse(body));
});
};