Skip to content

Commit

Permalink
Ensure we query off MB for new api calls as well
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisronline committed Sep 11, 2020
1 parent 00b6838 commit 37a6cef
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 17 deletions.
14 changes: 12 additions & 2 deletions x-pack/plugins/monitoring/server/alerts/base_alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ILegacyCustomClusterClient,
Logger,
IUiSettingsClient,
LegacyCallAPIOptions,
} from 'kibana/server';
import { i18n } from '@kbn/i18n';
import {
Expand Down Expand Up @@ -36,6 +37,8 @@ import { MonitoringConfig } from '../config';
import { AlertSeverity } from '../../common/enums';
import { CommonAlertFilter, CommonAlertParams, CommonBaseAlert } from '../../common/types';
import { MonitoringLicenseService } from '../types';
import { mbSafeQuery } from '../lib/mb_safe_query';
import { appendMetricbeatIndex } from '../lib/alerts/append_mb_index';

export class BaseAlert {
public type!: string;
Expand Down Expand Up @@ -212,13 +215,20 @@ export class BaseAlert {
`Executing alert with params: ${JSON.stringify(params)} and state: ${JSON.stringify(state)}`
);

const callCluster = this.monitoringCluster
const _callCluster = this.monitoringCluster
? this.monitoringCluster.callAsInternalUser
: services.callCluster;
const callCluster = async (
endpoint: string,
clientParams?: Record<string, unknown>,
options?: LegacyCallAPIOptions
) => {
return await mbSafeQuery(async () => _callCluster(endpoint, clientParams, options));
};
const availableCcs = this.config.ui.ccs.enabled ? await fetchAvailableCcs(callCluster) : [];
// Support CCS use cases by querying to find available remote clusters
// and then adding those to the index pattern we are searching against
let esIndexPattern = INDEX_PATTERN_ELASTICSEARCH;
let esIndexPattern = appendMetricbeatIndex(this.config, INDEX_PATTERN_ELASTICSEARCH);
if (availableCcs) {
esIndexPattern = getCcsIndexPattern(esIndexPattern, availableCcs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ describe('ClusterHealthAlert', () => {
});
const monitoringCluster = null;
const config = {
ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } },
ui: {
ccs: { enabled: true },
container: { elasticsearch: { enabled: false } },
metricbeat: { index: 'metricbeat-*' },
},
};
const kibanaUrl = 'http://localhost:5601';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ describe('CpuUsageAlert', () => {
});
const monitoringCluster = null;
const config = {
ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } },
ui: {
ccs: { enabled: true },
container: { elasticsearch: { enabled: false } },
metricbeat: { index: 'metricbeat-*' },
},
};
const kibanaUrl = 'http://localhost:5601';

Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
CommonAlertParams,
CommonAlertParamDetail,
} from '../../common/types';
import { appendMetricbeatIndex } from '../lib/alerts/append_mb_index';

const RESOLVED = i18n.translate('xpack.monitoring.alerts.cpuUsage.resolved', {
defaultMessage: 'resolved',
Expand Down Expand Up @@ -137,7 +138,7 @@ export class CpuUsageAlert extends BaseAlert {
uiSettings: IUiSettingsClient,
availableCcs: string[]
): Promise<AlertData[]> {
let esIndexPattern = INDEX_PATTERN_ELASTICSEARCH;
let esIndexPattern = appendMetricbeatIndex(this.config, INDEX_PATTERN_ELASTICSEARCH);
if (availableCcs) {
esIndexPattern = getCcsIndexPattern(esIndexPattern, availableCcs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ describe('ElasticsearchVersionMismatchAlert', () => {
});
const monitoringCluster = null;
const config = {
ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } },
ui: {
ccs: { enabled: true },
container: { elasticsearch: { enabled: false } },
metricbeat: { index: 'metricbeat-*' },
},
};
const kibanaUrl = 'http://localhost:5601';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ describe('KibanaVersionMismatchAlert', () => {
});
const monitoringCluster = null;
const config = {
ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } },
ui: {
ccs: { enabled: true },
container: { elasticsearch: { enabled: false } },
metricbeat: { index: 'metricbeat-*' },
},
};
const kibanaUrl = 'http://localhost:5601';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ describe('LicenseExpirationAlert', () => {
});
const monitoringCluster = null;
const config = {
ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } },
ui: {
ccs: { enabled: true },
container: { elasticsearch: { enabled: false } },
metricbeat: { index: 'metricbeat-*' },
},
};
const kibanaUrl = 'http://localhost:5601';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ describe('LogstashVersionMismatchAlert', () => {
});
const monitoringCluster = null;
const config = {
ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } },
ui: {
ccs: { enabled: true },
container: { elasticsearch: { enabled: false } },
metricbeat: { index: 'metricbeat-*' },
},
};
const kibanaUrl = 'http://localhost:5601';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ describe('NodesChangedAlert', () => {
});
const monitoringCluster = null;
const config = {
ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } },
ui: {
ccs: { enabled: true },
container: { elasticsearch: { enabled: false } },
metricbeat: { index: 'metricbeat-*' },
},
};
const kibanaUrl = 'http://localhost:5601';

Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/monitoring/server/lib/alerts/append_mb_index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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 { MonitoringConfig } from '../../config';

export function appendMetricbeatIndex(config: MonitoringConfig, indexPattern: string) {
return `${indexPattern},${config.ui.metricbeat.index}`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/
export function getCcsIndexPattern(indexPattern: string, remotes: string[]): string {
return `${indexPattern},${indexPattern
.split(',')
.map((pattern) => {
return remotes.map((remoteName) => `${remoteName}:${pattern}`).join(',');
})
.join(',')}`;
const patternsToAdd = [];
for (const index of indexPattern.split(',')) {
for (const remote of remotes) {
patternsToAdd.push(`${remote}:${index}`);
}
}
return [...indexPattern.split(','), ...patternsToAdd].join(',');
}
2 changes: 1 addition & 1 deletion x-pack/plugins/monitoring/server/lib/ccs_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function appendMetricbeatIndex(config, indexPattern) {
if (isFunction(config.get)) {
mbIndex = config.get('monitoring.ui.metricbeat.index');
} else {
mbIndex = get(config, 'monitoring.ui.metricbeat.index');
mbIndex = get(config, 'ui.metricbeat.index');
}

const newIndexPattern = `${indexPattern},${mbIndex}`;
Expand Down

0 comments on commit 37a6cef

Please sign in to comment.