-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathappstats.js
347 lines (313 loc) · 11.6 KB
/
appstats.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* -----------------------------------------------------------------------------
* @copyright (C) 2018, Alert Logic, Inc
* @doc
*
* The module for getting function invocation statistics from storage account.
*
* @end
* -----------------------------------------------------------------------------
*/
const async = require('async');
const util = require('util');
const moment = require('moment');
const parse = require('parse-key-value');
const azureStorage = require('azure-storage');
const TableQuery = azureStorage.TableQuery;
const TableUtilities = azureStorage.TableUtilities;
const STATS_PERIOD_MINUTES = 15;
const STAT_MSG_VISIBILITY_TIMEOUT_SEC = 300;
const STAT_MSG_NUMBER_PER_BATCH = 32;
const STAT_TYPES_LOG = 1;
const DEFAULT_STATS_QUEUE_NAME = 'alertlogic-stats';
/**
* @class
* A class for retrieving Azure web application invocation statistics.
*
* @constructor
* @param {List} functionNames - (optional) a list of Azure Function names. Default is [].
**/
class AzureWebAppStats {
constructor(functionNames = []) {
this._functionNames = functionNames;
const storageParams = parse(process.env.AzureWebJobsStorage);
this._tableService = azureStorage.createTableService(
storageParams.AccountName,
storageParams.AccountKey,
storageParams.AccountName + '.table.core.windows.net');
}
getTableService() {
return this._tableService;
}
getLogTableName() {
return 'AzureWebJobsHostLogs' + moment.utc().format('YYYYMM');
}
_getInvocationsQuery(functionName, timestamp) {
var functionFilter = TableQuery.stringFilter(
'FunctionName',
TableUtilities.QueryComparisons.EQUAL,
functionName);
var dateFilter = TableQuery.dateFilter(
'StartTime',
TableUtilities.QueryComparisons.GREATER_THAN_OR_EQUAL,
new Date(moment(timestamp).utc().subtract(STATS_PERIOD_MINUTES, 'minutes')));
var whereFilter = TableQuery.combineFilters(
dateFilter,
TableUtilities.TableOperators.AND,
functionFilter);
return new TableQuery().where(whereFilter);
};
_getInvocationStats(entities, accStats) {
accStats.invocations += entities.length;
return entities.reduce(function(acc, current) {
if (current.ErrorDetails) {
acc.errors++;
}
return acc;
},
accStats);
};
_getFunctionStats(functionName, timestamp, callback) {
var accStats = {
invocations : 0,
errors : 0
};
return this._getFunctionStatsAcc(functionName, timestamp, null, accStats, callback);
};
_getFunctionStatsAcc(functionName, timestamp, contToken, accStats, callback) {
var tableService = this._tableService;
var appstats = this;
var obj = {};
tableService.queryEntities(
appstats.getLogTableName(),
appstats._getInvocationsQuery(functionName, timestamp),
contToken,
function(error, result) {
if (error) {
obj[functionName] = {
error : `${error}`
};
return callback(null, obj);
} else {
if (result.continuationToken) {
return appstats._getFunctionStatsAcc(
functionName,
timestamp,
result.continuationToken,
appstats._getInvocationStats(result.entries, accStats),
callback);
} else {
obj[functionName] = appstats._getInvocationStats(result.entries, accStats);
return callback(null, obj);
}
}
});
};
/**
* @function
* Retrieve application stats for the last 15 mins starting from provided 'timestamp'.
* Stats include: function invocations total and invocation error count.
*
* @param {String} timestamp - for example, '2017-12-22T14:31:39'. Usually Master function timer trigger value is used.
*
* @return callback(err, stats)
* @param {Object} stats - for example,
* {
* statistics: [
* {"Master":
* {"invocations":2,"errors":0}
* },
* {"Collector":
* {"invocations":10,"errors":1}
* },
* {"Updater":
* {"invocations":0,"errors":0}
* }
* ]
* }
*/
getAppStats(timestamp, callback) {
var appstats = this;
async.map(appstats._functionNames,
function(fname, callback){
appstats._getFunctionStats(fname, timestamp, callback);
},
function (mapErr, mapsResult) {
if (mapErr) {
return callback(mapErr);
} else {
return callback(null, {statistics: mapsResult});
}
});
};
}
class CollectionStatRecord {
constructor() {
this.log = {
bytes: 0,
events: 0
};
};
reset() {
this.log = {
bytes: 0,
events: 0
};
};
add(addStats) {
if (addStats instanceof CollectionStatRecord) {
this.log.bytes += addStats.log.bytes;
this.log.events += addStats.log.events;
}
return this;
};
subtract(subtractStats) {
if (subtractStats instanceof CollectionStatRecord) {
this.log.bytes -= subtractStats.log.bytes > this.log.bytes ? this.log.bytes : subtractStats.log.bytes;
this.log.events -= subtractStats.log.events > this.log.events ? this.log.events : subtractStats.log.events;
}
return this;
};
// Update with stat messages from the Storage queue.
// msg.messageText is a JSON like.
// {invocationId : invId,
// type: STAT_TYPES_LOG,
// bytes: collectedBytes,
// events: collectedEvents}
//
_aggregateStats(statsMessages) {
var initStats = new CollectionStatRecord();
return statsMessages.reduce(function(acc, curr) {
try {
const stat = JSON.parse(curr.messageText);
switch(stat.type) {
case STAT_TYPES_LOG:
acc.log.bytes += stat.bytes;
acc.log.events += stat.events;
break;
default:
break;
};
return acc;
} catch (e) {
return acc;
}
}, initStats);
};
aggregateAdd(statsMessages) {
const aggrStats = this._aggregateStats(statsMessages);
return this.add(aggrStats);
};
aggregateSubtract(statsMessages) {
const aggrStats = this._aggregateStats(statsMessages);
return this.subtract(aggrStats);
}
}
class AzureCollectionStats {
constructor(context, {statsQueueName, outputQueueBinding} = {}) {
const storageParams = parse(process.env.AzureWebJobsStorage);
this._context = context;
this._statsQueueName = statsQueueName ? statsQueueName :
process.env.APP_STATS_QUEUE_NAME ? process.env.APP_STATS_QUEUE_NAME : DEFAULT_STATS_QUEUE_NAME;
this._outputQueueBinding = outputQueueBinding;
this._queueService = azureStorage.createQueueService(
storageParams.AccountName,
storageParams.AccountKey,
storageParams.AccountName + '.queue.core.windows.net');
}
getQueueService() {
return this._queueService;
};
_getStatsBatch(callback) {
var stats = this;
var queueService = stats._queueService;
const queueName = stats._statsQueueName;
const options = {
visibilityTimeout: STAT_MSG_VISIBILITY_TIMEOUT_SEC,
numOfMessages: STAT_MSG_NUMBER_PER_BATCH
};
var aggrStats = new CollectionStatRecord();
queueService.getMessages(queueName, options, function(error, statsMessages) {
if(!error) {
aggrStats.aggregateAdd(statsMessages);
async.filter(statsMessages, function(msg, callback) {
queueService.deleteMessage(queueName, msg.messageId, msg.popReceipt, function(err) {
return callback(null, err);
});
}, function(error, undeleted) {
aggrStats.aggregateSubtract(undeleted);
return callback(null, aggrStats);
});
} else if (error && error.code === 'QueueNotFound') {
return callback(null, aggrStats);
} else {
return callback(error, aggrStats);
}
});
};
getStats(callback) {
var stats = this;
const queueService = stats._queueService;
const queueName = stats._statsQueueName;
var resultStats = new CollectionStatRecord();
var resultError = '';
queueService.getQueueMetadata(queueName, function(error, metadata) {
if (!error) {
var processed = 0;
async.doWhilst(function(callback) {
stats._getStatsBatch(function(error, aggrStatsBatch) {
resultError = error ? error + resultError : resultError;
resultStats.add(aggrStatsBatch);
processed += STAT_MSG_NUMBER_PER_BATCH;
return callback();
});
}, function() {
return processed < metadata.approximateMessageCount;
}, function() {
if (!resultError) {
return callback(null, resultStats);
} else {
return callback(resultError, resultStats);
}
});
} else if (error && error.code === 'QueueNotFound') {
return callback(null, resultStats);
} else {
return callback(error, resultStats);
}
});
};
putLogStats(collectedBytes, collectedEvents, callback) {
const invId = this._context.executionContext.invocationId;
const logStats = {
invocationId : invId,
type: STAT_TYPES_LOG,
bytes: collectedBytes,
events: collectedEvents
};
return this._putStats(logStats, callback);
};
_putStats(collectionStats, callback) {
var stats = this;
const queueName = stats._statsQueueName;
var outBinding = stats._outputQueueBinding;
const collectionStatsString = JSON.stringify(collectionStats);
if (outBinding) {
outBinding.push(collectionStatsString);
return callback(null);
} else {
return async.waterfall([
function(callback) {
return stats._queueService.createQueueIfNotExists(queueName, callback);
},
function(metadata, resp, callback) {
return stats._queueService.createMessage(queueName, collectionStatsString, callback);
},
], callback);
}
};
}
module.exports = {
AzureWebAppStats: AzureWebAppStats,
AzureCollectionStats: AzureCollectionStats,
CollectionStatRecord: CollectionStatRecord
};