forked from nuclon/Statsd-PostgreSQL-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatsdPostgresBackend.js
309 lines (260 loc) · 7.88 KB
/
statsdPostgresBackend.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
/*
This is a backend for the etsy/statsd service designed to dump stats
into PostgreSQL
*/
module.exports = (function () {
"use strict";
const { Pool } = require("pg");
const CryptoJS = require("crypto-js");
const elasticsearch = require("./elasticsearch.js");
require("log-timestamp");
// Items we don't want to store but are sent with every statsd flush
const IGNORED_STATSD_METRICS = [
"statsd.bad_lines_seen",
"statsd.packets_received",
"statsd.metrics_received",
"statsd.timestamp_lag",
"processing_time",
];
// The various statsd types as per https://github.com/etsy/statsd/blob/master/docs/metric_types.md
const STATSD_TYPES = {
count: "count",
timer: "timer",
gauges: "gauge",
sets: "sets",
};
const STAT_SCHEMA = [
"topic",
"category",
"subcategory",
"identity",
"metric",
];
let pgPool;
let pgdb;
let pghost;
let pgport;
let pguser;
let pgpass;
// Calling this method grabs a connection to PostgreSQL from the connection pool
// then returns a client to be used. Done must be called at the end of using the
// connection to return it to the pool.
const initConnectionPool = async function (defaultConfig) {
pgdb = defaultConfig.pgdb;
pghost = defaultConfig.pghost;
pgport = defaultConfig.pgport || 5432;
pguser = defaultConfig.pguser;
pgpass = defaultConfig.pgpass;
// If config path is set, override config with values from secrets (from externalsecrets)
if (process.env.CONFIG_PATH) {
console.log(
"Using config values from CONFIG_PATH: ",
process.env.CONFIG_PATH
);
require("dotenv").config({ path: process.env.CONFIG_PATH });
pgdb = process.env.DB_NAME;
pghost = process.env.DB_HOST;
pgport = process.env.DB_PORT;
pguser = process.env.DB_USER;
pgpass = process.env.DB_PASS;
}
const newPool = new Pool({
user: pguser,
host: pghost,
database: pgdb,
password: pgpass,
port: pgport,
keepAlive: true,
});
await newPool.connect();
return newPool;
};
const recompileMetricString = function (obj) {
let metricString = [
obj.topic,
obj.category,
obj.subcategory,
obj.identity,
obj.metric,
obj.type,
].join(".");
if (obj.tags) {
for (const tag in obj.tags) {
metricString += ";" + tag + "=" + obj.tags[tag];
}
}
return metricString;
}
const generateMetricHash = async (hashable) => {
return CryptoJS.MD5(hashable).toString();
}
// Insert new metrics values
const insertMetric = async function (obj, metricString) {
const hash = await generateMetricHash(obj.collected + "." + metricString);
await pgPool.query({
text: "SELECT add_stat($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
values: [
obj.collected,
obj.topic,
obj.category,
obj.subcategory,
obj.identity,
obj.metric,
obj.type,
obj.value,
obj.tags,
hash,
],
});
};
const batchInsertMetrics = async function (metrics) {
const metrics_copy = (metrics || []).slice(0);
const metricsArr = [];
if (metrics_copy.length === 0) {
return;
}
const seenHash = new Set();
for (const index in metrics_copy) {
try {
const metricString = recompileMetricString(metrics_copy[index]);
const hash = await generateMetricHash(metrics_copy[index].collected + "." + metricString);
if (seenHash.has(hash)) {
console.log("Skipping duplicate metric: " + metricString);
continue;
}
metricsArr.push([
metrics_copy[index].collected,
metrics_copy[index].topic,
metrics_copy[index].category,
metrics_copy[index].subcategory,
metrics_copy[index].identity,
metrics_copy[index].metric,
metrics_copy[index].type,
metrics_copy[index].value,
JSON.stringify(metrics_copy[index].tags),
hash,
]);
seenHash.add(hash);
console.log(metricString);
} catch (error) {
console.log(error);
}
}
const metricsPGArray = metricsArr
.map(metric => `(${metric.map(value => `'${value}'`).join(",")})::metricstat_type`)
.join(", ");
const start = Date.now();
await pgPool.query({
text: `SELECT batch_add_stat(ARRAY[${metricsPGArray}])`,
});
const end = Date.now();
console.log(`Batch insert took ${end - start}ms`);
}
// Inserts multiple metrics records
const insertMetrics = async function (metrics) {
const metrics_copy = (metrics || []).slice(0);
for (const index in metrics_copy) {
try {
const metricString = recompileMetricString(metrics_copy[index]);
const start = Date.now();
await insertMetric(metrics_copy[index], metricString);
const end = Date.now();
console.log(`${metricString} took ${end - start}ms`);
} catch (error) {
console.log(error);
}
}
};
const parseMetricAndTags = function (metricField) {
const tags = {};
// the index is always the metric, the rest are tags
const splitMetric = metricField.split(";");
const metric = splitMetric[0];
const splitTags = splitMetric.slice(1);
for (const index in splitTags) {
const tag = splitTags[index].split("=");
tags[tag[0]] = tag.length > 1 ? tag[1] : true;
}
return [metric, tags];
};
const parseStatFields = function (statString) {
const result = {};
const splitStats = statString.split(".");
for (const index in splitStats) {
if (STAT_SCHEMA[index] === "metric") {
const [metric, tags] = parseMetricAndTags(splitStats[index]);
result.metric = metric;
result.tags = tags;
continue;
}
result[STAT_SCHEMA[index]] = splitStats[index];
}
return result;
};
// Ignore values that are either empty arrays or 0
const isEmptyValue = function (value, type) {
if (type === STATSD_TYPES.count && value === 0) {
return true;
}
if (type === STATSD_TYPES.timer && value.length === 0) {
return true;
}
return false;
};
// Extracts stats appropriately and returns an array of objects
const extractor = function (timestamp, stats, type) {
const results = [];
for (const statString in stats) {
if (
!stats.hasOwnProperty(statString) ||
IGNORED_STATSD_METRICS.indexOf(statString) !== -1 ||
statString.indexOf(".") === -1
)
continue;
if (isEmptyValue(stats[statString], type)) continue;
const stat = {
collected: new Date(timestamp * 1000).toISOString(),
type: type,
value: JSON.stringify(stats[statString]),
...parseStatFields(statString),
};
results.push(stat);
}
return results;
};
return {
init: async function (startup_time, config, events, logger) {
if (!pgPool) {
pgPool = await initConnectionPool(config);
}
events.on("flush", function (timestamp, statsdMetrics) {
let metrics = extractor(
timestamp,
statsdMetrics.counters,
STATSD_TYPES.count
);
metrics = metrics.concat(
extractor(timestamp, statsdMetrics.gauges, STATSD_TYPES.gauges)
);
metrics = metrics.concat(
extractor(timestamp, statsdMetrics.sets, STATSD_TYPES.sets)
);
metrics = metrics.concat(
extractor(timestamp, statsdMetrics.timers, STATSD_TYPES.timer)
);
insertMetrics(metrics);
elasticsearch.sendMetricsToElasticSearch(metrics);
});
events.on("status", function (callback) {
callback(null, "postgresBackend", null, null);
});
return true;
},
stop: function (callback) {
if (pgPool !== null) {
pgPool.end();
}
callback();
},
};
})();