Skip to content

Commit

Permalink
add a helper logger function so collector has access to logger at con…
Browse files Browse the repository at this point in the history
…struction
  • Loading branch information
tsullivan committed May 18, 2018
1 parent dec23ad commit 0aca12d
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('CollectorSet', () => {
onPayload: identity
});

collectors.register(new Collector({
collectors.register(new Collector(server, {
type: 'type_collector_test',
fetchAfterInit: true,
init,
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('CollectorSet', () => {
});

fetch = () => ({ testFetch: true });
collectors.register(new Collector({
collectors.register(new Collector(server, {
type: 'type_collector_test',
fetchAfterInit: true,
init,
Expand Down Expand Up @@ -143,7 +143,7 @@ describe('CollectorSet', () => {
onPayload: identity
});

collectors.register(new Collector({
collectors.register(new Collector(server, {
type: 'type_collector_test',
fetchAfterInit: true,
init,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,24 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { getCollectorLogger } from '../lib';

export class Collector {
/*
* @param {String} type - property name as the key for the data
* @param {Function} init (optional) - initialization function
* @param {Function} fetch - function to query data
* @param {Function} cleanup (optional) - cleanup function
* @param {Boolean} fetchAfterInit (optional) - if collector should fetch immediately after init
* @param {Object} server - server object
* @param {String} properties.type - property name as the key for the data
* @param {Function} properties.init (optional) - initialization function
* @param {Function} properties.fetch - function to query data
* @param {Function} properties.cleanup (optional) - cleanup function
* @param {Boolean} properties.fetchAfterInit (optional) - if collector should fetch immediately after init
*/
constructor({ type, init, fetch, cleanup, fetchAfterInit }) {
constructor(server, { type, init, fetch, cleanup, fetchAfterInit }) {
this.type = type;
this.init = init;
this.fetch = fetch;
this.cleanup = cleanup;

this.fetchAfterInit = fetchAfterInit;
}

/*
* Allows using `server.log('debug', message)` as `this.log.debug(message)`.
* Works for info and warn logs as well.
*/
setLogger(logger) {
this.log = logger;
this.log = getCollectorLogger(server);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
*/

import { flatten, isEmpty } from 'lodash';
import { LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG } from '../../../common/constants';
import Promise from 'bluebird';
import { getCollectorLogger } from '../lib';
import { Collector } from './collector';
import { UsageCollector } from './usage_collector';

const LOGGING_TAGS = [LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG];

/*
* 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
Expand All @@ -20,10 +18,10 @@ const LOGGING_TAGS = [LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG];
export class CollectorSet {

/*
* @param server {Object} server object
* @param options.interval {Number} in milliseconds
* @param options.combineTypes {Function}
* @param options.onPayload {Function}
* @param {Object} server - server object
* @param {Number} options.interval - in milliseconds
* @param {Function} options.combineTypes
* @param {Function} options.onPayload
*/
constructor(server, { interval, combineTypes, onPayload }) {
this._collectors = [];
Expand All @@ -39,26 +37,21 @@ export class CollectorSet {
throw new Error('onPayload function is required');
}

this._log = {
debug: message => server.log(['debug', ...LOGGING_TAGS], message),
info: message => server.log(['info', ...LOGGING_TAGS], message),
warn: message => server.log(['warning', ...LOGGING_TAGS], message)
};
this._log = getCollectorLogger(server);

this._interval = interval;
this._combineTypes = combineTypes;
this._onPayload = onPayload;
}

/*
* @param {Collector} collector object
* @param collector {Collector} collector object
*/
register(collector) {
// check instanceof
if (!(collector instanceof Collector)) {
throw new Error('CollectorSet can only have Collector instances registered');
}
collector.setLogger(this._log);
this._collectors.push(collector);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { Collector } from './collector';

export class UsageCollector extends Collector {
constructor(properties) {
super(properties);
constructor(server, properties) {
super(server, properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ describe('getKibanaUsageCollector', () => {
getCluster: sinon.stub()
}
},
config: () => ({ get: sinon.stub() })
config: () => ({ get: sinon.stub() }),
log: sinon.stub(),
};
serverStub.plugins.elasticsearch.getCluster.withArgs('admin').returns(clusterStub);
callClusterStub = callClusterFactory(serverStub).getCallClusterInternal();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const TYPES = [
* Fetches saved object client counts by querying the saved object index
*/
export function getKibanaUsageCollector(server, callCluster) {
return new UsageCollector({
return new UsageCollector(server, {
type: KIBANA_USAGE_TYPE,
async fetch() {
const index = server.config().get('kibana.index');
Expand Down Expand Up @@ -53,9 +53,7 @@ export function getKibanaUsageCollector(server, callCluster) {

return {
index,

// combine the bucketCounts and 0s for types that don't have documents
...TYPES.reduce((acc, type) => ({
...TYPES.reduce((acc, type) => ({ // combine the bucketCounts and 0s for types that don't have documents
...acc,
[snakeCase(type)]: {
total: bucketCounts[type] || 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function getOpsStatsCollector(server) {
}, 5 * 1000); // wait 5 seconds to avoid race condition with reloading logging configuration
});

return new Collector({
return new Collector(server, {
type: KIBANA_STATS_TYPE,
init,
fetch: buffer.flush,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

import { KIBANA_REPORTING_TYPE } from '../../../common/constants';
import { getReportingUsage } from '../../../../reporting';
import { UsageCollector } from '../';

export function getReportingUsageCollector(server, callCluster) {
return {
return new UsageCollector(server, {
type: KIBANA_REPORTING_TYPE,
fetch() {
return getReportingUsage(callCluster, server);
}
};
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function getSettingsCollector(server) {
const { callWithInternalUser } = server.plugins.elasticsearch.getCluster('admin');
const config = server.config();

return new Collector({
return new Collector(server, {
type: KIBANA_SETTINGS_TYPE,
async fetch() {
let kibanaSettingsData;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 { LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG } from '../../../common/constants';

const LOGGING_TAGS = [LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG];

/*
* @param {Object} server
* @return {Object} helpful logger object
*/
export function getCollectorLogger(server) {
return {
debug: message => server.log(['debug', ...LOGGING_TAGS], message),
info: message => server.log(['info', ...LOGGING_TAGS], message),
warn: message => server.log(['warning', ...LOGGING_TAGS], message)
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* 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 { getCollectorLogger } from './get_collector_logger';

0 comments on commit 0aca12d

Please sign in to comment.