-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
262 lines (213 loc) · 6.73 KB
/
index.ts
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
import objectAssign = require('object-assign');
import chalk = require('chalk');
import request = require('request');
import util = require('util');
var circular = require('circular');
const kSystemHostname: string = require('os').hostname();
const kEnvironmentName: string = process.env.NODE_ENV || 'dev';
var opts = null;
function prettyStack(stack) {
var lines = stack.split('\n');
lines.shift();
return lines.map(function (line) {
return line.indexOf('/node_modules/') > -1 || line.indexOf('(native)') > -1 ? chalk.black(line) : chalk.gray(line);
}).join('\n');
}
// method to write directly to the console for local logging
function writeLocal(level, data) {
if (level === 'error') {
console.error(chalk.red(util.inspect(data)));
} else if (level === 'warning') {
console.error(chalk.yellow(util.inspect(data)));
} else if (level === 'info') {
console.log(chalk.cyan(util.inspect(data)));
} else {
console.log(chalk.white(util.inspect(data)));
}
if (data && data.stack) {
console.log(prettyStack(data.stack));
} else if (data && data.exception && data.exception.stack) {
console.log(prettyStack(data.exception.stack));
}
}
// method to attach constants to every trace and write
function formatData(data) {
// basic clone of data so we don't attach system and hostname to original obj
const formatted = objectAssign({}, data, {
timestamp: new Date().toString(),
system: opts.sysIdent,
hostname: kSystemHostname,
env: kEnvironmentName,
});
return formatted;
}
namespace logger {
export class Transaction {
id: string = require('uuid').v4();
type: string;
parent: string;
private _startTime = new Date().getTime();
constructor(type: string, parent?: string | Transaction) {
this.type = type;
// support for passing in the parent transaction directly rather than needing the ID
if (typeof parent === 'string') {
this.parent = parent;
} else if (parent && parent.id) {
this.parent = parent.id;
}
}
data: any;
setData(data) {
this.data = data;
}
/** duration */
time: number;
end() {
const endTime = new Date().getTime();
this.time = endTime - this._startTime;
delete this._startTime;
logger.trace(this);
}
write(level: string, data: any) {
data.transaction = this.id;
logger.write(level, data);
}
factory(type: string, parent?: string | Transaction) {
return new Transaction(type, parent);
}
promise<T, P extends PromiseLike<T>>(promise: P): P {
return promise.then(result => {
this.end();
return result; // don't swallow
}, error => {
this.write(error.level || 'error', error);
this.end();
throw error; // throw it back
}) as P;
}
}
export function init(sysIdent, base, key) {
opts = {
sysIdent: sysIdent,
base: base,
key: key,
};
}
export function setWriteLocalEnabled(bool: boolean) {
opts.writeLocal = bool || false;
}
export function setTraceLocalEnabled(bool: boolean) {
opts.traceLocal = bool || false;
}
export function write(level: string, data, done?: Function) {
if (!opts) return;
const obj = {
level,
data,
transaction: data.transaction || void 0,
};
const formatted = formatData(obj);
if (opts.writeLocal) writeLocal(level, formatted);
logger.commit('log', formatted, done);
}
export function trace(transaction) {
if (!opts) return;
transaction = formatData(transaction);
logger.commit('transaction', transaction);
if (opts.traceLocal) writeLocal('trace', transaction);
}
export function commit(type: string, obj, done?: Function) {
request({
url: opts.base + '/' + type + '?key=' + opts.key,
method: 'POST',
body: JSON.stringify(obj, circular()),
headers: {
'content-type': 'application/json',
},
}, function (err/*, response, data*/) {
if (done) done(err);
});
}
export function createTransaction(type: string, parent?: string | Transaction) {
return new Transaction(type, parent);
}
export function express(req, res, next) {
var parentTransactionID = null;
if (req.headers['x-parent-transaction']) {
parentTransactionID = req.headers['x-parent-transaction'];
}
req.transaction = logger.createTransaction('express', parentTransactionID);
req.logger = {
write(level, data) {
var object = {};
// basic clone of data so we don't attach these props to the obj
for (var k in data) {
object[k] = data[k];
}
req.transaction.write(level, object);
},
};
res.on('finish', function () {
req.transaction.setData({
request: {
route: (req.route) ? req.route.path : '',
method: req.method,
url: req.url,
headers: req.headers,
params: req.params,
query: req.query,
body: req.body,
},
response: {
status: res.statusCode,
},
});
req.transaction.end();
});
res.setHeader('X-FC-Transaction', req.transaction.id);
next();
}
let _didWarnForRabbitr = false;
export function rabbitr() {
if (!_didWarnForRabbitr) {
console.warn(new Error('You\'re using the old way of hooking FC with rabbitr.'));
_didWarnForRabbitr = true;
}
return require('./rabbitr').middleware.apply(null, Array.prototype.slice.call(arguments));
}
export interface AsyncFunction { (...args): PromiseLike<any>; }
/**
* Wrap a function that returns a promise with a transaction.
*
* Returns a function that's identical to the wrapped one. You can also pass parent transactions
* in the context (`this` object) as `this.transaction`.
*/
export function wrapAsync<Fn extends AsyncFunction>(name: string, fn: Fn): Fn {
return function (...args) {
const transaction = new Transaction(name, this && this.transaction);
const promise = fn(...args);
return promise && promise.then ? promise.then(result => {
transaction.setData({ result, fulfilled: true });
transaction.end();
return result;
}, error => {
transaction.setData({ fulfilled: false });
transaction.write(error && error.level || 'error', error);
transaction.end();
throw error;
}) : promise;
} as Fn;
}
}
process.on('uncaughtException', function (err) {
if (err.name === 'SyntaxError') throw err;
writeLocal('error', {
exception: err,
});
process.exit(1);
});
process.on('unhandledRejection', (reason, p) => {
writeLocal('warning', reason);
});
console.log('Added generic exception handler for FlightControl logger\n');
export = logger;