-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg-backend.js
572 lines (477 loc) · 15.3 KB
/
pg-backend.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
///////////////////////////////////////////////////////////////////////////////////
// NodeJS Statsd PostgreSQL Backend
// ------------------------------------------------------------------------------
//
// Authors: Dmitry Gribanov
// Date: 19/09/2019
//
///////////////////////////////////////////////////////////////////////////////////
const { Pool } = require('pg');
var util = require('util'),
fs = require('fs'),
sequence = require('sequence').Sequence.create();
var STATSD_PACKETS_RECEIVED = "statsd.packets_received";
var STATSD_BAD_LINES = "statsd.bad_lines_seen";
/**
* Backend Constructor
*
* Example config :
*
postgres: {
host: "localhost",
port: 5432,
user: "root",
password: "root",
database: "statsd_db",
tables: ["statsd_users", "statsd_statistics"]
}
*
* @param startupTime
* @param config
* @param emmiter
* @param client
*/
function StatdPostgreSQLBackend(startupTime, config, emitter, client) {
var self = this;
self.config = config.postgres || {};
self.engines = {
counters: [],
gauges: [],
timers: [],
sets: []
};
self.client = client;
// Verifying that the config file contains enough information for this backend to work
if(!this.config.host || !this.config.database || !this.config.user) {
console.log("You need to specify at least host, port, database, user and password for this PostgreSQL backend");
process.exit(-1);
}
// Default port for Postgres is 5432, if unset in conf file, we set it here to default
if(!this.config.port) {
this.config.port = 5432;
}
// Set backend path
for(var backend_index in config.backends) {
var currentBackend = config.backends[backend_index];
if(currentBackend.indexOf('pg-backend.js') > -1) {
self.config.backendPath = currentBackend.substring(0, currentBackend.lastIndexOf('/')+1);
}
}
//Default tables
if(!this.config.tables) {
this.config.tables = {counters: ["counters_statistics"], gauges: ["gauges_statistics"], timers:["timers_statistics"],sets:["sets_statistics"]};
}
// Default engines
if(!self.config.engines) {
self.config.engines = {
counters: ["engines/countersEngine.js"],
gauges: ["engines/gaugesEngine.js"],
timers: ["engines/timersEngine.js"],
sets: ["engines/setsEngine.js"]
};
}
// Synchronous sequence
sequence.then(function( next ) {
// Check if tables exists
self.checkDatabase(function(err) {
if(err) {
console.log('Database check failed ! Exit...');
process.exit(-1);
} else {
console.log('Database is valid.');
next();
}
});
}).then(function( next ) {
process.stdout.write('Loading PostgreSQL backend engines...');
// Load backend engines
self.loadEngines(function(err) {
if(err) {
process.stdout.write("[FAILED]\n");
console.log(err);
}
process.stdout.write("[OK]\n");
next();
});
}).then(function( next ) {
// Attach events
emitter.on('flush', function(time_stamp, metrics) { self.onFlush(time_stamp, metrics); } );
emitter.on('status', self.onStatus );
console.log("Statsd PostgreSQL backend is loaded.");
});
}
/**
* Load PostgreSQL Backend Query Engines
*
*/
StatdPostgreSQLBackend.prototype.loadEngines = function(callback) {
var self = this;
// Iterate on each engine type defined in configuration
for(var engineType in self.config.engines) {
var typeEngines = self.config.engines[engineType];
// Load engines for current type
for(var engineIndex in typeEngines) {
// Get current engine path
var enginePath = typeEngines[engineIndex];
// Load current engine
var currentEngine = require(self.config.backendPath + enginePath).init();
if(currentEngine === undefined) {
callback("Unable to load engine '" + enginePath + "' ! Please check...");
}
// Add engine to PostgreSQL Backend engines
self.engines[engineType].push(currentEngine);
}
}
callback();
}
/**
* Check if required tables are created. If not create them.
*
*/
StatdPostgreSQLBackend.prototype.checkDatabase = function(callback) {
var self = this;
console.log("Checking database...");
var tables = self.config.tables
// Count stats types
var typesCount = 0;
for(var statType in tables) { typesCount++; }
// Iterate on each stat type (counters, gauges, ...)
var statTypeIndex = 0;
for(var statType in tables) {
// Get tables for current stat type
var typeTables = tables[statType];
// Count tables for current type
var tablesCount = 0;
for(var table_index in typeTables) { tablesCount++; }
// Check if tables exists for current type
self.checkIfTablesExists(statTypeIndex, typeTables, tablesCount, 0, function(type_index, err) {
if(err) {
callback(err);
}
// If all types were parsed, call the callback method
if(type_index == typesCount-1) {
callback();
}
});
statTypeIndex++;
}
}
/**
* Check if a table exists in database. If not, create it.
*/
StatdPostgreSQLBackend.prototype.checkIfTablesExists = function(type_index, tables_names, size, startIndex, callback) {
var self = this;
q = 'SELECT * FROM pg_catalog.pg_tables WHERE pg_tables.tablename like \'' + tables_names[startIndex] + '\';';
self.client.query(q, (err, res) => {
if (err) {
callback(err);
}
if(res.rows.length == 0) {
console.log("Table '" + tables_names[startIndex] + "' was not found !");
// Create table
self.createTable(tables_names[startIndex], function(err) {
if(err) {
callback(type_index, err);
}
if(startIndex == size - 1) {
// If all tables were created for this type, call the callback method
callback(type_index);
}
else {
// Else iterate on the next table to create
self.checkIfTablesExists(type_index, tables_names, size, startIndex+1, callback);
}
});
}
// If table was found in database
else {
console.log("Table '" + tables_names[startIndex] + "' was found.");
if(startIndex == size-1){
// If all tables were created for this type, call the callback method
callback(type_index);
}
else {
// Else iterate on the next table to create
self.checkIfTablesExists(type_index, tables_names, size, startIndex+1, callback)
}
}
})
}
/**
* Create a table from corresponding sql script file
*/
StatdPostgreSQLBackend.prototype.createTable = function(table_name, callback) {
var self = this;
// Try to read SQL file for this table
var sqlFilePath = self.config.backendPath + 'tables/' + table_name + '.sql';
fs.readFile(sqlFilePath, 'utf8', function (err,data) {
if (err) {
console.log("Unable to read file: '" + sqlFilePath + "' !");
callback(err);
}
// Split querries
var querries = data.split("$$");
// Prepare querries
var queuedQuerries = "";
for(var queryIndex in querries) {
var query = querries[queryIndex];
if(query.trim() == "") continue;
queuedQuerries += query;
if(queuedQuerries[queuedQuerries.length-1] !== ";") {
queuedQuerries += ";";
}
}
// Execute querries
self.client.query(queuedQuerries, (err, res) => {
if (err) {
console.log("Unable to execute query: '" + queuedQuerries +"' for table '"+table_name+"' !");
callback(err);
}
console.log("Table '" + table_name +"' was created with success.");
callback();
})
});
}
/**
* Method executed when statsd flush received datas
*
* @param time_stamp
* @param metrics
*/
StatdPostgreSQLBackend.prototype.onFlush = function(time_stamp, metrics) {
var self = this;
var counters = metrics['counters'];
var timers = metrics['timers'];
var gauges = metrics['gauges'];
var sets = metrics['sets'];
var pctThreshold = metrics['pctThreshold'];
//console.log("METRICS : \n " + util.inspect(metrics) + "\n ===========================");
// Handle statsd counters
self.handleCounters(counters,time_stamp);
// Handle statsd gauges
self.handleGauges(gauges,time_stamp);
// Handle statsd timers
self.handleTimers(timers,time_stamp);
// Handle stastd sets
self.handleSets(sets,time_stamp);
}
/**
* Handle and process received counters
*
* @param _counters received counters
* @param time_stamp flush time_stamp
*/
StatdPostgreSQLBackend.prototype.handleCounters = function(_counters, time_stamp) {
var self = this;
var packets_received = parseInt(_counters[STATSD_PACKETS_RECEIVED]);
var bad_lines_seen = parseInt(_counters[STATSD_BAD_LINES]);
if(packets_received > 0) {
// Get userCounters for this flush
var userCounters = self.getUserCounters(_counters);
var userCountersSize = 0;
for(var userCounterName in userCounters) { userCountersSize++; }
if(userCountersSize > 0) {
console.log("Counters received !");
var querries = [];
//////////////////////////////////////////////////////////////////////
// Call buildQuerries method on each counterEngine
for(var countersEngineIndex in self.engines.counters) {
console.log("countersEngineIndex = " + countersEngineIndex);
var countersEngine = self.engines.counters[countersEngineIndex];
// Add current engine querries to querries list
var engineQuerries = countersEngine.buildQuerries(userCounters, time_stamp);
querries = querries.concat(engineQuerries);
// Insert data into database every 100 query
if(querries.length >= 100) {
// Execute querries
self.executeQuerries(querries);
querries = [];
}
}
if(querries.length > 0) {
// Execute querries
self.executeQuerries(querries);
querries = [];
}
}
}
}
/**
* Handle and process received gauges
*
* @param _gauges received _gauges
* @param time_stamp flush time_stamp
*/
StatdPostgreSQLBackend.prototype.handleGauges = function(_gauges, time_stamp) {
var self = this;
var gaugesSize = 0
for(var g in _gauges) { gaugesSize++; }
// If gauges received
if(gaugesSize > 0) {
console.log("Gauges received !");
console.log("Gauges = " + util.inspect(_gauges));
var querries = [];
//////////////////////////////////////////////////////////////////////
// Call buildQuerries method on each counterEngine
for(var gaugesEngineIndex in self.engines.gauges) {
console.log("gaugesEngineIndex = " + gaugesEngineIndex);
var gaugesEngine = self.engines.gauges[gaugesEngineIndex];
// Add current engine querries to querries list
var engineQuerries = gaugesEngine.buildQuerries(_gauges, time_stamp);
querries = querries.concat(engineQuerries);
// Insert data into database every 100 query
if(querries.length >= 100) {
// Execute querries
self.executeQuerries(querries);
querries = [];
}
}
if(querries.length > 0) {
// Execute querries
self.executeQuerries(querries);
querries = [];
}
}
}
/**
* Handle and process received timers
*
* @param _timers received timers
* @param time_stamp flush time_stamp
*/
StatdPostgreSQLBackend.prototype.handleTimers = function(_timers, time_stamp) {
var self = this;
var timersSize = 0
for(var t in _timers) { timersSize++; }
// If timers received
if(timersSize > 0) {
console.log("Timers received !");
console.log("Timers = " + util.inspect(_timers));
var querries = [];
//////////////////////////////////////////////////////////////////////
// Call buildQuerries method on each counterEngine
for(var timersEngineIndex in self.engines.timers) {
console.log("timersEngineIndex = " + timersEngineIndex);
var timersEngine = self.engines.timers[timersEngineIndex];
// Add current engine querries to querries list
var engineQuerries = timersEngine.buildQuerries(_timers, time_stamp);
querries = querries.concat(engineQuerries);
// Insert data into database every 100 query
if(querries.length >= 100) {
// Execute querries
self.executeQuerries(querries);
querries = [];
}
}
if(querries.length > 0) {
// Execute querries
self.executeQuerries(querries);
querries = [];
}
}
}
/**
* Handle and process received sets
*
* @param _sets received sets
* @param time_stamp flush time_stamp
*/
StatdPostgreSQLBackend.prototype.handleSets = function(_sets, time_stamp) {
var self = this;
var setsSize = 0
for(var s in _sets) { setsSize++; }
// If timers received
if(setsSize > 0) {
console.log("sets received !");
console.log("Sets = " + util.inspect(_sets));
var querries = [];
//////////////////////////////////////////////////////////////////////
// Call buildQuerries method on each counterEngine
for(var setsEngineIndex in self.engines.sets) {
console.log("setsEngineIndex = " + setsEngineIndex);
var setsEngine = self.engines.sets[setsEngineIndex];
// Add current engine querries to querries list
var engineQuerries = setsEngine.buildQuerries(_sets, time_stamp);
querries = querries.concat(engineQuerries);
// Insert data into database every 100 query
if(querries.length >= 100) {
// Execute querries
self.executeQuerries(querries);
querries = [];
}
}
if(querries.length > 0) {
// Execute querries
self.executeQuerries(querries);
querries = [];
}
}
}
/**
* MISSING DOCUMENTATION
*
* @param sqlQuerries
*/
StatdPostgreSQLBackend.prototype.executeQuerries = function(sqlQuerries) {
var self = this;
for(var i = 0 ; i < sqlQuerries.length ; i++){
console.log("Query " + i + " : " + sqlQuerries[i]);
self.client.query(sqlQuerries[i], (err, res) => {
//done();
if (err) {
console.log(" -> Query [ERROR]" + err.stack);
} //else {
// console.log(" -> Query [SUCCESS]", sqlQuerries[i]);
//}
})
}
}
/**
*
*
*/
StatdPostgreSQLBackend.prototype.getUserCounters = function(_counters) {
var userCounters = {};
for(var counterName in _counters) {
var counterNameParts = counterName.split('.');
if(counterNameParts[0] !== "statsd") {
userCounters[counterName] = _counters[counterName];
}
}
return userCounters;
}
/**
*
*
*/
StatdPostgreSQLBackend.prototype.getStatsdCounters = function(_counters) {
var statsdCounters = {};
for(var counterName in _counters) {
var counterNameParts = counterName.split('.');
if(counterNameParts[0] === "statsd") {
statsdCounters[counterName] = _counters[counterName];
}
}
return statsdCounters;
}
/**
*
* @param error
* @param backend_name
* @param stat_name
* @param stat_value
*/
StatdPostgreSQLBackend.prototype.onStatus = function(error, backend_name, stat_name, stat_value) {
}
exports.init = function(startupTime, config, events) {
var pool = new Pool(config.postgres);
pool.on('error', (err, client) => {
console.error('Unexpected error on idle client', err)
process.exit(-1)
});
pool.connect((err, client, done) => {
var instance = new StatdPostgreSQLBackend(startupTime, config, events, client);
if (err) throw new Error(err);
});
return true;
};