-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
collector_set.ts
365 lines (323 loc) · 12.7 KB
/
collector_set.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { withTimeout } from '@kbn/std';
import { snakeCase } from 'lodash';
import type {
Logger,
ElasticsearchClient,
SavedObjectsClientContract,
KibanaExecutionContext,
ExecutionContextSetup,
} from '@kbn/core/server';
import { Collector } from './collector';
import type { ICollector, CollectorOptions, CollectorFetchContext } from './types';
import { UsageCollector, UsageCollectorOptions } from './usage_collector';
import { DEFAULT_MAXIMUM_WAIT_TIME_FOR_ALL_COLLECTORS_IN_S } from '../../common/constants';
import { createPerformanceObsHook, perfTimerify } from './measure_duration';
import { usageCollectorsStatsCollector } from './collector_stats';
const SECOND_IN_MS = 1000;
// Needed for the general array containing all the collectors. We don't really care about their types here
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type AnyCollector = ICollector<any, any>;
interface CollectorWithStatus {
isReadyWithTimeout: Awaited<ReturnType<typeof withTimeout>>;
collector: AnyCollector;
}
export interface CollectorSetConfig {
logger: Logger;
executionContext: ExecutionContextSetup;
maximumWaitTimeForAllCollectorsInS?: number;
collectors?: AnyCollector[];
}
export class CollectorSet {
private readonly logger: Logger;
private readonly executionContext: ExecutionContextSetup;
private readonly maximumWaitTimeForAllCollectorsInS: number;
private readonly collectors: Map<string, AnyCollector>;
constructor({
logger,
executionContext,
maximumWaitTimeForAllCollectorsInS = DEFAULT_MAXIMUM_WAIT_TIME_FOR_ALL_COLLECTORS_IN_S,
collectors = [],
}: CollectorSetConfig) {
this.logger = logger;
this.executionContext = executionContext;
this.collectors = new Map(collectors.map((collector) => [collector.type, collector]));
this.maximumWaitTimeForAllCollectorsInS = maximumWaitTimeForAllCollectorsInS;
}
/**
* Instantiates a stats collector with the definition provided in the options
* @param options Definition of the collector {@link CollectorOptions}
*/
public makeStatsCollector = <TFetchReturn, ExtraOptions extends object = {}>(
options: CollectorOptions<TFetchReturn, ExtraOptions>
) => {
return new Collector<TFetchReturn, ExtraOptions>(this.logger, options);
};
/**
* Instantiates an usage collector with the definition provided in the options
* @param options Definition of the collector {@link CollectorOptions}
*/
public makeUsageCollector = <TFetchReturn, ExtraOptions extends object = {}>(
options: UsageCollectorOptions<TFetchReturn, ExtraOptions>
) => {
return new UsageCollector<TFetchReturn, ExtraOptions>(this.logger, options);
};
/**
* Registers a collector to be used when collecting all the usage and stats data
* @param collector Collector to be added to the set (previously created via `makeUsageCollector` or `makeStatsCollector`)
*/
public registerCollector = <TFetchReturn, ExtraOptions extends object>(
collector: Collector<TFetchReturn, ExtraOptions>
) => {
// check instanceof
if (!(collector instanceof Collector)) {
throw new Error('CollectorSet can only have Collector instances registered');
}
if (this.collectors.get(collector.type)) {
throw new Error(`Usage collector's type "${collector.type}" is duplicated.`);
}
this.collectors.set(collector.type, collector);
};
public getCollectorByType = (type: string) => {
return [...this.collectors.values()].find((c) => c.type === type);
};
private getReadyCollectors = async (
collectors: Map<string, AnyCollector> = this.collectors
): Promise<{
readyCollectors: AnyCollector[];
nonReadyCollectorTypes: string[];
timedOutCollectorsTypes: string[];
}> => {
if (!(collectors instanceof Map)) {
throw new Error(
`getReadyCollectors method given bad Map of collectors: ` + typeof collectors
);
}
const timeoutMs = this.maximumWaitTimeForAllCollectorsInS * SECOND_IN_MS;
const collectorsWithStatus: CollectorWithStatus[] = await Promise.all(
[...collectors.values()].map(async (collector) => {
const wrappedPromise = perfTimerify(
`is_ready_${collector.type}`,
async (): Promise<boolean> => {
try {
return await collector.isReady();
} catch (err) {
this.logger.debug(`Collector ${collector.type} failed to get ready. ${err}`);
return false;
}
}
);
const isReadyWithTimeout = await withTimeout<boolean>({
promise: wrappedPromise(),
timeoutMs,
});
if (isReadyWithTimeout.timedout) {
return { isReadyWithTimeout, collector };
}
return {
isReadyWithTimeout: {
value: isReadyWithTimeout.value,
timedout: isReadyWithTimeout.timedout,
},
collector,
};
})
);
const timedOutCollectorsTypes = collectorsWithStatus
.filter((collectorWithStatus) => collectorWithStatus.isReadyWithTimeout.timedout)
.map(({ collector }) => collector.type);
if (timedOutCollectorsTypes.length) {
this.logger.debug(
`Some collectors timedout getting ready (${timedOutCollectorsTypes.join(', ')}). ` +
`Waited for ${this.maximumWaitTimeForAllCollectorsInS}s and will return data from collectors that are ready.`
);
}
const nonTimedOutCollectors = collectorsWithStatus.filter(
(
collectorWithStatus
): collectorWithStatus is {
isReadyWithTimeout: { timedout: false; value: boolean };
collector: AnyCollector;
} => collectorWithStatus.isReadyWithTimeout.timedout === false
);
const collectorsTypesNotReady = nonTimedOutCollectors
.filter(({ isReadyWithTimeout }) => isReadyWithTimeout.value === false)
.map(({ collector }) => collector.type);
if (collectorsTypesNotReady.length) {
this.logger.debug(
`Some collectors are not ready (${collectorsTypesNotReady.join(',')}). ` +
`will return data from all collectors that are ready.`
);
}
const readyCollectors = nonTimedOutCollectors
.filter(({ isReadyWithTimeout }) => isReadyWithTimeout.value === true)
.map(({ collector }) => collector);
return {
readyCollectors,
nonReadyCollectorTypes: collectorsTypesNotReady,
timedOutCollectorsTypes,
};
};
private fetchCollector = async (
collector: AnyCollector,
context: CollectorFetchContext
): Promise<{
result?: unknown;
status: 'failed' | 'success';
type: string;
}> => {
const { type } = collector;
this.logger.debug(`Fetching data from ${type} collector`);
const executionContext: KibanaExecutionContext = {
type: 'usage_collection',
name: 'collector.fetch',
id: type,
description: `Fetch method in the Collector "${type}"`,
};
try {
const result = await this.executionContext.withContext(executionContext, () =>
collector.fetch(context)
);
return { type, result, status: 'success' as const };
} catch (err) {
this.logger.warn(err);
this.logger.warn(`Unable to fetch data from ${type} collector`);
return { type, status: 'failed' as const };
}
};
public bulkFetch = async (
esClient: ElasticsearchClient,
soClient: SavedObjectsClientContract,
collectors: Map<string, AnyCollector> = this.collectors
) => {
this.logger.debug(`Getting ready collectors`);
const getMarks = createPerformanceObsHook();
const { readyCollectors, nonReadyCollectorTypes, timedOutCollectorsTypes } =
await this.getReadyCollectors(collectors);
// freeze object to prevent collectors from mutating it.
const context = Object.freeze({ esClient, soClient });
const fetchExecutions = await Promise.all(
readyCollectors.map(async (collector) => {
const wrappedPromise = perfTimerify(
`fetch_${collector.type}`,
async () => await this.fetchCollector(collector, context)
);
return await wrappedPromise();
})
);
const durationMarks = getMarks();
const isReadyExecutionDurationByType = [
...readyCollectors.map(({ type }) => {
// should always find a duration, fallback to 0 in case something unexpected happened
const duration = durationMarks[`is_ready_${type}`] || 0;
return { duration, type };
}),
...nonReadyCollectorTypes.map((type) => {
// should always find a duration, fallback to 0 in case something unexpected happened
const duration = durationMarks[`is_ready_${type}`] || 0;
return { duration, type };
}),
...timedOutCollectorsTypes.map((type) => {
const timeoutMs = this.maximumWaitTimeForAllCollectorsInS * SECOND_IN_MS;
// if undefined default to timeoutMs since the collector timedout
const duration = durationMarks[`is_ready_${type}`] || timeoutMs;
return { duration, type };
}),
];
const fetchExecutionDurationByType = fetchExecutions.map(({ type, status }) => {
// should always find a duration, fallback to 0 in case something unexpected happened
const duration = durationMarks[`fetch_${type}`] || 0;
return { duration, type, status };
});
const usageCollectorStats = usageCollectorsStatsCollector(
// pass `this` as `usageCollection` to the collector to mimic
// registering a collector via usageCollection.SetupContract
this,
{
// isReady stats
nonReadyCollectorTypes,
timedOutCollectorsTypes,
isReadyExecutionDurationByType,
// fetch stats
fetchExecutionDurationByType,
}
);
return [
...fetchExecutions
// pluck type and result from collector object
.map(({ type, result }) => ({ type, result }))
// only keep data of collectors thar returned a result
.filter(
(response): response is { type: string; result: unknown } =>
typeof response?.result !== 'undefined'
),
// Treat collector stats as just another "collector"
{ type: usageCollectorStats.type, result: usageCollectorStats.fetch(context) },
];
};
/*
* @return {new CollectorSet}
*/
private getFilteredCollectorSet = (filter: (col: AnyCollector) => boolean) => {
const filtered = [...this.collectors.values()].filter(filter);
return this.makeCollectorSetFromArray(filtered);
};
public bulkFetchUsage = async (
esClient: ElasticsearchClient,
savedObjectsClient: SavedObjectsClientContract
) => {
const usageCollectors = this.getFilteredCollectorSet((c) => c instanceof UsageCollector);
return await this.bulkFetch(esClient, savedObjectsClient, usageCollectors.collectors);
};
/**
* Convert an array of fetched stats results into key/object
* @param statsData Array of fetched stats results
*/
public toObject = <Result extends Record<string, unknown>, T = unknown>(
statsData: Array<{ type: string; result: T }> = []
): Result => {
return Object.fromEntries(statsData.map(({ type, result }) => [type, result])) as Result;
};
/**
* Rename fields to use API conventions
* @param apiData Data to be normalized
*/
public toApiFieldNames = (
apiData: Record<string, unknown> | unknown[]
): Record<string, unknown> | unknown[] => {
// handle array and return early, or return a reduced object
if (Array.isArray(apiData)) {
return apiData.map((value) => this.getValueOrRecurse(value));
}
return Object.fromEntries(
Object.entries(apiData).map(([field, value]) => {
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 [newName, this.getValueOrRecurse(value)];
})
);
};
private getValueOrRecurse = (value: unknown) => {
if (Array.isArray(value) || (typeof value === 'object' && value !== null)) {
return this.toApiFieldNames(value as Record<string, unknown> | unknown[]); // recurse
}
return value;
};
private makeCollectorSetFromArray = (collectors: AnyCollector[]) => {
return new CollectorSet({
logger: this.logger,
executionContext: this.executionContext,
maximumWaitTimeForAllCollectorsInS: this.maximumWaitTimeForAllCollectorsInS,
collectors,
});
};
}