-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwf-api.js
113 lines (98 loc) · 3.03 KB
/
wf-api.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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright (c) 2014, Joyent, Inc.
*/
var path = require('path'),
EffluentLogger = require('effluent-logger'),
fs = require('fs'),
util = require('util'),
http = require('http'),
https = require('https'),
wf = require('wf'),
config_file = path.resolve(__dirname, 'etc/config.json'),
config,
api,
log,
retries = 0,
MAX_RETRIES;
function addFluentdHost(log, host) {
var evtLogger = new EffluentLogger({
filter: function _evtFilter(obj) { return (!!obj.evt); },
host: host,
log: log,
port: 24224,
tag: 'debug'
});
log.addStream({
stream: evtLogger,
type: 'raw'
});
}
fs.readFile(config_file, 'utf8', function (err, data) {
if (err) {
console.error('Error reading config file:');
console.dir(err);
process.exit(1);
} else {
try {
config = JSON.parse(data);
} catch (e) {
console.error('Error parsing config file JSON:');
console.dir(e);
process.exit(1);
}
if (typeof (config.maxHttpSockets) === 'number') {
console.log('Tuning max sockets to %d', config.maxHttpSockets);
http.globalAgent.maxSockets = config.maxHttpSockets;
https.globalAgent.maxSockets = config.maxHttpSockets;
}
config.logger = {
streams: [ {
level: config.logLevel || 'info',
stream: process.stdout
}]
};
config.metrics = {
datacenterName: config.datacenterName,
serviceName: config.serviceName,
instanceUuid: config.instanceUuid,
serverUuid: config.serverUuid,
adminIp: config.adminIp
};
api = wf.API(config);
log = api.log;
// EXPERIMENTAL
if (config.fluentd_host) {
addFluentdHost(log, config.fluentd_host);
}
var init = function (cb) {
api.init(function onInit() {
return cb();
}, function onError(err) {
if (err) {
log.error(err);
}
});
};
init(function () {
log.info('API server up and running!');
});
// Setup a logger on HTTP Agent queueing
setInterval(function () {
var agent = http.globalAgent;
if (agent.requests && agent.requests.length > 0) {
log.warn('http.globalAgent queueing, depth=%d',
agent.requests.length);
}
agent = https.globalAgent;
if (agent.requests && agent.requests.length > 0) {
log.warn('https.globalAgent queueing, depth=%d',
agent.requests.length);
}
}, 1000);
}
});