-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
collector_set.js
152 lines (133 loc) · 4.85 KB
/
collector_set.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
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { snakeCase } from 'lodash';
import Promise from 'bluebird';
import { getCollectorLogger } from '../lib';
import { Collector } from './collector';
import { UsageCollector } from './usage_collector';
/*
* A collector object has types registered into it with the register(type)
* function. Each type that gets registered defines how to fetch its own data
* and optionally, how to combine it into a unified payload for bulk upload.
*/
export class CollectorSet {
/*
* @param {Object} server - server object
* @param {Array} collectors to initialize, usually as a result of filtering another CollectorSet instance
*/
constructor(server, collectors = []) {
this._log = getCollectorLogger(server);
this._collectors = collectors;
/*
* Helper Factory methods
* Define as instance properties to allow enclosing the server object
*/
this.makeStatsCollector = options => new Collector(server, options);
this.makeUsageCollector = options => new UsageCollector(server, options);
this._makeCollectorSetFromArray = collectorsArray => new CollectorSet(server, collectorsArray);
}
/*
* @param collector {Collector} collector object
*/
register(collector) {
// check instanceof
if (!(collector instanceof Collector)) {
throw new Error('CollectorSet can only have Collector instances registered');
}
this._collectors.push(collector);
if (collector.init) {
this._log.debug(`Initializing ${collector.type} collector`);
collector.init();
}
}
getCollectorByType(type) {
return this._collectors.find(c => c.type === type);
}
/*
* Call a bunch of fetch methods and then do them in bulk
* @param {CollectorSet} collectorSet - a set of collectors to fetch. Default to all registered collectors
*/
bulkFetch(callCluster, collectorSet = this) {
if (!(collectorSet instanceof CollectorSet)) {
throw new Error(`bulkFetch method given bad collectorSet parameter: ` + typeof collectorSet);
}
const fetchPromises = collectorSet.map(collector => {
const collectorType = collector.type;
this._log.debug(`Fetching data from ${collectorType} collector`);
return Promise.props({
type: collectorType,
result: collector.fetchInternal(callCluster) // use the wrapper for fetch, kicks in error checking
})
.catch(err => {
this._log.warn(err);
this._log.warn(`Unable to fetch data from ${collectorType} collector`);
});
});
return Promise.all(fetchPromises);
}
/*
* @return {new CollectorSet}
*/
getFilteredCollectorSet(filter) {
const filtered = this._collectors.filter(filter);
return this._makeCollectorSetFromArray(filtered);
}
async bulkFetchUsage(callCluster) {
const usageCollectors = this.getFilteredCollectorSet(c => c instanceof UsageCollector);
return this.bulkFetch(callCluster, usageCollectors);
}
// convert an array of fetched stats results into key/object
toObject(statsData) {
return statsData.reduce((accumulatedStats, { type, result }) => {
return {
...accumulatedStats,
[type]: result,
};
}, {});
}
// rename fields to use api conventions
toApiFieldNames(apiData) {
const getValueOrRecurse = value => {
if (value == null || typeof value !== 'object') {
return value;
} else {
return this.toApiFieldNames(value); // recurse
}
};
// handle array and return early, or return a reduced object
if (Array.isArray(apiData)) {
return apiData.map(getValueOrRecurse);
}
return Object.keys(apiData).reduce((accum, field) => {
const value = apiData[field];
let newName = field;
newName = snakeCase(newName);
newName = newName.replace(/^(1|5|15)_m/, '$1m'); // os.load.15m, os.load.5m, os.load.1m
newName = newName.replace('_in_bytes', '_bytes');
newName = newName.replace('_in_millis', '_ms');
return {
...accum,
[newName]: getValueOrRecurse(value),
};
}, {});
}
map(mapFn) {
return this._collectors.map(mapFn);
}
}