Skip to content

Commit

Permalink
[Monitoring] Refactor index patterns from configurable to constants (#…
Browse files Browse the repository at this point in the history
…29528)

* Move away from index patterns as configs and move them into constants

* Refactor more usages of the index patterns

* Remove debug

* Update comment

* Fix mocha tests

* PR feedback
  • Loading branch information
chrisronline authored Jan 30, 2019
1 parent cc0e2d0 commit c443fee
Show file tree
Hide file tree
Showing 36 changed files with 131 additions and 81 deletions.
7 changes: 7 additions & 0 deletions x-pack/plugins/monitoring/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,10 @@ export const DEBOUNCE_FAST_MS = 10; // roughly how long it takes to render a fra
export const CLUSTER_ALERTS_ADDRESS_CONFIG_KEY = 'cluster_alerts.email_notifications.email_address';

export const STANDALONE_CLUSTER_CLUSTER_UUID = '__standalone_cluster__';

export const INDEX_PATTERN = '.monitoring-*-6-*';
export const INDEX_PATTERN_KIBANA = '.monitoring-kibana-6-*';
export const INDEX_PATTERN_LOGSTASH = '.monitoring-logstash-6-*';
export const INDEX_PATTERN_BEATS = '.monitoring-beats-6-*';
export const INDEX_ALERTS = '.monitoring-alerts-6';
export const INDEX_PATTERN_ELASTICSEARCH = '.monitoring-es-6-*';
10 changes: 0 additions & 10 deletions x-pack/plugins/monitoring/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,14 @@ export const config = (Joi) => {
}).default()
}).default()
}).default(),
index_pattern: Joi.string().default('.monitoring-*-6-*'),
kibana: Joi.object({
index_pattern: Joi.string().default('.monitoring-kibana-6-*'),
collection: Joi.object({
enabled: Joi.boolean().default(true),
interval: Joi.number().default(10000) // op status metrics get buffered at `ops.interval` and flushed to the bulk endpoint at this interval
}).default()
}).default(),
logstash: Joi.object({
index_pattern: Joi.string().default('.monitoring-logstash-6-*')
}).default(),
beats: Joi.object({
index_pattern: Joi.string().default('.monitoring-beats-6-*')
}).default(),
cluster_alerts: Joi.object({
enabled: Joi.boolean().default(true),
index: Joi.string().default('.monitoring-alerts-6'),
email_notifications: Joi.object({
enabled: Joi.boolean().default(true),
email_address: Joi.string().email(),
Expand All @@ -62,7 +53,6 @@ export const config = (Joi) => {
}).default(),
elasticsearch: Joi.object({
customHeaders: Joi.object().default({}),
index_pattern: Joi.string().default('.monitoring-es-6-*'),
logQueries: Joi.boolean().default(false),
requestHeadersWhitelist: Joi.array().items().single().default(DEFAULT_REQUEST_HEADERS),
sniffOnStart: Joi.boolean().default(false),
Expand Down
35 changes: 12 additions & 23 deletions x-pack/plugins/monitoring/server/lib/__tests__/ccs_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,76 +10,65 @@ import { parseCrossClusterPrefix, prefixIndexPattern } from '../ccs_utils';

describe('ccs_utils', () => {
describe('prefixIndexPattern', () => {
const indexPatternName = 'xyz';
const indexPattern = '.monitoring-xyz-1-*,.monitoring-xyz-2-*';

it('returns the index pattern if ccs is not enabled', () => {
const get = sinon.stub();
const config = { get };

get.withArgs(indexPatternName).returns(indexPattern);
get.withArgs('xpack.monitoring.ccs.enabled').returns(false);

// falsy string values should be ignored
const allPattern = prefixIndexPattern(config, indexPatternName, '*');
const onePattern = prefixIndexPattern(config, indexPatternName, 'do_not_use_me');
const allPattern = prefixIndexPattern(config, indexPattern, '*');
const onePattern = prefixIndexPattern(config, indexPattern, 'do_not_use_me');

expect(allPattern).to.be(indexPattern);
expect(onePattern).to.be(indexPattern);
// uses the config and indexPatternName supplied
expect(get.callCount).to.eql(4);
expect(get.callCount).to.eql(2);
});

it('returns the index pattern if ccs is not used', () => {
const get = sinon.stub();
const config = { get };

get.withArgs(indexPatternName).returns(indexPattern);
get.withArgs('xpack.monitoring.ccs.enabled').returns(true);

// falsy string values should be ignored
const undefinedPattern = prefixIndexPattern(config, indexPatternName);
const nullPattern = prefixIndexPattern(config, indexPatternName, null);
const blankPattern = prefixIndexPattern(config, indexPatternName, '');
const undefinedPattern = prefixIndexPattern(config, indexPattern);
const nullPattern = prefixIndexPattern(config, indexPattern, null);
const blankPattern = prefixIndexPattern(config, indexPattern, '');

expect(undefinedPattern).to.be(indexPattern);
expect(nullPattern).to.be(indexPattern);
expect(blankPattern).to.be(indexPattern);
// uses the config and indexPatternName supplied
expect(get.callCount).to.eql(6);
expect(get.callCount).to.eql(3);
});

it('returns the ccs-prefixed index pattern', () => {
const get = sinon.stub();
const config = { get };

get.withArgs(indexPatternName).returns(indexPattern);
get.withArgs('xpack.monitoring.ccs.enabled').returns(true);

const abcPattern = prefixIndexPattern(config, indexPatternName, 'aBc');
const underscorePattern = prefixIndexPattern(config, indexPatternName, 'cluster_one');
const abcPattern = prefixIndexPattern(config, indexPattern, 'aBc');
const underscorePattern = prefixIndexPattern(config, indexPattern, 'cluster_one');

expect(abcPattern).to.eql('aBc:.monitoring-xyz-1-*,aBc:.monitoring-xyz-2-*');
expect(underscorePattern).to.eql('cluster_one:.monitoring-xyz-1-*,cluster_one:.monitoring-xyz-2-*');

// uses the config and indexPatternName supplied, but only calls once
expect(get.callCount).to.eql(4);
expect(get.callCount).to.eql(2);
});

it('returns the ccs-prefixed index pattern when wildcard and the local cluster pattern', () => {
const get = sinon.stub();
const config = { get };

get.withArgs(indexPatternName).returns(indexPattern);
get.withArgs('xpack.monitoring.ccs.enabled').returns(true);

const pattern = prefixIndexPattern(config, indexPatternName, '*');
const pattern = prefixIndexPattern(config, indexPattern, '*');

// it should have BOTH patterns so that it searches all CCS clusters and the local cluster
expect(pattern).to.eql('*:.monitoring-xyz-1-*,*:.monitoring-xyz-2-*' + ',' + indexPattern);

// uses the config and indexPatternName supplied, but only calls once
expect(get.callCount).to.eql(2);
expect(get.callCount).to.eql(1);
});
});

Expand Down
5 changes: 2 additions & 3 deletions x-pack/plugins/monitoring/server/lib/ccs_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
* which means that the index pattern will be returned without using {@code ccs}.
*
* @param {Object} config The Kibana configuration object.
* @param {String} indexPatternName The index pattern name (e.g., 'xpack.monitoring.elasticsearch.index_pattern')
* @param {String} indexPattern The index pattern name
* @param {String} ccs The optional cluster-prefix to prepend.
* @return {String} The index pattern with the {@code cluster} prefix appropriately prepended.
*/
export function prefixIndexPattern(config, indexPatternName, ccs) {
export function prefixIndexPattern(config, indexPattern, ccs) {
const ccsEnabled = config.get('xpack.monitoring.ccs.enabled');
const indexPattern = config.get(indexPatternName);

if (!ccsEnabled || !ccs) {
return indexPattern;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { get } from 'lodash';
import Boom from 'boom';
import { INDEX_PATTERN } from '../../../common/constants';

/*
* Check the currently logged-in user's privileges for "read" privileges on the
Expand Down Expand Up @@ -36,15 +37,14 @@ export async function verifyMonitoringAuth(req) {
*/
async function verifyHasPrivileges(req) {
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('monitoring');
const config = req.server.config();

const response = await callWithRequest(req, 'transport.request', {
method: 'POST',
path: '/_security/user/_has_privileges',
body: {
index: [
{
names: [ config.get('xpack.monitoring.index_pattern') ], // uses wildcard
names: [ INDEX_PATTERN ], // uses wildcard
privileges: [ 'read' ]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { alertsClusterSearch } from '../../../../cluster_alerts/alerts_cluster_s
import { checkLicense } from '../../../../cluster_alerts/check_license';
import { getClusterLicense } from '../../../../lib/cluster/get_cluster_license';
import { prefixIndexPattern } from '../../../../lib/ccs_utils';
import { INDEX_PATTERN_ELASTICSEARCH, INDEX_ALERTS } from '../../../../../common/constants';

/*
* Cluster Alerts route.
Expand All @@ -35,8 +36,8 @@ export function clusterAlertsRoute(server) {
const config = server.config();
const ccs = req.payload.ccs;
const clusterUuid = req.params.clusterUuid;
const esIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.elasticsearch.index_pattern', ccs);
const alertsIndex = prefixIndexPattern(config, 'xpack.monitoring.cluster_alerts.index', ccs);
const esIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_ELASTICSEARCH, ccs);
const alertsIndex = prefixIndexPattern(config, INDEX_ALERTS, ccs);
const options = {
start: req.payload.timeRange.min,
end: req.payload.timeRange.max
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getMetrics } from '../../../../lib/details/get_metrics';
import { metricSet } from './metric_set_overview';
import { handleError } from '../../../../lib/errors';
import { getApmInfo } from '../../../../lib/apm';
import { INDEX_PATTERN_BEATS } from '../../../../../common/constants';

export function apmInstanceRoute(server) {
server.route({
Expand All @@ -35,7 +36,7 @@ export function apmInstanceRoute(server) {
const config = server.config();
const clusterUuid = req.params.clusterUuid;
const ccs = req.payload.ccs;
const apmIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.beats.index_pattern', ccs);
const apmIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_BEATS, ccs);

try {
const [ metrics, apmSummary ] = await Promise.all([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Joi from 'joi';
import { prefixIndexPattern } from '../../../../lib/ccs_utils';
import { getStats, getApms } from '../../../../lib/apm';
import { handleError } from '../../../../lib/errors';
import { INDEX_PATTERN_BEATS } from '../../../../../common/constants';

export function apmInstancesRoute(server) {
server.route({
Expand All @@ -31,7 +32,7 @@ export function apmInstancesRoute(server) {
const config = server.config();
const ccs = req.payload.ccs;
const clusterUuid = req.params.clusterUuid;
const apmIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.beats.index_pattern', ccs);
const apmIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_BEATS, ccs);

try {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getMetrics } from '../../../../lib/details/get_metrics';
import { metricSet } from './metric_set_overview';
import { handleError } from '../../../../lib/errors';
import { getApmClusterStatus } from './_get_apm_cluster_status';
import { INDEX_PATTERN_BEATS } from '../../../../../common/constants';

export function apmOverviewRoute(server) {
server.route({
Expand All @@ -33,7 +34,7 @@ export function apmOverviewRoute(server) {
const config = server.config();
const ccs = req.payload.ccs;
const clusterUuid = req.params.clusterUuid;
const apmIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.beats.index_pattern', ccs);
const apmIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_BEATS, ccs);

try {
const [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getBeatSummary } from '../../../../lib/beats';
import { getMetrics } from '../../../../lib/details/get_metrics';
import { handleError } from '../../../../lib/errors';
import { metricSet } from './metric_set_detail';
import { INDEX_PATTERN_BEATS } from '../../../../../common/constants';

export function beatsDetailRoute(server) {
server.route({
Expand All @@ -36,7 +37,7 @@ export function beatsDetailRoute(server) {
const beatUuid = req.params.beatUuid;
const config = server.config();
const ccs = req.payload.ccs;
const beatsIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.beats.index_pattern', ccs);
const beatsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_BEATS, ccs);

const summaryOptions = {
clusterUuid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Joi from 'joi';
import { prefixIndexPattern } from '../../../../lib/ccs_utils';
import { getStats, getBeats } from '../../../../lib/beats';
import { handleError } from '../../../../lib/errors';
import { INDEX_PATTERN_BEATS } from '../../../../../common/constants';

export function beatsListingRoute(server) {
server.route({
Expand All @@ -32,7 +33,7 @@ export function beatsListingRoute(server) {
const config = server.config();
const ccs = req.payload.ccs;
const clusterUuid = req.params.clusterUuid;
const beatsIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.beats.index_pattern', ccs);
const beatsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_BEATS, ccs);

try {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getMetrics } from '../../../../lib/details/get_metrics';
import { getLatestStats, getStats } from '../../../../lib/beats';
import { handleError } from '../../../../lib/errors';
import { metricSet } from './metric_set_overview';
import { INDEX_PATTERN_BEATS } from '../../../../../common/constants';

export function beatsOverviewRoute(server) {
server.route({
Expand All @@ -34,7 +35,7 @@ export function beatsOverviewRoute(server) {
const config = server.config();
const ccs = req.payload.ccs;
const clusterUuid = req.params.clusterUuid;
const beatsIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.beats.index_pattern', ccs);
const beatsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_BEATS, ccs);

try {
const [
Expand Down
19 changes: 13 additions & 6 deletions x-pack/plugins/monitoring/server/routes/api/v1/cluster/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import Joi from 'joi';
import { getClustersFromRequest } from '../../../../lib/cluster/get_clusters_from_request';
import { handleError } from '../../../../lib/errors';
import { prefixIndexPattern } from '../../../../lib/ccs_utils';
import {
INDEX_PATTERN_KIBANA,
INDEX_PATTERN_ELASTICSEARCH,
INDEX_PATTERN_LOGSTASH,
INDEX_PATTERN_BEATS,
INDEX_ALERTS
} from '../../../../../common/constants';

export function clusterRoute(server) {
/*
Expand All @@ -33,12 +40,12 @@ export function clusterRoute(server) {
handler: (req) => {
const config = server.config();
const ccs = req.payload.ccs;
const esIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.elasticsearch.index_pattern', ccs);
const kbnIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.kibana.index_pattern', ccs);
const lsIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.logstash.index_pattern', ccs);
const beatsIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.beats.index_pattern', ccs);
const apmIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.beats.index_pattern', ccs);
const alertsIndex = prefixIndexPattern(config, 'xpack.monitoring.cluster_alerts.index', ccs);
const esIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_ELASTICSEARCH, ccs);
const kbnIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_KIBANA, ccs);
const lsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_LOGSTASH, ccs);
const beatsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_BEATS, ccs);
const apmIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_BEATS, ccs);
const alertsIndex = prefixIndexPattern(config, INDEX_ALERTS, ccs);
const indexPatterns = { esIndexPattern, kbnIndexPattern, lsIndexPattern, beatsIndexPattern, apmIndexPattern, alertsIndex };
const options = {
clusterUuid: req.params.clusterUuid,
Expand Down
19 changes: 13 additions & 6 deletions x-pack/plugins/monitoring/server/routes/api/v1/cluster/clusters.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import { getClustersFromRequest } from '../../../../lib/cluster/get_clusters_fro
import { verifyMonitoringAuth } from '../../../../lib/elasticsearch/verify_monitoring_auth';
import { handleError } from '../../../../lib/errors';
import { prefixIndexPattern } from '../../../../lib/ccs_utils';
import {
INDEX_PATTERN_ELASTICSEARCH,
INDEX_PATTERN_KIBANA,
INDEX_PATTERN_LOGSTASH,
INDEX_PATTERN_BEATS,
INDEX_ALERTS
} from '../../../../../common/constants';

export function clustersRoute(server) {
/*
Expand Down Expand Up @@ -40,12 +47,12 @@ export function clustersRoute(server) {
// wildcard means to search _all_ clusters
const ccs = '*';
const config = server.config();
const esIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.elasticsearch.index_pattern', ccs);
const kbnIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.kibana.index_pattern', ccs);
const lsIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.logstash.index_pattern', ccs);
const beatsIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.beats.index_pattern', ccs);
const apmIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.beats.index_pattern', ccs);
const alertsIndex = prefixIndexPattern(config, 'xpack.monitoring.cluster_alerts.index', ccs);
const esIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_ELASTICSEARCH, ccs);
const kbnIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_KIBANA, ccs);
const lsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_LOGSTASH, ccs);
const beatsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_BEATS, ccs);
const apmIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_BEATS, ccs);
const alertsIndex = prefixIndexPattern(config, INDEX_ALERTS, ccs);
const indexPatterns = { esIndexPattern, kbnIndexPattern, lsIndexPattern, beatsIndexPattern, apmIndexPattern, alertsIndex };

clusters = await getClustersFromRequest(req, indexPatterns);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import moment from 'moment';
import { get, groupBy } from 'lodash';
import { handleError } from '../../../../lib/errors/handle_error';
import { prefixIndexPattern } from '../../../../lib/ccs_utils';
import { INDEX_PATTERN_ELASTICSEARCH } from '../../../../../common/constants';

function getBucketScript(max, min) {
return {
Expand Down Expand Up @@ -185,7 +186,7 @@ export function ccrRoute(server) {
async handler(req) {
const config = server.config();
const ccs = req.payload.ccs;
const esIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.elasticsearch.index_pattern', ccs);
const esIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_ELASTICSEARCH, ccs);

try {
const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('monitoring');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Joi from 'joi';
import { handleError } from '../../../../lib/errors/handle_error';
import { prefixIndexPattern } from '../../../../lib/ccs_utils';
import { getMetrics } from '../../../../lib/details/get_metrics';
import { INDEX_PATTERN_ELASTICSEARCH } from '../../../../../common/constants';

function getFormattedLeaderIndex(leaderIndex) {
let leader = leaderIndex;
Expand Down Expand Up @@ -92,7 +93,7 @@ export function ccrShardRoute(server) {
const index = req.params.index;
const shardId = req.params.shardId;
const ccs = req.payload.ccs;
const esIndexPattern = prefixIndexPattern(config, 'xpack.monitoring.elasticsearch.index_pattern', ccs);
const esIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_ELASTICSEARCH, ccs);

const filters = [
{
Expand Down
Loading

0 comments on commit c443fee

Please sign in to comment.