-
Notifications
You must be signed in to change notification settings - Fork 8k
/
collector_set.js
168 lines (140 loc) · 5.41 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { identity, noop } from 'lodash';
import sinon from 'sinon';
import expect from 'expect.js';
import { Collector } from '../collector';
import { CollectorSet } from '../collector_set';
const DEBUG_LOG = [ 'debug', 'monitoring-ui', 'kibana-monitoring' ];
const INFO_LOG = [ 'info', 'monitoring-ui', 'kibana-monitoring' ];
const COLLECTOR_INTERVAL = 10000;
const CHECK_DELAY = 100; // can be lower than COLLECTOR_INTERVAL because the collectors use fetchAfterInit
describe('CollectorSet', () => {
describe('registers a collector set and runs lifecycle events', () => {
let server;
let init;
let cleanup;
let fetch;
beforeEach(() => {
server = {
log: sinon.spy()
};
init = noop;
cleanup = noop;
fetch = noop;
});
it('should throw an error if non-Collector type of object is registered', () => {
const collectors = new CollectorSet(server, {
interval: COLLECTOR_INTERVAL,
combineTypes: identity,
onPayload: identity
});
const registerPojo = () => {
collectors.register({
type: 'type_collector_test',
fetchAfterInit: true,
init,
fetch,
cleanup
});
};
expect(registerPojo).to.throwException(({ message }) => {
expect(message).to.be('CollectorSet can only have Collector instances registered');
});
});
it('should skip bulk upload if payload is empty', (done) => {
const collectors = new CollectorSet(server, {
interval: COLLECTOR_INTERVAL,
combineTypes: identity,
onPayload: identity
});
collectors.register(new Collector(server, {
type: 'type_collector_test',
fetchAfterInit: true,
init,
fetch,
cleanup
}));
collectors.start();
// allow interval to tick a few times
setTimeout(() => {
collectors.cleanup();
expect(server.log.calledWith(INFO_LOG, 'Starting all stats collectors')).to.be(true);
expect(server.log.calledWith(DEBUG_LOG, 'Initializing type_collector_test collector')).to.be(true);
expect(server.log.calledWith(DEBUG_LOG, 'Fetching data from type_collector_test collector')).to.be(true);
expect(server.log.calledWith(DEBUG_LOG, 'Skipping bulk uploading of an empty stats payload')).to.be(true); // proof of skip
expect(server.log.calledWith(INFO_LOG, 'Stopping all stats collectors')).to.be(true);
expect(server.log.calledWith(DEBUG_LOG, 'Running type_collector_test cleanup')).to.be(true);
done(); // for async exit
}, CHECK_DELAY);
});
it('should run the bulk upload handler', (done) => {
const combineTypes = sinon.spy(data => {
return [
data[0][0],
{ ...data[0][1], combined: true }
];
});
const onPayload = sinon.spy();
const collectors = new CollectorSet(server, {
interval: COLLECTOR_INTERVAL,
combineTypes,
onPayload
});
fetch = () => ({ testFetch: true });
collectors.register(new Collector(server, {
type: 'type_collector_test',
fetchAfterInit: true,
init,
fetch,
cleanup
}));
collectors.start();
// allow interval to tick a few times
setTimeout(() => {
collectors.cleanup();
expect(server.log.calledWith(INFO_LOG, 'Starting all stats collectors')).to.be(true);
expect(server.log.calledWith(DEBUG_LOG, 'Initializing type_collector_test collector')).to.be(true);
expect(server.log.calledWith(DEBUG_LOG, 'Fetching data from type_collector_test collector')).to.be(true);
expect(server.log.calledWith(DEBUG_LOG, 'Uploading bulk stats payload to the local cluster')).to.be(true);
expect(server.log.calledWith(INFO_LOG, 'Stopping all stats collectors')).to.be(true);
expect(server.log.calledWith(DEBUG_LOG, 'Running type_collector_test cleanup')).to.be(true);
// un-flattened
expect(combineTypes.getCall(0).args[0]).to.eql(
[ [ { index: { _type: 'type_collector_test' } }, { testFetch: true } ] ]
);
// flattened and altered
expect(onPayload.getCall(0).args[0]).to.eql(
[ { index: { _type: 'type_collector_test' } }, { testFetch: true, combined: true } ]
);
done(); // for async exit
}, CHECK_DELAY);
});
it('should log the info-level status of stopping and restarting', (done) => {
const collectors = new CollectorSet(server, {
interval: COLLECTOR_INTERVAL,
combineTypes: identity,
onPayload: identity
});
collectors.register(new Collector(server, {
type: 'type_collector_test',
fetchAfterInit: true,
init,
fetch,
cleanup
}));
collectors.start();
expect(server.log.calledWith(INFO_LOG, 'Starting all stats collectors')).to.be(true);
collectors.cleanup();
expect(server.log.calledWith(INFO_LOG, 'Stopping all stats collectors')).to.be(true);
collectors.start();
expect(server.log.calledWith(INFO_LOG, 'Starting all stats collectors')).to.be(true);
// exit
collectors.cleanup();
done();
});
});
});