/
telemetry.js
216 lines (188 loc) · 7.2 KB
/
telemetry.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
var util = require("util");
var os = require("os");
try {
var geoip = require('geoip');
var country = new geoip.Country(process.env.TELEMETRY_GEOIP_DATABASE || __dirname + '/GeoIP.dat');
} catch(e) { var geoup = null; }
var telemetry = exports = module.exports = function(config) {
/**
* The inputs that are listening on this server
*/
this.inputs = {};
/**
* Show the current status of the node
*/
this.get_status = function(req, res, next) {
res.send({
status: this.status,
startup: process.env.startUp,
errors: this.errors,
last_error: this.last_error || undefined,
inputs: Object.keys(this.inputs)
});
return true;
};
/**
* Add metadata to an incoming transaction for debugging purposes
*/
this.add_metadata = function(transaction, req, bulk_loaded) {
if (this.log_metadata === true) {
transaction.metadata = {};
transaction.metadata['user-agent'] = req.headers['user-agent'] || "";
transaction.metadata['x-forwarded-for'] = req.headers['x-forwarded-for'] || "";
transaction.metadata.ip = req.connection.remoteAddress;
transaction.metadata.bulk_loaded = bulk_loaded;
// Geocode IP address and attach to request
try {
if (geoip !== null && country !== undefined) {
var ips = [ req.connection.remoteAddress ];
if (req.headers['x-forwarded-for'] !== undefined) {
ips = ips.concat(req.headers['x-forwarded-for'].split(","));
}
for (var i = 0; i < ips.length; i++) {
if (! transaction.metadata.country) {
var lookup = country.lookupSync(ips[i]);
if (lookup !== null) {
transaction.metadata.country = lookup.country_code;
}
}
}
}
} catch (e) { console.log(e.message); }
}
return transaction;
};
/**
* Notify all listeners of a new message on an input
* Called by post_message and bulk_load
*/
this.process_message = function(input, data) {
// Notify listeners of new data
if (this.inputs[input] === undefined) return;
for (var i = 0; i < this.inputs[input].length; i++) {
this.inputs[input][i].post(data);
}
};
/**
* Post an individual message to an input
*/
this.post_message = function(req, res, next) {
var input = req.params.input;
if (this.inputs[input] === undefined) {
// Bad input, send 404
res.send({ error: 'The input you specified could not be found.' }, 404);
return;
}
if (req.body === undefined) {
// Bad input, send 404
res.send({ error: 'No body was received by telemetry. Please ensure you are sending a Content-Type header.' }, 400);
return;
}
if (this.inputs[input].length === 0) {
// No listeners, send 500
res.send({ error: 'There are no listeners on this input.' }, 500);
return;
}
// Optionally include request metadata
var data = req.body;
if (this.log_metadata === true) {
data = this.add_metadata(data, req, false);
}
this.process_message(input, data);
// Send success
res.send('', 200);
};
/**
* Bulk load data and collate by input
*/
this.bulk_load = function(req, res, next) {
if (req.body === undefined || req.body.data === undefined) {
// Bad request, send "Unsupported Media Type"
res.send({ error: 'Transactions must be in a JSON array called data in order to be processed.' }, 415);
return;
}
// Process as much of the incoming data as possible
var records = req.body.data.length;
var data = req.body.data.slice(0);
var processed = 0;
for (var i = 0; i < data.length; i++) {
var transaction = data[i];
var error = false;
// Input must be specified for each transaction
if (transaction.input === undefined) {
error = true;
}
// Ensure that input is valid
if (this.inputs[transaction.input] === undefined) {
error = true;
}
// Optionally include request metadata on each transaction
if (this.log_metadata === true) {
transaction = this.add_metadata(transaction, req, false);
}
// If there are no errors, process the transaction
if (error === false) {
processed++;
this.process_message(transaction.input, transaction);
}
}
// Send results of bulk load
res.send({
processed: processed,
errors: records - processed
}, 200);
};
/**
* Log error to telemetry_errors input if defined
* @message {Object} The message to be logged
* @options {Object} Logging options
*
* Available options:
* - error {Boolean} whether or not to include a stack trace
*/
this.log = function(message, options) {
// Create logging payload
var data = {
time: new Date(),
message: message,
host: options && options.host ? options.host : undefined,
freemem: os.freemem(),
loadavg: os.loadavg()
};
// Optionally include a stack trace
if (options && options.error !== undefined) {
data.trace = util.inspect(new Error().stack);
}
// Notify the system of the error
this.process_message("telemetry_errors", data);
};
this.error = function(message, options) {
if (! options) options = {};
options.error = true;
this.log(message, options);
};
/**
* Read in configuration and set up inputs
* @param config
*/
// Load inputs
for (var i in config) {
if (config.hasOwnProperty(i)) {
this.inputs[i] = [];
// Initialize listeners for this input
for (var j = 0; j < config[i].length; j++) {
try {
var listener_config = config[i][j];
listener_config.input = i;
// Initialize class defined in config
var listener = new (global[config[i][j].type])(listener_config);
// Add listener to input
this.inputs[i].push(listener);
} catch (e) {
// If listener failed to load, log error and continue initializing
this.error("Could not load " + config[i][j].type + "listener [" + e.message + "]");
}
}
}
}
};