Skip to content

Commit

Permalink
[Stack Monitoring] use CCS constant in place of '*' string (#130466)
Browse files Browse the repository at this point in the history
* add constant for CCS_REMOTE_PATTERN

* remove getConfigCcs and replace with CCS_REMOTE_PATTERN constant

* use CCS_REMOTE_PATTERN for * queries

* changes prefixIndexPattern to prefixIndePatternWithCcs
  • Loading branch information
neptunian authored Apr 19, 2022
1 parent 627c594 commit 00effe7
Show file tree
Hide file tree
Showing 34 changed files with 127 additions and 103 deletions.
20 changes: 10 additions & 10 deletions x-pack/plugins/monitoring/common/ccs_utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
*/

import expect from '@kbn/expect';
import { parseCrossClusterPrefix, prefixIndexPattern } from './ccs_utils';
import { parseCrossClusterPrefix, prefixIndexPatternWithCcs } from './ccs_utils';

// TODO: tests were not running and are not updated.
// They need to be changed to run.
describe.skip('ccs_utils', () => {
describe('prefixIndexPattern', () => {
describe('prefixIndexPatternWithCcs', () => {
const indexPattern = '.monitoring-xyz-1-*,.monitoring-xyz-2-*';

it('returns the index pattern if ccs is not enabled', () => {
// TODO apply as MonitoringConfig during typescript conversion
const config = { ui: { css: { enabled: false } } };

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

expect(allPattern).to.be(indexPattern);
expect(onePattern).to.be(indexPattern);
Expand All @@ -31,9 +31,9 @@ describe.skip('ccs_utils', () => {
const config = { ui: { css: { enabled: true } } };

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

expect(undefinedPattern).to.be(indexPattern);
expect(nullPattern).to.be(indexPattern);
Expand All @@ -44,8 +44,8 @@ describe.skip('ccs_utils', () => {
// TODO apply as MonitoringConfig during typescript conversion
const config = { ui: { css: { enabled: true } } };

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

expect(abcPattern).to.eql(
'aBc:.monitoring-xyz-1-*,aBc:.monitoring-xyz-2-*,aBc:monitoring-xyz-1-*,aBc:monitoring-xyz-2-*'
Expand All @@ -59,7 +59,7 @@ describe.skip('ccs_utils', () => {
// TODO apply as MonitoringConfig during typescript conversion
const config = { ui: { css: { enabled: true } } };

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

// it should have BOTH patterns so that it searches all CCS clusters and the local cluster
expect(pattern).to.eql(
Expand Down
12 changes: 6 additions & 6 deletions x-pack/plugins/monitoring/common/ccs_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import type { MonitoringConfig } from '../server/config';

export function getConfigCcs(config: MonitoringConfig): boolean {
// TODO: (Mat) this function can probably be removed in favor of direct config access where it's used.
return config.ui.ccs.enabled;
}
/**
* Prefix all comma separated index patterns within the original {@code indexPattern}.
*
Expand All @@ -23,8 +19,12 @@ export function getConfigCcs(config: MonitoringConfig): boolean {
* @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: MonitoringConfig, indexPattern: string, ccs?: string) {
const ccsEnabled = getConfigCcs(config);
export function prefixIndexPatternWithCcs(
config: MonitoringConfig,
indexPattern: string,
ccs?: string
) {
const ccsEnabled = config.ui.ccs.enabled;
if (!ccsEnabled || !ccs) {
return indexPattern;
}
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/monitoring/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export const CLUSTER_ALERTS_ADDRESS_CONFIG_KEY = 'cluster_alerts.email_notificat

export const STANDALONE_CLUSTER_CLUSTER_UUID = '__standalone_cluster__';

export const CCS_REMOTE_PATTERN = '*';
export const INDEX_PATTERN = '.monitoring-*';
export const INDEX_PATTERN_KIBANA = '.monitoring-kibana-*';
export const INDEX_PATTERN_LOGSTASH = '.monitoring-logstash-*';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import { useEffect, useState } from 'react';
import { DataViewsPublicPluginStart, DataView } from '@kbn/data-views-plugin/public';
import { prefixIndexPattern } from '../../../../common/ccs_utils';
import { prefixIndexPatternWithCcs } from '../../../../common/ccs_utils';
import {
CCS_REMOTE_PATTERN,
INDEX_PATTERN_BEATS,
INDEX_PATTERN_ELASTICSEARCH,
INDEX_PATTERN_KIBANA,
Expand All @@ -22,7 +23,11 @@ export const useDerivedIndexPattern = (
dataViews: DataViewsPublicPluginStart,
config?: MonitoringConfig
): { loading: boolean; derivedIndexPattern?: DataView } => {
const indexPattern = prefixIndexPattern(config || ({} as MonitoringConfig), INDEX_PATTERNS, '*');
const indexPattern = prefixIndexPatternWithCcs(
config || ({} as MonitoringConfig),
INDEX_PATTERNS,
CCS_REMOTE_PATTERN
);
const [loading, setLoading] = useState<boolean>(true);
const [dataView, setDataView] = useState<DataView>();
useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { get } from 'lodash';
import { ElasticsearchClient } from '@kbn/core/server';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { MonitoringConfig } from '../../../config';
// @ts-ignore
import { prefixIndexPattern } from '../../../../common/ccs_utils';
import { StackProductUsage } from '../types';

interface ESResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import { ElasticsearchClient } from '@kbn/core/server';
import { MonitoringClusterStackProductUsage } from '../types';
import { fetchESUsage } from './fetch_es_usage';
import { MonitoringConfig } from '../../../config';
// @ts-ignore
import { getIndexPatterns } from '../../../lib/cluster/get_index_patterns';
// @ts-ignore
import { prefixIndexPattern } from '../../../../common/ccs_utils';
import {
INDEX_PATTERN_ELASTICSEARCH,
INDEX_PATTERN_KIBANA,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import { ElasticsearchClient } from '@kbn/core/server';
import { get } from 'lodash';
import { CCS_REMOTE_PATTERN } from '../../../common/constants';
import { CCRReadExceptionsStats } from '../../../common/types/alerts';
import { getNewIndexPatterns } from '../cluster/get_index_patterns';
import { createDatasetFilter } from './create_dataset_query_filter';
import { Globals } from '../../static_globals';
import { getConfigCcs } from '../../../common/ccs_utils';

export async function fetchCCRReadExceptions(
esClient: ElasticsearchClient,
Expand All @@ -24,7 +24,7 @@ export async function fetchCCRReadExceptions(
config: Globals.app.config,
moduleType: 'elasticsearch',
dataset: 'ccr',
ccs: getConfigCcs(Globals.app.config) ? '*' : undefined,
ccs: CCS_REMOTE_PATTERN,
});
const params = {
index: indexPatterns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { AlertCluster, AlertClusterHealth } from '../../../common/types/alerts';
import { ElasticsearchSource } from '../../../common/types/es';
import { createDatasetFilter } from './create_dataset_query_filter';
import { Globals } from '../../static_globals';
import { getConfigCcs } from '../../../common/ccs_utils';
import { getNewIndexPatterns } from '../cluster/get_index_patterns';
import { CCS_REMOTE_PATTERN } from '../../../common/constants';

export async function fetchClusterHealth(
esClient: ElasticsearchClient,
Expand All @@ -21,7 +21,7 @@ export async function fetchClusterHealth(
config: Globals.app.config,
moduleType: 'elasticsearch',
dataset: 'cluster_stats',
ccs: getConfigCcs(Globals.app.config) ? '*' : undefined,
ccs: CCS_REMOTE_PATTERN,
});
const params = {
index: indexPatterns,
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/monitoring/server/lib/alerts/fetch_clusters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { AlertCluster } from '../../../common/types/alerts';
import { getNewIndexPatterns } from '../cluster/get_index_patterns';
import { createDatasetFilter } from './create_dataset_query_filter';
import { Globals } from '../../static_globals';
import { getConfigCcs } from '../../../common/ccs_utils';
import { CCS_REMOTE_PATTERN } from '../../../common/constants';

interface RangeFilter {
[field: string]: {
Expand All @@ -28,7 +28,7 @@ export async function fetchClusters(
config: Globals.app.config,
moduleType: 'elasticsearch',
dataset: 'cluster_stats',
ccs: getConfigCcs(Globals.app.config) ? '*' : undefined,
ccs: CCS_REMOTE_PATTERN,
});
const params = {
index: indexPatterns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { AlertCluster, AlertCpuUsageNodeStats } from '../../../common/types/aler
import { createDatasetFilter } from './create_dataset_query_filter';
import { getNewIndexPatterns } from '../cluster/get_index_patterns';
import { Globals } from '../../static_globals';
import { getConfigCcs } from '../../../common/ccs_utils';
import { CCS_REMOTE_PATTERN } from '../../../common/constants';

interface NodeBucketESResponse {
key: string;
Expand Down Expand Up @@ -43,7 +43,7 @@ export async function fetchCpuUsageNodeStats(
config: Globals.app.config,
moduleType: 'elasticsearch',
dataset: 'node_stats',
ccs: getConfigCcs(Globals.app.config) ? '*' : undefined,
ccs: CCS_REMOTE_PATTERN,
});
const params = {
index: indexPatterns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { get } from 'lodash';
import { AlertCluster, AlertDiskUsageNodeStats } from '../../../common/types/alerts';
import { createDatasetFilter } from './create_dataset_query_filter';
import { Globals } from '../../static_globals';
import { getConfigCcs } from '../../../common/ccs_utils';
import { CCS_REMOTE_PATTERN } from '../../../common/constants';
import { getNewIndexPatterns } from '../cluster/get_index_patterns';

export async function fetchDiskUsageNodeStats(
Expand All @@ -25,7 +25,7 @@ export async function fetchDiskUsageNodeStats(
config: Globals.app.config,
moduleType: 'elasticsearch',
dataset: 'node_stats',
ccs: getConfigCcs(Globals.app.config) ? '*' : undefined,
ccs: CCS_REMOTE_PATTERN,
});
const params = {
index: indexPatterns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AlertCluster, AlertVersions } from '../../../common/types/alerts';
import { ElasticsearchSource } from '../../../common/types/es';
import { createDatasetFilter } from './create_dataset_query_filter';
import { Globals } from '../../static_globals';
import { getConfigCcs } from '../../../common/ccs_utils';
import { CCS_REMOTE_PATTERN } from '../../../common/constants';
import { getNewIndexPatterns } from '../cluster/get_index_patterns';

export async function fetchElasticsearchVersions(
Expand All @@ -22,7 +22,7 @@ export async function fetchElasticsearchVersions(
config: Globals.app.config,
moduleType: 'elasticsearch',
dataset: 'cluster_stats',
ccs: getConfigCcs(Globals.app.config) ? '*' : undefined,
ccs: CCS_REMOTE_PATTERN,
});
const params = {
index: indexPatterns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ElasticsearchIndexStats, ElasticsearchResponseHit } from '../../../comm
import { ESGlobPatterns, RegExPatterns } from '../../../common/es_glob_patterns';
import { createDatasetFilter } from './create_dataset_query_filter';
import { Globals } from '../../static_globals';
import { getConfigCcs } from '../../../common/ccs_utils';
import { CCS_REMOTE_PATTERN } from '../../../common/constants';
import { getNewIndexPatterns } from '../cluster/get_index_patterns';

type TopHitType = ElasticsearchResponseHit & {
Expand Down Expand Up @@ -40,7 +40,7 @@ export async function fetchIndexShardSize(
config: Globals.app.config,
moduleType: 'elasticsearch',
dataset: 'index',
ccs: getConfigCcs(Globals.app.config) ? '*' : undefined,
ccs: CCS_REMOTE_PATTERN,
});
const params = {
index: indexPatterns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { get } from 'lodash';
import { AlertCluster, AlertVersions } from '../../../common/types/alerts';
import { createDatasetFilter } from './create_dataset_query_filter';
import { Globals } from '../../static_globals';
import { getConfigCcs } from '../../../common/ccs_utils';
import { CCS_REMOTE_PATTERN } from '../../../common/constants';
import { getNewIndexPatterns } from '../cluster/get_index_patterns';

interface ESAggResponse {
Expand All @@ -26,7 +26,7 @@ export async function fetchKibanaVersions(
config: Globals.app.config,
moduleType: 'kibana',
dataset: 'stats',
ccs: getConfigCcs(Globals.app.config) ? '*' : undefined,
ccs: CCS_REMOTE_PATTERN,
});
const params = {
index: indexPatterns,
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AlertLicense, AlertCluster } from '../../../common/types/alerts';
import { ElasticsearchSource } from '../../../common/types/es';
import { createDatasetFilter } from './create_dataset_query_filter';
import { Globals } from '../../static_globals';
import { getConfigCcs } from '../../../common/ccs_utils';
import { CCS_REMOTE_PATTERN } from '../../../common/constants';
import { getNewIndexPatterns } from '../cluster/get_index_patterns';

export async function fetchLicenses(
Expand All @@ -21,7 +21,7 @@ export async function fetchLicenses(
config: Globals.app.config,
moduleType: 'elasticsearch',
dataset: 'cluster_stats',
ccs: getConfigCcs(Globals.app.config) ? '*' : undefined,
ccs: CCS_REMOTE_PATTERN,
});
const params = {
index: indexPatterns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { get } from 'lodash';
import { AlertCluster, AlertVersions } from '../../../common/types/alerts';
import { createDatasetFilter } from './create_dataset_query_filter';
import { Globals } from '../../static_globals';
import { getConfigCcs } from '../../../common/ccs_utils';
import { CCS_REMOTE_PATTERN } from '../../../common/constants';
import { getNewIndexPatterns } from '../cluster/get_index_patterns';

interface ESAggResponse {
Expand All @@ -26,7 +26,7 @@ export async function fetchLogstashVersions(
config: Globals.app.config,
moduleType: 'logstash',
dataset: 'node_stats',
ccs: getConfigCcs(Globals.app.config) ? '*' : undefined,
ccs: CCS_REMOTE_PATTERN,
});
const params = {
index: indexPatterns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { get } from 'lodash';
import { AlertCluster, AlertMemoryUsageNodeStats } from '../../../common/types/alerts';
import { createDatasetFilter } from './create_dataset_query_filter';
import { Globals } from '../../static_globals';
import { getConfigCcs } from '../../../common/ccs_utils';
import { CCS_REMOTE_PATTERN } from '../../../common/constants';
import { getNewIndexPatterns } from '../cluster/get_index_patterns';

export async function fetchMemoryUsageNodeStats(
Expand All @@ -26,7 +26,7 @@ export async function fetchMemoryUsageNodeStats(
config: Globals.app.config,
moduleType: 'elasticsearch',
dataset: 'node_stats',
ccs: getConfigCcs(Globals.app.config) ? '*' : undefined,
ccs: CCS_REMOTE_PATTERN,
});
const params = {
index: indexPatterns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ElasticsearchClient } from '@kbn/core/server';
import { get } from 'lodash';
import { AlertCluster, AlertMissingData } from '../../../common/types/alerts';
import { Globals } from '../../static_globals';
import { getConfigCcs } from '../../../common/ccs_utils';
import { CCS_REMOTE_PATTERN } from '../../../common/constants';
import { getNewIndexPatterns } from '../cluster/get_index_patterns';
import { createDatasetFilter } from './create_dataset_query_filter';

Expand Down Expand Up @@ -59,7 +59,7 @@ export async function fetchMissingMonitoringData(
config: Globals.app.config,
moduleType: 'elasticsearch',
dataset: 'node_stats',
ccs: getConfigCcs(Globals.app.config) ? '*' : undefined,
ccs: CCS_REMOTE_PATTERN,
});
const params = {
index: indexPatterns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AlertCluster, AlertClusterStatsNodes } from '../../../common/types/aler
import { ElasticsearchSource } from '../../../common/types/es';
import { createDatasetFilter } from './create_dataset_query_filter';
import { Globals } from '../../static_globals';
import { getConfigCcs } from '../../../common/ccs_utils';
import { CCS_REMOTE_PATTERN } from '../../../common/constants';
import { getNewIndexPatterns } from '../cluster/get_index_patterns';

function formatNode(
Expand All @@ -36,7 +36,7 @@ export async function fetchNodesFromClusterStats(
config: Globals.app.config,
moduleType: 'elasticsearch',
dataset: 'cluster_stats',
ccs: getConfigCcs(Globals.app.config) ? '*' : undefined,
ccs: CCS_REMOTE_PATTERN,
});
const params = {
index: indexPatterns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { get } from 'lodash';
import { AlertCluster, AlertThreadPoolRejectionsStats } from '../../../common/types/alerts';
import { createDatasetFilter } from './create_dataset_query_filter';
import { Globals } from '../../static_globals';
import { getConfigCcs } from '../../../common/ccs_utils';
import { CCS_REMOTE_PATTERN } from '../../../common/constants';
import { getNewIndexPatterns } from '../cluster/get_index_patterns';

const invalidNumberValue = (value: number) => {
Expand Down Expand Up @@ -52,7 +52,7 @@ export async function fetchThreadPoolRejectionStats(
config: Globals.app.config,
moduleType: 'elasticsearch',
dataset: 'node_stats',
ccs: getConfigCcs(Globals.app.config) ? '*' : undefined,
ccs: CCS_REMOTE_PATTERN,
});
const params = {
index: indexPatterns,
Expand Down
Loading

0 comments on commit 00effe7

Please sign in to comment.