-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add some collector classes for objects that get registered in a CollectorSet #19098
Changes from 1 commit
f235925
c4b2902
958ec72
dec23ad
0aca12d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export class Collector { | ||
/* | ||
* @param {String} type.type - property name as the key for the data | ||
* @param {Function} type.init (optional) - initialization function | ||
* @param {Function} type.fetch - function to query data | ||
* @param {Function} type.cleanup (optional) - cleanup function | ||
* @param {Boolean} type.fetchAfterInit (optional) - if collector should fetch immediately after init | ||
*/ | ||
constructor({ type, init, fetch, cleanup, fetchAfterInit }) { | ||
this.type = type; | ||
this.init = init; | ||
this.fetch = fetch; | ||
this.cleanup = cleanup; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Weird blank line makes this extra field seem special. |
||
this.fetchAfterInit = fetchAfterInit; | ||
} | ||
|
||
/* | ||
* Allows using `server.log('debug', message)` as `this.log.debug(message)`. | ||
* Works for info and warn logs as well. | ||
*/ | ||
setLogger(logger) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could get this without making the |
||
this.log = logger; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import { flatten, isEmpty } from 'lodash'; | ||
import { LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG } from '../../../common/constants'; | ||
import Promise from 'bluebird'; | ||
import { UsageCollector } from './usage_collector'; | ||
|
||
const LOGGING_TAGS = [LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG]; | ||
|
||
|
@@ -52,13 +53,10 @@ export class CollectorSet { | |
} | ||
|
||
/* | ||
* @param {String} type.type | ||
* @param {Function} type.init (optional) | ||
* @param {Function} type.fetch | ||
* @param {Function} type.cleanup (optional) | ||
* @param {Collector} collector object | ||
*/ | ||
register(type) { | ||
this._collectors.push(type); | ||
register(collector) { | ||
this._collectors.push(collector); | ||
} | ||
|
||
/* | ||
|
@@ -139,6 +137,19 @@ export class CollectorSet { | |
}); | ||
} | ||
|
||
async bulkFetchUsage() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
const usageCollectors = this._collectors.filter(c => c instanceof UsageCollector); | ||
const bulk = await this._bulkFetch(usageCollectors); | ||
|
||
// summarize each type of stat | ||
return bulk.reduce((accumulatedStats, currentStat) => { | ||
return { | ||
...accumulatedStats, | ||
[currentStat.type]: currentStat.result, | ||
}; | ||
}, {}); | ||
} | ||
|
||
cleanup() { | ||
this._log.info(`Stopping all stats collectors`); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* 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 { Collector } from './collector'; | ||
|
||
export class UsageCollector extends Collector { | ||
constructor(properties) { | ||
super(properties); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,9 @@ | |
*/ | ||
|
||
import { get } from 'lodash'; | ||
|
||
import { XPACK_DEFAULT_ADMIN_EMAIL_UI_SETTING } from '../../../../../server/lib/constants'; | ||
import { KIBANA_SETTINGS_TYPE } from '../../../common/constants'; | ||
import { Collector } from '../classes/collector'; | ||
|
||
/* | ||
* Check if Cluster Alert email notifications is enabled in config | ||
|
@@ -58,36 +58,28 @@ export function getSettingsCollector(server) { | |
const { callWithInternalUser } = server.plugins.elasticsearch.getCluster('admin'); | ||
const config = server.config(); | ||
|
||
let _log; | ||
const setLogger = logger => { | ||
_log = logger; | ||
}; | ||
|
||
const fetch = async () => { | ||
let kibanaSettingsData; | ||
const defaultAdminEmail = await checkForEmailValue(config, callWithInternalUser); | ||
|
||
// skip everything if defaultAdminEmail === undefined | ||
if (defaultAdminEmail || (defaultAdminEmail === null && shouldUseNull)) { | ||
kibanaSettingsData = { | ||
xpack: { | ||
default_admin_email: defaultAdminEmail | ||
} | ||
}; | ||
_log.debug(`[${defaultAdminEmail}] default admin email setting found, sending [${KIBANA_SETTINGS_TYPE}] monitoring document.`); | ||
} else { | ||
_log.debug(`not sending [${KIBANA_SETTINGS_TYPE}] monitoring document because [${defaultAdminEmail}] is null or invalid.`); | ||
} | ||
|
||
// remember the current email so that we can mark it as successful if the bulk does not error out | ||
shouldUseNull = !!defaultAdminEmail; | ||
|
||
return kibanaSettingsData; | ||
}; | ||
|
||
return { | ||
return new Collector({ | ||
type: KIBANA_SETTINGS_TYPE, | ||
setLogger, | ||
fetch | ||
}; | ||
async fetch() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. had to move this function into the object literal for context to work |
||
let kibanaSettingsData; | ||
const defaultAdminEmail = await checkForEmailValue(config, callWithInternalUser); | ||
|
||
// skip everything if defaultAdminEmail === undefined | ||
if (defaultAdminEmail || (defaultAdminEmail === null && shouldUseNull)) { | ||
kibanaSettingsData = { | ||
xpack: { | ||
default_admin_email: defaultAdminEmail | ||
} | ||
}; | ||
this.log.debug(`[${defaultAdminEmail}] default admin email setting found, sending [${KIBANA_SETTINGS_TYPE}] monitoring document.`); | ||
} else { | ||
this.log.debug(`not sending [${KIBANA_SETTINGS_TYPE}] monitoring document because [${defaultAdminEmail}] is null or invalid.`); | ||
} | ||
|
||
// remember the current email so that we can mark it as successful if the bulk does not error out | ||
shouldUseNull = !!defaultAdminEmail; | ||
|
||
return kibanaSettingsData; | ||
} | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: all of these have an extraneous
type.
prefix now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we're adding a
server
param before this object in the args, so I'm keeping the prefix but calling itproperties
which matches what it's called inUsageCollector