From de206fb5942e017ceaeff8d5d20d01eff3c9fe2d Mon Sep 17 00:00:00 2001 From: Mat Schaffer Date: Wed, 16 Feb 2022 21:40:58 +0900 Subject: [PATCH] Stronger typing for monitoring configs (#125467) Replaces previous `config.get` pattern typed to `string | undefined` with a full `MonitoringConfig` type. --- .../monitoring/common/ccs_utils.test.js | 29 ++++--------- x-pack/plugins/monitoring/common/ccs_utils.ts | 20 ++------- x-pack/plugins/monitoring/server/config.ts | 24 +++++++++-- .../collectors/get_usage_collector.test.ts | 5 ++- .../monitoring/server/lib/apm/_apm_stats.ts | 4 +- .../monitoring/server/lib/apm/get_apm_info.ts | 7 ++-- .../monitoring/server/lib/apm/get_apms.ts | 4 +- .../server/lib/apm/get_apms_for_clusters.ts | 8 ++-- .../monitoring/server/lib/apm/get_stats.ts | 6 +-- .../server/lib/beats/_beats_stats.ts | 2 +- .../monitoring/server/lib/beats/get_beats.ts | 4 +- .../lib/beats/get_beats_for_clusters.ts | 6 +-- .../server/lib/beats/get_latest_stats.ts | 4 +- .../monitoring/server/lib/beats/get_stats.ts | 4 +- .../cluster/flag_supported_clusters.test.js | 6 +-- .../lib/cluster/flag_supported_clusters.ts | 3 +- .../lib/cluster/get_clusters_from_request.ts | 3 +- .../server/lib/cluster/get_clusters_stats.ts | 4 +- .../server/lib/cluster/get_index_patterns.ts | 2 +- .../server/lib/details/get_metrics.test.js | 10 +---- .../server/lib/details/get_metrics.ts | 4 +- .../server/lib/elasticsearch/get_ml_jobs.ts | 4 +- .../lib/elasticsearch/indices/get_indices.ts | 4 +- .../nodes/get_nodes/get_nodes.ts | 10 +++-- .../get_nodes/get_paginated_nodes.test.js | 4 +- .../nodes/get_nodes/get_paginated_nodes.ts | 6 +-- ...get_indices_unassigned_shard_stats.test.js | 4 +- .../get_indices_unassigned_shard_stats.ts | 4 +- .../shards/get_nodes_shard_count.test.js | 4 +- .../shards/get_nodes_shard_count.ts | 4 +- .../shards/get_shard_allocation.ts | 4 +- .../shards/get_shard_stat_aggs.ts | 6 +-- .../elasticsearch/shards/get_shard_stats.ts | 2 +- .../_enterprise_search_stats.ts | 4 +- .../get_enterprise_search_for_clusters.ts | 6 +-- .../server/lib/enterprise_search/get_stats.ts | 6 +-- .../server/lib/kibana/get_kibanas.ts | 4 +- .../lib/kibana/get_kibanas_for_clusters.ts | 4 +- .../monitoring/server/lib/logs/get_logs.ts | 5 ++- .../lib/logstash/get_logstash_for_clusters.ts | 11 ++--- .../server/lib/logstash/get_nodes.test.ts | 9 +--- .../server/lib/logstash/get_nodes.ts | 4 +- .../lib/logstash/get_paginated_pipelines.ts | 5 +-- .../server/lib/logstash/get_pipeline.ts | 5 ++- .../get_pipeline_stats_aggregation.ts | 14 +++---- .../lib/logstash/get_pipeline_versions.ts | 7 +--- .../lib/logstash/get_pipeline_vertex.ts | 5 ++- .../get_pipeline_vertex_stats_aggregation.ts | 6 +-- .../collection/get_collection_status.test.js | 11 +---- .../setup/collection/get_collection_status.ts | 8 ++-- x-pack/plugins/monitoring/server/plugin.ts | 42 +++++-------------- .../server/routes/api/v1/apm/instance.js | 4 +- .../server/routes/api/v1/apm/instances.js | 4 +- .../server/routes/api/v1/apm/overview.js | 4 +- .../server/routes/api/v1/beats/beat_detail.js | 2 +- .../server/routes/api/v1/beats/beats.js | 2 +- .../server/routes/api/v1/beats/overview.js | 2 +- .../server/routes/api/v1/cluster/cluster.ts | 4 +- .../server/routes/api/v1/cluster/clusters.js | 4 +- .../server/routes/api/v1/elasticsearch/ccr.ts | 20 +++------ .../api/v1/elasticsearch/index_detail.js | 9 +--- .../api/v1/elasticsearch/node_detail.js | 13 ++---- .../routes/api/v1/elasticsearch/overview.js | 12 ++---- .../check/internal_monitoring.ts | 2 +- .../server/routes/api/v1/logstash/node.js | 4 +- .../server/routes/api/v1/logstash/nodes.js | 7 +--- .../server/routes/api/v1/logstash/pipeline.js | 3 +- .../pipelines/cluster_pipeline_ids.js | 11 ++--- .../monitoring/server/static_globals.ts | 9 +--- x-pack/plugins/monitoring/server/types.ts | 19 ++++----- 70 files changed, 198 insertions(+), 308 deletions(-) diff --git a/x-pack/plugins/monitoring/common/ccs_utils.test.js b/x-pack/plugins/monitoring/common/ccs_utils.test.js index 959c378ace604..45f709584c429 100644 --- a/x-pack/plugins/monitoring/common/ccs_utils.test.js +++ b/x-pack/plugins/monitoring/common/ccs_utils.test.js @@ -6,7 +6,6 @@ */ import expect from '@kbn/expect'; -import sinon from 'sinon'; import { parseCrossClusterPrefix, prefixIndexPattern } from './ccs_utils'; // TODO: tests were not running and are not updated. @@ -16,10 +15,8 @@ describe.skip('ccs_utils', () => { 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('monitoring.ui.ccs.enabled').returns(false); + // TODO apply as MonitoringConfig during typescript conversion + const config = { ui: { css: { enabled: false } } }; // falsy string values should be ignored const allPattern = prefixIndexPattern(config, indexPattern, '*'); @@ -27,14 +24,11 @@ describe.skip('ccs_utils', () => { expect(allPattern).to.be(indexPattern); expect(onePattern).to.be(indexPattern); - 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('monitoring.ui.ccs.enabled').returns(true); + // TODO apply as MonitoringConfig during typescript conversion + const config = { ui: { css: { enabled: true } } }; // falsy string values should be ignored const undefinedPattern = prefixIndexPattern(config, indexPattern); @@ -44,14 +38,11 @@ describe.skip('ccs_utils', () => { expect(undefinedPattern).to.be(indexPattern); expect(nullPattern).to.be(indexPattern); expect(blankPattern).to.be(indexPattern); - expect(get.callCount).to.eql(3); }); it('returns the ccs-prefixed index pattern', () => { - const get = sinon.stub(); - const config = { get }; - - get.withArgs('monitoring.ui.ccs.enabled').returns(true); + // 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'); @@ -62,14 +53,11 @@ describe.skip('ccs_utils', () => { expect(underscorePattern).to.eql( 'cluster_one:.monitoring-xyz-1-*,cluster_one:.monitoring-xyz-2-*,cluster_one:monitoring-xyz-1-*,cluster_one:monitoring-xyz-2-*' ); - 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('monitoring.ui.ccs.enabled').returns(true); + // TODO apply as MonitoringConfig during typescript conversion + const config = { ui: { css: { enabled: true } } }; const pattern = prefixIndexPattern(config, indexPattern, '*'); @@ -79,7 +67,6 @@ describe.skip('ccs_utils', () => { ',' + indexPattern ); - expect(get.callCount).to.eql(1); }); }); diff --git a/x-pack/plugins/monitoring/common/ccs_utils.ts b/x-pack/plugins/monitoring/common/ccs_utils.ts index 982189a1e4a97..150e4f709de12 100644 --- a/x-pack/plugins/monitoring/common/ccs_utils.ts +++ b/x-pack/plugins/monitoring/common/ccs_utils.ts @@ -5,24 +5,12 @@ * 2.0. */ -import { isFunction, get } from 'lodash'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import type { MonitoringConfig } from '../server/config'; -type Config = Partial & { - get?: (key: string) => any; -}; - -export function getConfigCcs(config: Config): boolean { - let ccsEnabled = false; - // TODO: NP - // This function is called with both NP config and LP config - if (isFunction(config.get)) { - ccsEnabled = config.get('monitoring.ui.ccs.enabled'); - } else { - ccsEnabled = get(config, 'ui.ccs.enabled'); - } - return ccsEnabled; +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}. @@ -35,7 +23,7 @@ export function getConfigCcs(config: Config): 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: Config, indexPattern: string, ccs?: string) { +export function prefixIndexPattern(config: MonitoringConfig, indexPattern: string, ccs?: string) { const ccsEnabled = getConfigCcs(config); if (!ccsEnabled || !ccs) { return indexPattern; diff --git a/x-pack/plugins/monitoring/server/config.ts b/x-pack/plugins/monitoring/server/config.ts index 9b6bf4c393b44..1cef598ab663b 100644 --- a/x-pack/plugins/monitoring/server/config.ts +++ b/x-pack/plugins/monitoring/server/config.ts @@ -10,6 +10,7 @@ import { config as ElasticsearchBaseConfig, ElasticsearchConfig, } from '../../../../src/core/server/'; +import { MonitoringConfigSchema } from './types'; const hostURISchema = schema.uri({ scheme: ['http', 'https'] }); @@ -79,7 +80,7 @@ export const configSchema = schema.object({ }); export class MonitoringElasticsearchConfig extends ElasticsearchConfig { - public readonly logFetchCount?: number; + public readonly logFetchCount: number; constructor(rawConfig: TypeOf) { super(rawConfig as ElasticsearchConfigType); @@ -87,8 +88,25 @@ export class MonitoringElasticsearchConfig extends ElasticsearchConfig { } } -export type MonitoringConfig = ReturnType; -export function createConfig(config: TypeOf) { +// Build MonitoringConfig type based on MonitoringConfigSchema (config input) but with ui.elasticsearch as a MonitoringElasticsearchConfig (instantiated class) +type MonitoringConfigTypeOverriddenUI = Omit; + +interface MonitoringConfigTypeOverriddenUIElasticsearch + extends Omit { + elasticsearch: MonitoringElasticsearchConfig; +} + +/** + * A functional representation of the `monitoring.*` configuration tree passed in from kibana.yml + */ +export interface MonitoringConfig extends MonitoringConfigTypeOverriddenUI { + /** + * A functional representation of the `monitoring.ui.*` configuration tree passed in from kibana.yml + */ + ui: MonitoringConfigTypeOverriddenUIElasticsearch; +} + +export function createConfig(config: MonitoringConfigSchema): MonitoringConfig { return { ...config, ui: { diff --git a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/get_usage_collector.test.ts b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/get_usage_collector.test.ts index 9c72c1f8d3841..fa276add71efb 100644 --- a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/get_usage_collector.test.ts +++ b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/get_usage_collector.test.ts @@ -8,6 +8,7 @@ import { getMonitoringUsageCollector } from './get_usage_collector'; import { fetchClusters } from '../../lib/alerts/fetch_clusters'; import { elasticsearchServiceMock } from '../../../../../../src/core/server/mocks'; +import { MonitoringConfig } from '../../config'; jest.mock('../../lib/alerts/fetch_clusters', () => ({ fetchClusters: jest.fn().mockImplementation(() => { @@ -61,13 +62,13 @@ jest.mock('./lib/fetch_license_type', () => ({ describe('getMonitoringUsageCollector', () => { const esClient = elasticsearchServiceMock.createClusterClient(); const getEsClient = () => esClient; - const config: any = { + const config = { ui: { ccs: { enabled: true, }, }, - }; + } as MonitoringConfig; it('should be configured correctly', async () => { const usageCollection: any = { diff --git a/x-pack/plugins/monitoring/server/lib/apm/_apm_stats.ts b/x-pack/plugins/monitoring/server/lib/apm/_apm_stats.ts index 0068d521055ef..79d7ed9d26c58 100644 --- a/x-pack/plugins/monitoring/server/lib/apm/_apm_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/apm/_apm_stats.ts @@ -7,7 +7,7 @@ import type { ElasticsearchResponse } from '../../../common/types/es'; -const getMemPath = (cgroup?: string) => +const getMemPath = (cgroup?: boolean) => cgroup ? 'beats_stats.metrics.beat.cgroup.memory.mem.usage.bytes' : 'beats_stats.metrics.beat.memstats.rss'; @@ -30,7 +30,7 @@ export const apmAggFilterPath = [ 'aggregations.max_mem_total.value', 'aggregations.versions.buckets', ]; -export const apmUuidsAgg = (maxBucketSize?: string, cgroup?: string) => ({ +export const apmUuidsAgg = (maxBucketSize?: number, cgroup?: boolean) => ({ total: { cardinality: { field: 'beats_stats.beat.uuid', diff --git a/x-pack/plugins/monitoring/server/lib/apm/get_apm_info.ts b/x-pack/plugins/monitoring/server/lib/apm/get_apm_info.ts index 5b2f8424a566d..d12d69f9a18da 100644 --- a/x-pack/plugins/monitoring/server/lib/apm/get_apm_info.ts +++ b/x-pack/plugins/monitoring/server/lib/apm/get_apm_info.ts @@ -17,11 +17,12 @@ import { ApmMetric } from '../metrics'; import { getTimeOfLastEvent } from './_get_time_of_last_event'; import { LegacyRequest } from '../../types'; import { ElasticsearchResponse } from '../../../common/types/es'; +import { MonitoringConfig } from '../../config'; export function handleResponse( response: ElasticsearchResponse, apmUuid: string, - config: { get: (key: string) => string | undefined } + config: MonitoringConfig ) { if (!response.hits || response.hits.hits.length === 0) { return {}; @@ -68,7 +69,7 @@ export function handleResponse( eventsDropped: getDiffCalculation(eventsDroppedLast, eventsDroppedFirst), bytesWritten: getDiffCalculation(Number(bytesWrittenLast), Number(bytesWrittenFirst)), config: { - container: config.get('monitoring.ui.container.apm.enabled'), + container: config.ui.container.apm.enabled, }, }; } @@ -168,7 +169,7 @@ export async function getApmInfo( }), ]); - const formattedResponse = handleResponse(response, apmUuid, req.server.config()); + const formattedResponse = handleResponse(response, apmUuid, req.server.config); return { ...formattedResponse, timeOfLastEvent, diff --git a/x-pack/plugins/monitoring/server/lib/apm/get_apms.ts b/x-pack/plugins/monitoring/server/lib/apm/get_apms.ts index 7089a0507107a..92eb36f978551 100644 --- a/x-pack/plugins/monitoring/server/lib/apm/get_apms.ts +++ b/x-pack/plugins/monitoring/server/lib/apm/get_apms.ts @@ -104,13 +104,13 @@ export function handleResponse(response: ElasticsearchResponse, start: number, e export async function getApms(req: LegacyRequest, apmIndexPattern: string, clusterUuid: string) { checkParam(apmIndexPattern, 'apmIndexPattern in getBeats'); - const config = req.server.config(); + const config = req.server.config; const start = moment.utc(req.payload.timeRange.min).valueOf(); const end = moment.utc(req.payload.timeRange.max).valueOf(); const params = { index: apmIndexPattern, - size: config.get('monitoring.ui.max_bucket_size'), // FIXME + size: config.ui.max_bucket_size, ignore_unavailable: true, filter_path: [ // only filter path can filter for inner_hits diff --git a/x-pack/plugins/monitoring/server/lib/apm/get_apms_for_clusters.ts b/x-pack/plugins/monitoring/server/lib/apm/get_apms_for_clusters.ts index 77b5e44754999..2bf294b93fa1f 100644 --- a/x-pack/plugins/monitoring/server/lib/apm/get_apms_for_clusters.ts +++ b/x-pack/plugins/monitoring/server/lib/apm/get_apms_for_clusters.ts @@ -36,9 +36,9 @@ export function handleResponse(clusterUuid: string, response: ElasticsearchRespo export function getApmsForClusters(req: LegacyRequest, clusters: Cluster[], ccs?: string) { const start = req.payload.timeRange.min; const end = req.payload.timeRange.max; - const config = req.server.config(); - const maxBucketSize = config.get('monitoring.ui.max_bucket_size'); - const cgroup = config.get('monitoring.ui.container.apm.enabled'); + const config = req.server.config; + const maxBucketSize = config.ui.max_bucket_size; + const cgroup = config.ui.container.apm.enabled; const indexPatterns = getLegacyIndexPattern({ moduleType: 'beats', @@ -82,7 +82,7 @@ export function getApmsForClusters(req: LegacyRequest, clusters: Cluster[], ccs? return { ...formattedResponse, config: { - container: config.get('monitoring.ui.container.apm.enabled'), + container: cgroup, }, stats: { ...formattedResponse.stats, diff --git a/x-pack/plugins/monitoring/server/lib/apm/get_stats.ts b/x-pack/plugins/monitoring/server/lib/apm/get_stats.ts index ea71229bac816..e536972c7acb4 100644 --- a/x-pack/plugins/monitoring/server/lib/apm/get_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/apm/get_stats.ts @@ -27,11 +27,11 @@ export function handleResponse(response: ElasticsearchResponse) { export async function getStats(req: LegacyRequest, apmIndexPattern: string, clusterUuid: string) { checkParam(apmIndexPattern, 'apmIndexPattern in getBeats'); - const config = req.server.config(); + const config = req.server.config; const start = moment.utc(req.payload.timeRange.min).valueOf(); const end = moment.utc(req.payload.timeRange.max).valueOf(); - const maxBucketSize = config.get('monitoring.ui.max_bucket_size'); - const cgroup = config.get('monitoring.ui.container.apm.enabled'); + const maxBucketSize = config.ui.max_bucket_size; + const cgroup = config.ui.container.apm.enabled; const params = { index: apmIndexPattern, diff --git a/x-pack/plugins/monitoring/server/lib/beats/_beats_stats.ts b/x-pack/plugins/monitoring/server/lib/beats/_beats_stats.ts index c2ddadcf7ebf4..3b55a66561a88 100644 --- a/x-pack/plugins/monitoring/server/lib/beats/_beats_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/beats/_beats_stats.ts @@ -28,7 +28,7 @@ export const beatsAggFilterPath = [ 'aggregations.max_bytes_sent_total.value', ]; -export const beatsUuidsAgg = (maxBucketSize: string) => ({ +export const beatsUuidsAgg = (maxBucketSize: number) => ({ types: { terms: { field: 'beats_stats.beat.type', diff --git a/x-pack/plugins/monitoring/server/lib/beats/get_beats.ts b/x-pack/plugins/monitoring/server/lib/beats/get_beats.ts index 85a4bb61f5573..35bd035a91fea 100644 --- a/x-pack/plugins/monitoring/server/lib/beats/get_beats.ts +++ b/x-pack/plugins/monitoring/server/lib/beats/get_beats.ts @@ -114,13 +114,13 @@ export function handleResponse(response: ElasticsearchResponse, start: number, e export async function getBeats(req: LegacyRequest, beatsIndexPattern: string, clusterUuid: string) { checkParam(beatsIndexPattern, 'beatsIndexPattern in getBeats'); - const config = req.server.config(); + const config = req.server.config; const start = moment.utc(req.payload.timeRange.min).valueOf(); const end = moment.utc(req.payload.timeRange.max).valueOf(); const params = { index: beatsIndexPattern, - size: config.get('monitoring.ui.max_bucket_size'), // FIXME + size: config.ui.max_bucket_size, ignore_unavailable: true, filter_path: [ // only filter path can filter for inner_hits diff --git a/x-pack/plugins/monitoring/server/lib/beats/get_beats_for_clusters.ts b/x-pack/plugins/monitoring/server/lib/beats/get_beats_for_clusters.ts index ae723fa99bbde..6f42695fb7e99 100644 --- a/x-pack/plugins/monitoring/server/lib/beats/get_beats_for_clusters.ts +++ b/x-pack/plugins/monitoring/server/lib/beats/get_beats_for_clusters.ts @@ -35,8 +35,8 @@ export function handleResponse(clusterUuid: string, response: ElasticsearchRespo export function getBeatsForClusters(req: LegacyRequest, clusters: Cluster[], ccs: string) { const start = req.payload.timeRange.min; const end = req.payload.timeRange.max; - const config = req.server.config(); - const maxBucketSize = config.get('monitoring.ui.max_bucket_size'); + const config = req.server.config; + const maxBucketSize = config.ui.max_bucket_size; const indexPatterns = getLegacyIndexPattern({ moduleType: 'beats', ccs, @@ -57,7 +57,7 @@ export function getBeatsForClusters(req: LegacyRequest, clusters: Cluster[], ccs clusterUuid, metric: BeatsClusterMetric.getMetricFields(), // override default of BeatMetric.getMetricFields }), - aggs: beatsUuidsAgg(maxBucketSize!), + aggs: beatsUuidsAgg(maxBucketSize), }, }; diff --git a/x-pack/plugins/monitoring/server/lib/beats/get_latest_stats.ts b/x-pack/plugins/monitoring/server/lib/beats/get_latest_stats.ts index 684b0d6301e1f..81cf42ab0094b 100644 --- a/x-pack/plugins/monitoring/server/lib/beats/get_latest_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/beats/get_latest_stats.ts @@ -72,7 +72,7 @@ export function handleResponse(response?: BeatsElasticsearchResponse) { export function getLatestStats(req: LegacyRequest, beatsIndexPattern: string, clusterUuid: string) { checkParam(beatsIndexPattern, 'beatsIndexPattern in getBeats'); - const config = req.server.config(); + const config = req.server.config; const lastDayFilter = { range: { timestamp: { gte: 'now-1d/d', lte: 'now/d' } } }; const beatUuidAgg = { // size of these buckets determines actual # of beats in each kind of aggregation @@ -80,7 +80,7 @@ export function getLatestStats(req: LegacyRequest, beatsIndexPattern: string, cl uuids: { terms: { field: 'beats_stats.beat.uuid', - size: config.get('monitoring.ui.max_bucket_size'), + size: config.ui.max_bucket_size, }, }, }, diff --git a/x-pack/plugins/monitoring/server/lib/beats/get_stats.ts b/x-pack/plugins/monitoring/server/lib/beats/get_stats.ts index be3f3d88e0709..1269255cd2b6e 100644 --- a/x-pack/plugins/monitoring/server/lib/beats/get_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/beats/get_stats.ts @@ -28,10 +28,10 @@ export function handleResponse(response: BeatsElasticsearchResponse) { export async function getStats(req: LegacyRequest, beatsIndexPattern: string, clusterUuid: string) { checkParam(beatsIndexPattern, 'beatsIndexPattern in getBeats'); - const config = req.server.config(); + const config = req.server.config; const start = moment.utc(req.payload.timeRange.min).valueOf(); const end = moment.utc(req.payload.timeRange.max).valueOf(); - const maxBucketSize = config.get('monitoring.ui.max_bucket_size'); + const maxBucketSize = config.ui.max_bucket_size; const params = { index: beatsIndexPattern, diff --git a/x-pack/plugins/monitoring/server/lib/cluster/flag_supported_clusters.test.js b/x-pack/plugins/monitoring/server/lib/cluster/flag_supported_clusters.test.js index 37525d5b68e09..28592654cb97d 100644 --- a/x-pack/plugins/monitoring/server/lib/cluster/flag_supported_clusters.test.js +++ b/x-pack/plugins/monitoring/server/lib/cluster/flag_supported_clusters.test.js @@ -11,11 +11,7 @@ import { flagSupportedClusters } from './flag_supported_clusters'; const mockReq = (log, queryResult = {}) => { return { server: { - config() { - return { - get: sinon.stub().withArgs('server.uuid').returns('kibana-1234'), - }; - }, + instanceUuid: 'kibana-1234', plugins: { elasticsearch: { getCluster() { diff --git a/x-pack/plugins/monitoring/server/lib/cluster/flag_supported_clusters.ts b/x-pack/plugins/monitoring/server/lib/cluster/flag_supported_clusters.ts index 9ead8833dce46..4c23ff858f471 100644 --- a/x-pack/plugins/monitoring/server/lib/cluster/flag_supported_clusters.ts +++ b/x-pack/plugins/monitoring/server/lib/cluster/flag_supported_clusters.ts @@ -89,7 +89,6 @@ async function findSupportedBasicLicenseCluster( * are also flagged as supported in this method. */ export function flagSupportedClusters(req: LegacyRequest, ccs: string) { - const config = req.server.config(); const serverLog = (message: string) => req.getLogger('supported-clusters').debug(message); const flagAllSupported = (clusters: ElasticsearchModifiedSource[]) => { clusters.forEach((cluster) => { @@ -129,7 +128,7 @@ export function flagSupportedClusters(req: LegacyRequest, ccs: string) { // if all linked are basic licenses if (linkedClusterCount === basicLicenseCount) { - const kibanaUuid = config.get('server.uuid') as string; + const kibanaUuid = req.server.instanceUuid; return await findSupportedBasicLicenseCluster(req, clusters, ccs, kibanaUuid, serverLog); } diff --git a/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_from_request.ts b/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_from_request.ts index 570d6fadfeb90..c4c31f31df688 100644 --- a/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_from_request.ts +++ b/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_from_request.ts @@ -54,7 +54,6 @@ export async function getClustersFromRequest( ) { const { filebeatIndexPattern } = indexPatterns; - const config = req.server.config(); const isStandaloneCluster = clusterUuid === STANDALONE_CLUSTER_CLUSTER_UUID; let clusters: Cluster[] = []; @@ -246,7 +245,7 @@ export async function getClustersFromRequest( // check ccr configuration const isCcrEnabled = await checkCcrEnabled(req, '*'); - const kibanaUuid = config.get('server.uuid')!; + const kibanaUuid = req.server.instanceUuid; return getClustersSummary(req.server, clusters as EnhancedClusters[], kibanaUuid, isCcrEnabled); } diff --git a/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_stats.ts b/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_stats.ts index 80fe5ce3806a6..e9b734d98b70d 100644 --- a/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_stats.ts @@ -53,14 +53,14 @@ function fetchClusterStats(req: LegacyRequest, clusterUuid: string, ccs?: string ccs: ccs || req.payload.ccs, }); - const config = req.server.config(); + const config = req.server.config; // Get the params from the POST body for the request const start = req.payload.timeRange.min; const end = req.payload.timeRange.max; const metric = ElasticsearchMetric.getMetricFields(); const params = { index: indexPattern, - size: config.get('monitoring.ui.max_bucket_size'), + size: config.ui.max_bucket_size, ignore_unavailable: true, filter_path: [ 'hits.hits._index', diff --git a/x-pack/plugins/monitoring/server/lib/cluster/get_index_patterns.ts b/x-pack/plugins/monitoring/server/lib/cluster/get_index_patterns.ts index fe9a319780db5..e40745790dc82 100644 --- a/x-pack/plugins/monitoring/server/lib/cluster/get_index_patterns.ts +++ b/x-pack/plugins/monitoring/server/lib/cluster/get_index_patterns.ts @@ -26,7 +26,7 @@ export function getIndexPatterns( additionalPatterns: Record = {}, ccs: string = '*' ) { - const config = server.config(); + const config = server.config; const esIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_ELASTICSEARCH, ccs); const kbnIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_KIBANA, ccs); const lsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_LOGSTASH, ccs); diff --git a/x-pack/plugins/monitoring/server/lib/details/get_metrics.test.js b/x-pack/plugins/monitoring/server/lib/details/get_metrics.test.js index 5ba7fd1207448..3f643cf0be27a 100644 --- a/x-pack/plugins/monitoring/server/lib/details/get_metrics.test.js +++ b/x-pack/plugins/monitoring/server/lib/details/get_metrics.test.js @@ -29,17 +29,9 @@ jest.mock('../../static_globals', () => ({ })); function getMockReq(metricsBuckets = []) { - const config = { - get: sinon.stub(), - }; - - config.get.withArgs('monitoring.ui.min_interval_seconds').returns(10); - return { server: { - config() { - return config; - }, + config: { ui: { min_interval_seconds: 10 } }, plugins: { elasticsearch: { getCluster: sinon diff --git a/x-pack/plugins/monitoring/server/lib/details/get_metrics.ts b/x-pack/plugins/monitoring/server/lib/details/get_metrics.ts index efca0b84b0d65..83db64add1c69 100644 --- a/x-pack/plugins/monitoring/server/lib/details/get_metrics.ts +++ b/x-pack/plugins/monitoring/server/lib/details/get_metrics.ts @@ -28,11 +28,11 @@ export async function getMetrics( checkParam(moduleType, 'moduleType in details/getMetrics'); checkParam(metricSet, 'metricSet in details/getMetrics'); - const config = req.server.config(); + const config = req.server.config; // TODO: Pass in req parameters as explicit function parameters let min = moment.utc(req.payload.timeRange.min).valueOf(); const max = moment.utc(req.payload.timeRange.max).valueOf(); - const minIntervalSeconds = Number(config.get('monitoring.ui.min_interval_seconds')); + const minIntervalSeconds = config.ui.min_interval_seconds; const bucketSize = calculateTimeseriesInterval(min, max, minIntervalSeconds); const timezone = await getTimezone(req); diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/get_ml_jobs.ts b/x-pack/plugins/monitoring/server/lib/elasticsearch/get_ml_jobs.ts index f321326ee090a..881643c8b33df 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/get_ml_jobs.ts +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/get_ml_jobs.ts @@ -40,8 +40,8 @@ export function handleResponse(response: ElasticsearchResponse) { export type MLJobs = ReturnType; export function getMlJobs(req: LegacyRequest) { - const config = req.server.config(); - const maxBucketSize = config.get('monitoring.ui.max_bucket_size'); + const config = req.server.config; + const maxBucketSize = config.ui.max_bucket_size; const start = req.payload.timeRange.min; // no wrapping in moment :) const end = req.payload.timeRange.max; const clusterUuid = req.params.clusterUuid; diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/indices/get_indices.ts b/x-pack/plugins/monitoring/server/lib/elasticsearch/indices/get_indices.ts index 85e49f463526a..548416d3d5244 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/indices/get_indices.ts +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/indices/get_indices.ts @@ -186,12 +186,12 @@ export function getIndices( const { min: start, max: end } = req.payload.timeRange; const clusterUuid = req.params.clusterUuid; - const config = req.server.config(); + const config = req.server.config; const params = buildGetIndicesQuery(req, clusterUuid, { start, end, showSystemIndices, - size: parseInt(config.get('monitoring.ui.max_bucket_size') || '', 10), + size: config.ui.max_bucket_size, }); const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('monitoring'); diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_nodes.ts b/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_nodes.ts index 2bd7078fa00a4..2793fe9317d71 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_nodes.ts +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_nodes.ts @@ -46,13 +46,13 @@ export async function getNodes( const max = end; const duration = moment.duration(max - orgStart, 'ms'); - const config = req.server.config(); + const config = req.server.config; const clusterUuid = req.params.clusterUuid; const metricFields = ElasticsearchMetric.getMetricFields(); const min = start; const bucketSize = Math.max( - parseInt(config.get('monitoring.ui.min_interval_seconds') as string, 10), + config.ui.min_interval_seconds, calculateAuto(100, duration).asSeconds() ); @@ -74,9 +74,11 @@ export async function getNodes( dataset, }); + const maxBucketSize = config.ui.max_bucket_size; + const params = { index: indexPatterns, - size: config.get('monitoring.ui.max_bucket_size'), + size: maxBucketSize, ignore_unavailable: true, body: { query: createQuery({ @@ -97,7 +99,7 @@ export async function getNodes( terms: { field: `source_node.uuid`, include: uuidsToInclude, - size: config.get('monitoring.ui.max_bucket_size'), + size: maxBucketSize, }, aggs: { by_date: { diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_paginated_nodes.test.js b/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_paginated_nodes.test.js index f96cb8e7a1853..3dc2c6c75b79f 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_paginated_nodes.test.js +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_paginated_nodes.test.js @@ -42,9 +42,7 @@ jest.mock('../../../details/get_metrics', () => ({ describe('getPaginatedNodes', () => { const req = { server: { - config: () => ({ - get: () => 10, - }), + config: { ui: { max_bucket_size: 10000 } }, }, }; const clusterUuid = '1abc'; diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_paginated_nodes.ts b/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_paginated_nodes.ts index d353ea844cdf9..7f250289cf3b6 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_paginated_nodes.ts +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_paginated_nodes.ts @@ -56,8 +56,8 @@ export async function getPaginatedNodes( nodesShardCount: { nodes: Record }; } ) { - const config = req.server.config(); - const size = Number(config.get('monitoring.ui.max_bucket_size')); + const config = req.server.config; + const size = config.ui.max_bucket_size; const nodes: Node[] = await getNodeIds(req, { clusterUuid }, size); // Add `isOnline` and shards from the cluster state and shard stats @@ -82,7 +82,7 @@ export async function getPaginatedNodes( const groupBy = { field: `source_node.uuid`, include: nodes.map((node) => node.uuid), - size: config.get('monitoring.ui.max_bucket_size'), + size, }; const metricSeriesData = await getMetrics( req, diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_indices_unassigned_shard_stats.test.js b/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_indices_unassigned_shard_stats.test.js index 6a9e01166ea87..f273117b867ad 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_indices_unassigned_shard_stats.test.js +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_indices_unassigned_shard_stats.test.js @@ -30,9 +30,7 @@ describe('getIndicesUnassignedShardStats', () => { const req = { payload: {}, server: { - config: () => ({ - get: () => {}, - }), + config: { ui: { max_bucket_size: 10000 } }, plugins: { elasticsearch: { getCluster: () => ({ diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_indices_unassigned_shard_stats.ts b/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_indices_unassigned_shard_stats.ts index b0ba916b53eb8..20f5134012c29 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_indices_unassigned_shard_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_indices_unassigned_shard_stats.ts @@ -20,8 +20,8 @@ import { getNewIndexPatterns } from '../../cluster/get_index_patterns'; import { Globals } from '../../../static_globals'; async function getUnassignedShardData(req: LegacyRequest, cluster: ElasticsearchModifiedSource) { - const config = req.server.config(); - const maxBucketSize = config.get('monitoring.ui.max_bucket_size'); + const config = req.server.config; + const maxBucketSize = config.ui.max_bucket_size; const metric = ElasticsearchMetric.getMetricFields(); const filters = []; diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_nodes_shard_count.test.js b/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_nodes_shard_count.test.js index c5c77ef389427..04e50f16be6de 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_nodes_shard_count.test.js +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_nodes_shard_count.test.js @@ -30,9 +30,7 @@ describe('getNodeShardCount', () => { const req = { payload: {}, server: { - config: () => ({ - get: () => {}, - }), + config: { ui: { max_bucket_size: 10000 } }, plugins: { elasticsearch: { getCluster: () => ({ diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_nodes_shard_count.ts b/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_nodes_shard_count.ts index a8ce0d429f06c..535070a15a55e 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_nodes_shard_count.ts +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_nodes_shard_count.ts @@ -18,8 +18,8 @@ import { getNewIndexPatterns } from '../../cluster/get_index_patterns'; import { Globals } from '../../../static_globals'; async function getShardCountPerNode(req: LegacyRequest, cluster: ElasticsearchModifiedSource) { - const config = req.server.config(); - const maxBucketSize = config.get('monitoring.ui.max_bucket_size'); + const config = req.server.config; + const maxBucketSize = config.ui.max_bucket_size; const metric = ElasticsearchMetric.getMetricFields(); const filters = []; diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_shard_allocation.ts b/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_shard_allocation.ts index cede4bf921419..ad306d9c50354 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_shard_allocation.ts +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_shard_allocation.ts @@ -97,7 +97,7 @@ export function getShardAllocation( }); } - const config = req.server.config(); + const config = req.server.config; const clusterUuid = req.params.clusterUuid; const metric = ElasticsearchMetric.getMetricFields(); @@ -113,7 +113,7 @@ export function getShardAllocation( const params = { index: indexPatterns, - size: config.get('monitoring.ui.max_bucket_size'), + size: config.ui.max_bucket_size, ignore_unavailable: true, body: { query: createQuery({ diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_shard_stat_aggs.ts b/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_shard_stat_aggs.ts index cf11c939fb230..b92c46c2fb1a1 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_shard_stat_aggs.ts +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_shard_stat_aggs.ts @@ -10,14 +10,14 @@ * @param {Boolean} includeNodes - whether to add the aggs for node shards */ -import { LegacyServer } from '../../../types'; +import { MonitoringConfig } from '../../../config'; export function getShardAggs( - config: ReturnType, + config: MonitoringConfig, includeNodes: boolean, includeIndices: boolean ) { - const maxBucketSize = config.get('monitoring.ui.max_bucket_size'); + const maxBucketSize = config.ui.max_bucket_size; const aggSize = 10; const indicesAgg = { terms: { diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_shard_stats.ts b/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_shard_stats.ts index 29cbcb9ac0567..9c2c5cf45235a 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_shard_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_shard_stats.ts @@ -70,7 +70,7 @@ export function getShardStats( dataset, }); - const config = req.server.config(); + const config = req.server.config; const metric = ElasticsearchMetric.getMetricFields(); const filters = []; if (cluster.cluster_state?.state_uuid) { diff --git a/x-pack/plugins/monitoring/server/lib/enterprise_search/_enterprise_search_stats.ts b/x-pack/plugins/monitoring/server/lib/enterprise_search/_enterprise_search_stats.ts index 77ee431cf5aa2..ad946d974ca96 100644 --- a/x-pack/plugins/monitoring/server/lib/enterprise_search/_enterprise_search_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/enterprise_search/_enterprise_search_stats.ts @@ -23,7 +23,7 @@ export const entSearchAggFilterPath = [ 'aggregations.cluster_heap_committed.value', ]; -export const entSearchUuidsAgg = (maxBucketSize?: string) => ({ +export const entSearchUuidsAgg = (maxBucketSize: number) => ({ // Count all unique agents total: { cardinality: { @@ -32,7 +32,7 @@ export const entSearchUuidsAgg = (maxBucketSize?: string) => ({ }, }, - // Collect all runnng versions + // Collect all running versions versions: { terms: { field: 'enterprisesearch.health.version.number', diff --git a/x-pack/plugins/monitoring/server/lib/enterprise_search/get_enterprise_search_for_clusters.ts b/x-pack/plugins/monitoring/server/lib/enterprise_search/get_enterprise_search_for_clusters.ts index d46853fe48d3f..f530912562976 100644 --- a/x-pack/plugins/monitoring/server/lib/enterprise_search/get_enterprise_search_for_clusters.ts +++ b/x-pack/plugins/monitoring/server/lib/enterprise_search/get_enterprise_search_for_clusters.ts @@ -33,8 +33,8 @@ export function getEnterpriseSearchForClusters( ) { const start = req.payload.timeRange.min; const end = req.payload.timeRange.max; - const config = req.server.config(); - const maxBucketSize = config.get('monitoring.ui.max_bucket_size'); + const config = req.server.config; + const maxBucketSize = config.ui.max_bucket_size; const indexPatterns = getLegacyIndexPattern({ moduleType: 'enterprisesearch', @@ -57,7 +57,7 @@ export function getEnterpriseSearchForClusters( uuid: clusterUuid, metric: EnterpriseSearchMetric.getMetricFields(), }), - aggs: entSearchUuidsAgg(maxBucketSize!), + aggs: entSearchUuidsAgg(maxBucketSize), }, }; diff --git a/x-pack/plugins/monitoring/server/lib/enterprise_search/get_stats.ts b/x-pack/plugins/monitoring/server/lib/enterprise_search/get_stats.ts index 73bd33daeac34..afe315f6a3ba7 100644 --- a/x-pack/plugins/monitoring/server/lib/enterprise_search/get_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/enterprise_search/get_stats.ts @@ -18,10 +18,10 @@ import { getLegacyIndexPattern } from '../cluster/get_index_patterns'; import { Globals } from '../../static_globals'; export async function getStats(req: LegacyRequest, clusterUuid: string) { - const config = req.server.config(); + const config = req.server.config; const start = moment.utc(req.payload.timeRange.min).valueOf(); const end = moment.utc(req.payload.timeRange.max).valueOf(); - const maxBucketSize = config.get('monitoring.ui.max_bucket_size'); + const maxBucketSize = config.ui.max_bucket_size; // just get the legacy pattern since no integration exists yet const indexPattern = getLegacyIndexPattern({ @@ -41,7 +41,7 @@ export async function getStats(req: LegacyRequest, clusterUuid: string) { end, uuid: clusterUuid, }), - aggs: entSearchUuidsAgg(maxBucketSize!), + aggs: entSearchUuidsAgg(maxBucketSize), }, }; diff --git a/x-pack/plugins/monitoring/server/lib/kibana/get_kibanas.ts b/x-pack/plugins/monitoring/server/lib/kibana/get_kibanas.ts index a476baa9c45d2..5f9cd6b53a6bb 100644 --- a/x-pack/plugins/monitoring/server/lib/kibana/get_kibanas.ts +++ b/x-pack/plugins/monitoring/server/lib/kibana/get_kibanas.ts @@ -55,7 +55,7 @@ interface Kibana { * - response times */ export async function getKibanas(req: LegacyRequest, { clusterUuid }: { clusterUuid: string }) { - const config = req.server.config(); + const config = req.server.config; const start = moment.utc(req.payload.timeRange.min).valueOf(); const end = moment.utc(req.payload.timeRange.max).valueOf(); const moduleType = 'kibana'; @@ -69,7 +69,7 @@ export async function getKibanas(req: LegacyRequest, { clusterUuid }: { clusterU }); const params = { index: indexPatterns, - size: config.get('monitoring.ui.max_bucket_size'), + size: config.ui.max_bucket_size, ignore_unavailable: true, body: { query: createQuery({ diff --git a/x-pack/plugins/monitoring/server/lib/kibana/get_kibanas_for_clusters.ts b/x-pack/plugins/monitoring/server/lib/kibana/get_kibanas_for_clusters.ts index 883ca17c98c5b..d54c9396d2df7 100644 --- a/x-pack/plugins/monitoring/server/lib/kibana/get_kibanas_for_clusters.ts +++ b/x-pack/plugins/monitoring/server/lib/kibana/get_kibanas_for_clusters.ts @@ -26,7 +26,7 @@ import { Globals } from '../../static_globals'; * - combined health */ export function getKibanasForClusters(req: LegacyRequest, clusters: Cluster[], ccs: string) { - const config = req.server.config(); + const config = req.server.config; const start = req.payload.timeRange.min; const end = req.payload.timeRange.max; @@ -62,7 +62,7 @@ export function getKibanasForClusters(req: LegacyRequest, clusters: Cluster[], c kibana_uuids: { terms: { field: 'kibana_stats.kibana.uuid', - size: config.get('monitoring.ui.max_bucket_size'), + size: config.ui.max_bucket_size, }, aggs: { latest_report: { diff --git a/x-pack/plugins/monitoring/server/lib/logs/get_logs.ts b/x-pack/plugins/monitoring/server/lib/logs/get_logs.ts index 5226c04377ffe..9e9f67831ba95 100644 --- a/x-pack/plugins/monitoring/server/lib/logs/get_logs.ts +++ b/x-pack/plugins/monitoring/server/lib/logs/get_logs.ts @@ -20,6 +20,7 @@ import { getTimezone } from '../get_timezone'; import { detectReasonFromException } from './detect_reason_from_exception'; import { LegacyRequest } from '../../types'; import { FilebeatResponse } from '../../../common/types/filebeat'; +import { MonitoringConfig } from '../../config'; interface Log { timestamp?: string | number; @@ -69,7 +70,7 @@ async function handleResponse( } export async function getLogs( - config: { get: (key: string) => any }, + config: MonitoringConfig, req: LegacyRequest, filebeatIndexPattern: string, { @@ -99,7 +100,7 @@ export async function getLogs( const params = { index: filebeatIndexPattern, - size: Math.min(50, config.get('monitoring.ui.elasticsearch.logFetchCount')), + size: Math.min(50, config.ui.elasticsearch.logFetchCount), filter_path: [ 'hits.hits._source.message', 'hits.hits._source.log.level', diff --git a/x-pack/plugins/monitoring/server/lib/logstash/get_logstash_for_clusters.ts b/x-pack/plugins/monitoring/server/lib/logstash/get_logstash_for_clusters.ts index a0be8efe5ebdf..8e4c45b81f7a9 100644 --- a/x-pack/plugins/monitoring/server/lib/logstash/get_logstash_for_clusters.ts +++ b/x-pack/plugins/monitoring/server/lib/logstash/get_logstash_for_clusters.ts @@ -44,7 +44,7 @@ export function getLogstashForClusters( ) { const start = req.payload.timeRange.min; const end = req.payload.timeRange.max; - const config = req.server.config(); + const config = req.server.config; const dataset = 'node_stats'; const type = 'logstash_stats'; @@ -59,6 +59,7 @@ export function getLogstashForClusters( return Promise.all( clusters.map((cluster) => { const clusterUuid = get(cluster, 'elasticsearch.cluster.id', cluster.cluster_uuid); + const maxBucketSize = config.ui.max_bucket_size; const params = { index: indexPatterns, size: 0, @@ -77,7 +78,7 @@ export function getLogstashForClusters( logstash_uuids: { terms: { field: 'logstash_stats.logstash.uuid', - size: config.get('monitoring.ui.max_bucket_size'), + size: maxBucketSize, }, aggs: { latest_report: { @@ -136,7 +137,7 @@ export function getLogstashForClusters( logstash_versions: { terms: { field: 'logstash_stats.logstash.version', - size: config.get('monitoring.ui.max_bucket_size'), + size: maxBucketSize, }, }, pipelines_nested: { @@ -152,7 +153,7 @@ export function getLogstashForClusters( queue_types: { terms: { field: 'logstash_stats.pipelines.queue.type', - size: config.get('monitoring.ui.max_bucket_size'), + size: maxBucketSize, }, aggs: { num_pipelines: { @@ -177,7 +178,7 @@ export function getLogstashForClusters( queue_types: { terms: { field: 'logstash.node.stats.pipelines.queue.type', - size: config.get('monitoring.ui.max_bucket_size'), + size: maxBucketSize, }, aggs: { num_pipelines: { diff --git a/x-pack/plugins/monitoring/server/lib/logstash/get_nodes.test.ts b/x-pack/plugins/monitoring/server/lib/logstash/get_nodes.test.ts index 24e3a84921464..3483d9be5d4a8 100644 --- a/x-pack/plugins/monitoring/server/lib/logstash/get_nodes.test.ts +++ b/x-pack/plugins/monitoring/server/lib/logstash/get_nodes.test.ts @@ -23,19 +23,12 @@ jest.mock('../../static_globals', () => ({ describe('getNodes', () => { it('ensures collapse key is present query responses', async () => { - const configs: { [key: string]: number } = { 'monitoring.ui.max_bucket_size': 10000 }; - const config = { - get: jest.fn().mockImplementation((key: string) => configs[key]), - }; - const response = {}; const callWithRequest = jest.fn().mockResolvedValue(response); const req = { server: { - config() { - return config; - }, + config: { ui: { max_bucket_size: 10000 } }, plugins: { elasticsearch: { getCluster: () => ({ diff --git a/x-pack/plugins/monitoring/server/lib/logstash/get_nodes.ts b/x-pack/plugins/monitoring/server/lib/logstash/get_nodes.ts index e5ee6ebda9150..584a82b6828de 100644 --- a/x-pack/plugins/monitoring/server/lib/logstash/get_nodes.ts +++ b/x-pack/plugins/monitoring/server/lib/logstash/get_nodes.ts @@ -77,7 +77,7 @@ export async function getNodes(req: LegacyRequest, { clusterUuid }: { clusterUui dataset, }); - const config = req.server.config(); + const config = req.server.config; const start = moment.utc(req.payload.timeRange.min).valueOf(); const end = moment.utc(req.payload.timeRange.max).valueOf(); @@ -85,7 +85,7 @@ export async function getNodes(req: LegacyRequest, { clusterUuid }: { clusterUui const params = { index: indexPatterns, - size: config.get('monitoring.ui.max_bucket_size'), // FIXME + size: config.ui.max_bucket_size, ignore_unavailable: true, body: { query: createQuery({ diff --git a/x-pack/plugins/monitoring/server/lib/logstash/get_paginated_pipelines.ts b/x-pack/plugins/monitoring/server/lib/logstash/get_paginated_pipelines.ts index 42c6a684234c4..517d71de25d6b 100644 --- a/x-pack/plugins/monitoring/server/lib/logstash/get_paginated_pipelines.ts +++ b/x-pack/plugins/monitoring/server/lib/logstash/get_paginated_pipelines.ts @@ -63,9 +63,8 @@ export async function getPaginatedPipelines({ }: GetPaginatedPipelinesParams) { const { throughputMetric, nodesCountMetric } = metrics; const sortField = sort.field; - const config = req.server.config(); - // TODO type config - const size = config.get('monitoring.ui.max_bucket_size') as unknown as number; + const config = req.server.config; + const size = config.ui.max_bucket_size; let pipelines = await getLogstashPipelineIds({ req, clusterUuid, diff --git a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline.ts b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline.ts index 16a96b132483f..8bc299b57e68b 100644 --- a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline.ts +++ b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline.ts @@ -15,6 +15,7 @@ import { ElasticsearchSource, ElasticsearchSourceLogstashPipelineVertex, } from '../../../common/types/es'; +import { MonitoringConfig } from '../../config'; export function _vertexStats( vertex: ElasticsearchSourceLogstashPipelineVertex, @@ -120,13 +121,13 @@ export function _enrichStateWithStatsAggregation( export async function getPipeline( req: LegacyRequest, - config: { get: (key: string) => string | undefined }, + config: MonitoringConfig, clusterUuid: string, pipelineId: string, version: PipelineVersion ) { // Determine metrics' timeseries interval based on version's timespan - const minIntervalSeconds = Math.max(Number(config.get('monitoring.ui.min_interval_seconds')), 30); + const minIntervalSeconds = Math.max(config.ui.min_interval_seconds, 30); const timeseriesInterval = calculateTimeseriesInterval( Number(version.firstSeen), Number(version.lastSeen), diff --git a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_stats_aggregation.ts b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_stats_aggregation.ts index 31cfa4b4a0291..0165f84e48d6e 100644 --- a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_stats_aggregation.ts +++ b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_stats_aggregation.ts @@ -15,7 +15,7 @@ function scalarCounterAggregation( field: string, fieldPath: string, ephemeralIdField: string, - maxBucketSize: string + maxBucketSize: number ) { const fullPath = `${fieldPath}.${field}`; @@ -54,7 +54,7 @@ function scalarCounterAggregation( return aggs; } -function nestedVertices(statsPath: string, maxBucketSize: string) { +function nestedVertices(statsPath: string, maxBucketSize: number) { const fieldPath = `${statsPath}.pipelines.vertices`; const ephemeralIdField = `${statsPath}.pipelines.vertices.pipeline_ephemeral_id`; @@ -81,7 +81,7 @@ function nestedVertices(statsPath: string, maxBucketSize: string) { }; } -function createScopedAgg(pipelineId: string, pipelineHash: string, maxBucketSize: string) { +function createScopedAgg(pipelineId: string, pipelineHash: string, maxBucketSize: number) { return (statsPath: string) => { const verticesAgg = { vertices: nestedVertices(statsPath, maxBucketSize), @@ -115,7 +115,7 @@ function fetchPipelineLatestStats( query: object, pipelineId: string, version: PipelineVersion, - maxBucketSize: string, + maxBucketSize: number, callWithRequest: any, req: LegacyRequest ) { @@ -219,15 +219,13 @@ export function getPipelineStatsAggregation({ filters, }); - const config = req.server.config(); + const config = req.server.config; return fetchPipelineLatestStats( query, pipelineId, version, - // @ts-ignore not undefined, need to get correct config - // https://github.com/elastic/kibana/issues/112146 - config.get('monitoring.ui.max_bucket_size'), + config.ui.max_bucket_size, callWithRequest, req ); diff --git a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_versions.ts b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_versions.ts index eecc4388f8947..56d56247c5e09 100644 --- a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_versions.ts +++ b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_versions.ts @@ -78,7 +78,7 @@ function fetchPipelineVersions({ moduleType, dataset, }); - const config = req.server.config(); + const config = req.server.config; const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('monitoring'); const filters = [ @@ -120,10 +120,7 @@ function fetchPipelineVersions({ filters, }); - const pipelineAggregation = createScopedAgg( - pipelineId, - Number(config.get('monitoring.ui.max_bucket_size')) - ); + const pipelineAggregation = createScopedAgg(pipelineId, config.ui.max_bucket_size); const aggs = { pipelines: pipelineAggregation('logstash_stats'), pipelines_mb: pipelineAggregation('logstash.node.stats'), diff --git a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_vertex.ts b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_vertex.ts index 5605b23b8988b..fcb38f2edc0a5 100644 --- a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_vertex.ts +++ b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_vertex.ts @@ -15,6 +15,7 @@ import { ElasticsearchSource, ElasticsearchSourceLogstashPipelineVertex, } from '../../../common/types/es'; +import { MonitoringConfig } from '../../config'; export function _vertexStats( vertex: ElasticsearchSourceLogstashPipelineVertex, @@ -135,14 +136,14 @@ export function _enrichVertexStateWithStatsAggregation( export async function getPipelineVertex( req: LegacyRequest, - config: { get: (key: string) => string | undefined }, + config: MonitoringConfig, clusterUuid: string, pipelineId: string, version: PipelineVersion, vertexId: string ) { // Determine metrics' timeseries interval based on version's timespan - const minIntervalSeconds = Math.max(Number(config.get('monitoring.ui.min_interval_seconds')), 30); + const minIntervalSeconds = Math.max(config.ui.min_interval_seconds, 30); const timeseriesInterval = calculateTimeseriesInterval( Number(version.firstSeen), Number(version.lastSeen), diff --git a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_vertex_stats_aggregation.ts b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_vertex_stats_aggregation.ts index 8b26f5d44855b..a13f9abbcb728 100644 --- a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_vertex_stats_aggregation.ts +++ b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_vertex_stats_aggregation.ts @@ -271,7 +271,7 @@ export function getPipelineVertexStatsAggregation({ filters, }); - const config = req.server.config(); + const config = req.server.config; return fetchPipelineVertexTimeSeriesStats({ query, @@ -279,9 +279,7 @@ export function getPipelineVertexStatsAggregation({ version, vertexId, timeSeriesIntervalInSeconds, - // @ts-ignore not undefined, need to get correct config - // https://github.com/elastic/kibana/issues/112146 - maxBucketSize: config.get('monitoring.ui.max_bucket_size'), + maxBucketSize: config.ui.max_bucket_size, callWithRequest, req, }); diff --git a/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.test.js b/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.test.js index bbcedf7fdd33d..214e8d5907443 100644 --- a/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.test.js +++ b/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.test.js @@ -17,6 +17,7 @@ const mockReq = ( ) => { return { server: { + instanceUuid: 'kibana-1234', newPlatform: { setup: { plugins: { @@ -28,15 +29,7 @@ const mockReq = ( }, }, }, - config() { - return { - get: jest.fn((prop) => { - if (prop === 'server.uuid') { - return 'kibana-1234'; - } - }), - }; - }, + config: { ui: { ccs: { enabled: false } } }, usage: { collectorSet: { getCollectorByType: () => ({ diff --git a/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.ts b/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.ts index 463ccf547d5db..3f306b3596f6a 100644 --- a/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.ts +++ b/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.ts @@ -35,7 +35,7 @@ const getRecentMonitoringDocuments = async ( indexPatterns: Record, clusterUuid?: string, nodeUuid?: string, - size?: string + size?: number ) => { const start = get(req.payload, 'timeRange.min') || `now-${NUMBER_OF_SECONDS_AGO_TO_LOOK}s`; const end = get(req.payload, 'timeRange.max') || 'now'; @@ -431,9 +431,9 @@ export const getCollectionStatus = async ( nodeUuid?: string, skipLiveData?: boolean ) => { - const config = req.server.config(); - const kibanaUuid = config.get('server.uuid'); - const size = config.get('monitoring.ui.max_bucket_size'); + const config = req.server.config; + const kibanaUuid = req.server.instanceUuid; + const size = config.ui.max_bucket_size; const hasPermissions = await hasNecessaryPermissions(req); if (!hasPermissions) { diff --git a/x-pack/plugins/monitoring/server/plugin.ts b/x-pack/plugins/monitoring/server/plugin.ts index ff07ea0f4a26d..ea3df28b1965d 100644 --- a/x-pack/plugins/monitoring/server/plugin.ts +++ b/x-pack/plugins/monitoring/server/plugin.ts @@ -19,9 +19,8 @@ import { Plugin, PluginInitializerContext, ResponseError, - SharedGlobalConfig, } from 'kibana/server'; -import { get, has } from 'lodash'; +import { get } from 'lodash'; import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/server'; import { KIBANA_MONITORING_LOGGING_TAG, @@ -44,6 +43,7 @@ import { IBulkUploader, LegacyRequest, LegacyShimDependencies, + MonitoringConfigSchema, MonitoringCore, MonitoringLicenseService, MonitoringPluginSetup, @@ -78,7 +78,6 @@ export class MonitoringPlugin private bulkUploader?: IBulkUploader; private readonly config: MonitoringConfig; - private readonly legacyConfig: SharedGlobalConfig; private coreSetup?: CoreSetup; private setupPlugins?: PluginsSetup; @@ -86,8 +85,7 @@ export class MonitoringPlugin this.initializerContext = initializerContext; this.log = initializerContext.logger.get(LOGGING_TAG); this.getLogger = (...scopes: string[]) => initializerContext.logger.get(LOGGING_TAG, ...scopes); - this.config = createConfig(this.initializerContext.config.get>()); - this.legacyConfig = this.initializerContext.config.legacy.get(); + this.config = createConfig(this.initializerContext.config.get()); } setup(coreSetup: CoreSetup, plugins: PluginsSetup) { @@ -119,7 +117,6 @@ export class MonitoringPlugin config: this.config!, getLogger: this.getLogger, log: this.log, - legacyConfig: this.legacyConfig, coreSetup: this.coreSetup!, setupPlugins: this.setupPlugins!, }); @@ -165,8 +162,7 @@ export class MonitoringPlugin } init(cluster: ICustomClusterClient, coreStart: CoreStart) { - const config = createConfig(this.initializerContext.config.get>()); - const legacyConfig = this.initializerContext.config.legacy.get(); + const config = createConfig(this.initializerContext.config.get()); const coreSetup = this.coreSetup!; const plugins = this.setupPlugins!; @@ -181,13 +177,12 @@ export class MonitoringPlugin ), }; - // If the UI is enabled, then we want to register it so it shows up + // If the UI is enabled, then we want to register it, so it shows up // and start any other UI-related setup tasks if (config.ui.enabled) { // Create our shim which is currently used to power our routing this.monitoringCore = this.getLegacyShim( config, - legacyConfig, coreSetup.getStartServices as () => Promise<[CoreStart, PluginsStart, {}]>, cluster, plugins @@ -311,37 +306,19 @@ export class MonitoringPlugin getLegacyShim( config: MonitoringConfig, - legacyConfig: any, getCoreServices: () => Promise<[CoreStart, PluginsStart, {}]>, cluster: ICustomClusterClient, setupPlugins: PluginsSetup ): MonitoringCore { const router = this.legacyShimDependencies.router; - const legacyConfigWrapper = () => ({ - get: (_key: string): string | undefined => { - const key = _key.includes('monitoring.') ? _key.split('monitoring.')[1] : _key; - if (has(config, key)) { - return get(config, key); - } - if (has(legacyConfig, key)) { - return get(legacyConfig, key); - } - - if (key === 'server.uuid') { - return this.legacyShimDependencies.instanceUuid; - } - - throw new Error(`Unknown key '${_key}'`); - }, - }); return { - config: legacyConfigWrapper, + config, log: this.log, route: (options: any) => { const method = options.method; const handler = async ( context: RequestHandlerContextMonitoringPlugin, - req: KibanaRequest, + req: KibanaRequest, res: KibanaResponseFactory ) => { const plugins = (await getCoreServices())[1]; @@ -370,9 +347,10 @@ export class MonitoringPlugin } }, server: { + instanceUuid: this.legacyShimDependencies.instanceUuid, log: this.log, route: () => {}, - config: legacyConfigWrapper, + config, newPlatform: { setup: { plugins: setupPlugins, @@ -424,7 +402,7 @@ export class MonitoringPlugin } else if (method === 'PUT') { router.put(options, handler); } else { - throw new Error('Unsupport API method: ' + method); + throw new Error('Unsupported API method: ' + method); } }, }; diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/apm/instance.js b/x-pack/plugins/monitoring/server/routes/api/v1/apm/instance.js index 4fa0de7d399d9..6c590682b4030 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/apm/instance.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/apm/instance.js @@ -34,12 +34,12 @@ export function apmInstanceRoute(server) { }, async handler(req) { const apmUuid = req.params.apmUuid; - const config = server.config(); + const config = server.config; const clusterUuid = req.params.clusterUuid; const ccs = req.payload.ccs; const apmIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_BEATS, ccs); - const showCgroupMetrics = config.get('monitoring.ui.container.apm.enabled'); + const showCgroupMetrics = config.ui.container.apm.enabled; if (showCgroupMetrics) { const metricCpu = metricSet.find((m) => m.name === 'apm_cpu'); metricCpu.keys = ['apm_cgroup_cpu']; diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/apm/instances.js b/x-pack/plugins/monitoring/server/routes/api/v1/apm/instances.js index 95f378ff5b98d..1d6a22fdf0c93 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/apm/instances.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/apm/instances.js @@ -30,7 +30,7 @@ export function apmInstancesRoute(server) { }, }, async handler(req) { - const config = server.config(); + const config = server.config; const ccs = req.payload.ccs; const clusterUuid = req.params.clusterUuid; const apmIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_BEATS, ccs); @@ -44,7 +44,7 @@ export function apmInstancesRoute(server) { return { stats, apms, - cgroup: req.server.config().get('monitoring.ui.container.apm.enabled'), + cgroup: config.ui.container.apm.enabled, }; } catch (err) { return handleError(err, req); diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/apm/overview.js b/x-pack/plugins/monitoring/server/routes/api/v1/apm/overview.js index b773cfd7b0fb9..1e5e125d7e6c9 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/apm/overview.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/apm/overview.js @@ -30,10 +30,10 @@ export function apmOverviewRoute(server) { }, }, async handler(req) { - const config = server.config(); + const config = server.config; const clusterUuid = req.params.clusterUuid; - const showCgroupMetrics = config.get('monitoring.ui.container.apm.enabled'); + const showCgroupMetrics = config.ui.container.apm.enabled; if (showCgroupMetrics) { const metricCpu = metricSet.find((m) => m.name === 'apm_cpu'); metricCpu.keys = ['apm_cgroup_cpu']; diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/beats/beat_detail.js b/x-pack/plugins/monitoring/server/routes/api/v1/beats/beat_detail.js index d9454b7ae62cf..cdab320f7930d 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/beats/beat_detail.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/beats/beat_detail.js @@ -35,7 +35,7 @@ export function beatsDetailRoute(server) { async handler(req) { const clusterUuid = req.params.clusterUuid; const beatUuid = req.params.beatUuid; - const config = server.config(); + const config = server.config; const ccs = req.payload.ccs; const beatsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_BEATS, ccs); diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/beats/beats.js b/x-pack/plugins/monitoring/server/routes/api/v1/beats/beats.js index fa35ccb9371c2..f63f8e4d8cecd 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/beats/beats.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/beats/beats.js @@ -30,7 +30,7 @@ export function beatsListingRoute(server) { }, }, async handler(req) { - const config = server.config(); + const config = server.config; const ccs = req.payload.ccs; const clusterUuid = req.params.clusterUuid; const beatsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_BEATS, ccs); diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/beats/overview.js b/x-pack/plugins/monitoring/server/routes/api/v1/beats/overview.js index 12349c8b85571..e6df588bc5a9a 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/beats/overview.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/beats/overview.js @@ -32,7 +32,7 @@ export function beatsOverviewRoute(server) { }, }, async handler(req) { - const config = server.config(); + const config = server.config; const ccs = req.payload.ccs; const clusterUuid = req.params.clusterUuid; const beatsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_BEATS, ccs); diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/cluster/cluster.ts b/x-pack/plugins/monitoring/server/routes/api/v1/cluster/cluster.ts index 2629d763d656d..ecfb62c7d2c4b 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/cluster/cluster.ts +++ b/x-pack/plugins/monitoring/server/routes/api/v1/cluster/cluster.ts @@ -35,10 +35,10 @@ export function clusterRoute(server: LegacyServer) { }, }, handler: async (req: LegacyRequest) => { - const config = server.config(); + const config = server.config; const indexPatterns = getIndexPatterns(server, { - filebeatIndexPattern: config.get('monitoring.ui.logs.index')!, + filebeatIndexPattern: config.ui.logs.index, }); const options = { clusterUuid: req.params.clusterUuid, diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/cluster/clusters.js b/x-pack/plugins/monitoring/server/routes/api/v1/cluster/clusters.js index 6bf2550bb720d..2a1ec03f93db6 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/cluster/clusters.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/cluster/clusters.js @@ -32,7 +32,7 @@ export function clustersRoute(server) { }, handler: async (req) => { let clusters = []; - const config = server.config(); + const config = server.config; // NOTE using try/catch because checkMonitoringAuth is expected to throw // an error when current logged-in user doesn't have permission to read @@ -40,7 +40,7 @@ export function clustersRoute(server) { try { await verifyMonitoringAuth(req); const indexPatterns = getIndexPatterns(server, { - filebeatIndexPattern: config.get('monitoring.ui.logs.index'), + filebeatIndexPattern: config.ui.logs.index, }); clusters = await getClustersFromRequest(req, indexPatterns, { codePaths: req.payload.codePaths, diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ccr.ts b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ccr.ts index 898cfc82463d9..7ffbdf98eba41 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ccr.ts +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ccr.ts @@ -19,6 +19,7 @@ import { ElasticsearchMetricbeatSource, } from '../../../../../common/types/es'; import { LegacyRequest } from '../../../../types'; +import { MonitoringConfig } from '../../../../config'; function getBucketScript(max: string, min: string) { return { @@ -32,16 +33,10 @@ function getBucketScript(max: string, min: string) { }; } -function buildRequest( - req: LegacyRequest, - config: { - get: (key: string) => string | undefined; - }, - esIndexPattern: string -) { +function buildRequest(req: LegacyRequest, config: MonitoringConfig, esIndexPattern: string) { const min = moment.utc(req.payload.timeRange.min).valueOf(); const max = moment.utc(req.payload.timeRange.max).valueOf(); - const maxBucketSize = config.get('monitoring.ui.max_bucket_size'); + const maxBucketSize = config.ui.max_bucket_size; const aggs = { ops_synced_max: { max: { @@ -200,12 +195,7 @@ function buildRequest( }; } -export function ccrRoute(server: { - route: (p: any) => void; - config: () => { - get: (key: string) => string | undefined; - }; -}) { +export function ccrRoute(server: { route: (p: any) => void; config: MonitoringConfig }) { server.route({ method: 'POST', path: '/api/monitoring/v1/clusters/{clusterUuid}/elasticsearch/ccr', @@ -224,7 +214,7 @@ export function ccrRoute(server: { }, }, async handler(req: LegacyRequest) { - const config = server.config(); + const config = server.config; const ccs = req.payload.ccs; const esIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_ELASTICSEARCH, ccs); diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/index_detail.js b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/index_detail.js index 6c6b1487da106..e8cdc1f6e30cd 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/index_detail.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/index_detail.js @@ -40,17 +40,12 @@ export function esIndexRoute(server) { }, handler: async (req) => { try { - const config = server.config(); + const config = server.config; const clusterUuid = req.params.clusterUuid; const indexUuid = req.params.id; const start = req.payload.timeRange.min; const end = req.payload.timeRange.max; - const filebeatIndexPattern = prefixIndexPattern( - config, - config.get('monitoring.ui.logs.index'), - '*', - true - ); + const filebeatIndexPattern = prefixIndexPattern(config, config.ui.logs.index, '*'); const isAdvanced = req.payload.is_advanced; const metricSet = isAdvanced ? metricSetAdvanced : metricSetOverview; diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/node_detail.js b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/node_detail.js index 86048ed1765ab..28a6ee2decca6 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/node_detail.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/node_detail.js @@ -40,19 +40,14 @@ export function esNodeRoute(server) { }, }, async handler(req) { - const config = server.config(); + const config = server.config; const ccs = req.payload.ccs; const showSystemIndices = req.payload.showSystemIndices; const clusterUuid = req.params.clusterUuid; const nodeUuid = req.params.nodeUuid; const start = req.payload.timeRange.min; const end = req.payload.timeRange.max; - const filebeatIndexPattern = prefixIndexPattern( - config, - config.get('monitoring.ui.logs.index'), - '*', - true - ); + const filebeatIndexPattern = prefixIndexPattern(config, config.ui.logs.index, '*'); const isAdvanced = req.payload.is_advanced; let metricSet; @@ -61,9 +56,7 @@ export function esNodeRoute(server) { } else { metricSet = metricSetOverview; // set the cgroup option if needed - const showCgroupMetricsElasticsearch = config.get( - 'monitoring.ui.container.elasticsearch.enabled' - ); + const showCgroupMetricsElasticsearch = config.ui.container.elasticsearch.enabled; const metricCpu = metricSet.find((m) => m.name === 'node_cpu_metric'); if (showCgroupMetricsElasticsearch) { metricCpu.keys = ['node_cgroup_quota_as_cpu_utilization']; diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/overview.js b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/overview.js index 6c74f9dd872d2..341193f173439 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/overview.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/overview.js @@ -35,14 +35,9 @@ export function esOverviewRoute(server) { }, }, async handler(req) { - const config = server.config(); + const config = server.config; const clusterUuid = req.params.clusterUuid; - const filebeatIndexPattern = prefixIndexPattern( - config, - config.get('monitoring.ui.logs.index'), - '*', - true - ); + const filebeatIndexPattern = prefixIndexPattern(config, config.ui.logs.index, '*'); const start = req.payload.timeRange.min; const end = req.payload.timeRange.max; @@ -51,7 +46,8 @@ export function esOverviewRoute(server) { const [clusterStats, metrics, shardActivity, logs] = await Promise.all([ getClusterStats(req, clusterUuid), getMetrics(req, 'elasticsearch', metricSet), - getLastRecovery(req, config.get('monitoring.ui.max_bucket_size')), + getLastRecovery(req, config.ui.max_bucket_size), + // TODO this call is missing some items from the signature of `getLogs`, will need to resolve during TS conversion getLogs(config, req, filebeatIndexPattern, { clusterUuid, start, end }), ]); const indicesUnassignedShardStats = await getIndicesUnassignedShardStats(req, clusterStats); diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts index a99729407931d..2482517bf4f4a 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/internal_monitoring.ts @@ -87,7 +87,7 @@ export function internalMonitoringCheckRoute(server: LegacyServer, npRoute: Rout mb_indices: 0, }; - const config = server.config(); + const config = server.config; const { ccs } = request.body; const esIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_ELASTICSEARCH, ccs); const kbnIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_KIBANA, ccs); diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/logstash/node.js b/x-pack/plugins/monitoring/server/routes/api/v1/logstash/node.js index 9c7749bf74903..be7ce5ad0d3c2 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/logstash/node.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/logstash/node.js @@ -47,7 +47,7 @@ export function logstashNodeRoute(server) { }, }, async handler(req) { - const config = server.config(); + const config = server.config; const clusterUuid = req.params.clusterUuid; const logstashUuid = req.params.logstashUuid; @@ -57,7 +57,7 @@ export function logstashNodeRoute(server) { } else { metricSet = metricSetOverview; // set the cgroup option if needed - const showCgroupMetricsLogstash = config.get('monitoring.ui.container.logstash.enabled'); + const showCgroupMetricsLogstash = config.ui.container.logstash.enabled; const metricCpu = metricSet.find((m) => m.name === 'logstash_node_cpu_metric'); if (showCgroupMetricsLogstash) { metricCpu.keys = ['logstash_node_cgroup_quota_as_cpu_utilization']; diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/logstash/nodes.js b/x-pack/plugins/monitoring/server/routes/api/v1/logstash/nodes.js index cca02519aa7c4..dd27dcec80605 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/logstash/nodes.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/logstash/nodes.js @@ -9,8 +9,6 @@ import { schema } from '@kbn/config-schema'; import { getClusterStatus } from '../../../../lib/logstash/get_cluster_status'; import { getNodes } from '../../../../lib/logstash/get_nodes'; import { handleError } from '../../../../lib/errors'; -import { prefixIndexPattern } from '../../../../../common/ccs_utils'; -import { INDEX_PATTERN_LOGSTASH } from '../../../../../common/constants'; /* * Logstash Nodes route. @@ -44,15 +42,12 @@ export function logstashNodesRoute(server) { }, }, async handler(req) { - const config = server.config(); - const ccs = req.payload.ccs; const clusterUuid = req.params.clusterUuid; - const lsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_LOGSTASH, ccs); try { const [clusterStatus, nodes] = await Promise.all([ getClusterStatus(req, { clusterUuid }), - getNodes(req, lsIndexPattern, { clusterUuid }), + getNodes(req, { clusterUuid }), ]); return { diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/logstash/pipeline.js b/x-pack/plugins/monitoring/server/routes/api/v1/logstash/pipeline.js index 59f91b82ca497..a9c3906dad675 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/logstash/pipeline.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/logstash/pipeline.js @@ -45,7 +45,7 @@ export function logstashPipelineRoute(server) { }, }, handler: async (req) => { - const config = server.config(); + const config = server.config; const clusterUuid = req.params.clusterUuid; const detailVertexId = req.payload.detailVertexId; @@ -66,6 +66,7 @@ export function logstashPipelineRoute(server) { } const version = getPipelineVersion(versions, pipelineHash); + // noinspection ES6MissingAwait const promises = [getPipeline(req, config, clusterUuid, pipelineId, version)]; if (detailVertexId) { promises.push( diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/logstash/pipelines/cluster_pipeline_ids.js b/x-pack/plugins/monitoring/server/routes/api/v1/logstash/pipelines/cluster_pipeline_ids.js index 7f14b74da207d..bd9de876f8903 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/logstash/pipelines/cluster_pipeline_ids.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/logstash/pipelines/cluster_pipeline_ids.js @@ -7,8 +7,6 @@ import { schema } from '@kbn/config-schema'; import { handleError } from '../../../../../lib/errors'; -import { prefixIndexPattern } from '../../../../../../common/ccs_utils'; -import { INDEX_PATTERN_LOGSTASH } from '../../../../../../common/constants'; import { getLogstashPipelineIds } from '../../../../../lib/logstash/get_pipeline_ids'; /** @@ -33,15 +31,12 @@ export function logstashClusterPipelineIdsRoute(server) { }, }, handler: async (req) => { - const config = server.config(); - const { ccs } = req.payload; + const config = server.config; const clusterUuid = req.params.clusterUuid; - const lsIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_LOGSTASH, ccs); - const size = config.get('monitoring.ui.max_bucket_size'); + const size = config.ui.max_bucket_size; try { - const pipelines = await getLogstashPipelineIds({ req, lsIndexPattern, clusterUuid, size }); - return pipelines; + return await getLogstashPipelineIds({ req, clusterUuid, size }); } catch (err) { throw handleError(err, req); } diff --git a/x-pack/plugins/monitoring/server/static_globals.ts b/x-pack/plugins/monitoring/server/static_globals.ts index c93d1a6359763..089d47f4d45d4 100644 --- a/x-pack/plugins/monitoring/server/static_globals.ts +++ b/x-pack/plugins/monitoring/server/static_globals.ts @@ -5,13 +5,7 @@ * 2.0. */ -import { - CoreSetup, - ElasticsearchClient, - Logger, - SharedGlobalConfig, - PluginInitializerContext, -} from 'kibana/server'; +import { CoreSetup, ElasticsearchClient, Logger, PluginInitializerContext } from 'kibana/server'; import url from 'url'; import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { MonitoringConfig } from './config'; @@ -25,7 +19,6 @@ interface InitSetupOptions { config: MonitoringConfig; getLogger: GetLogger; log: Logger; - legacyConfig: SharedGlobalConfig; setupPlugins: PluginsSetup; } diff --git a/x-pack/plugins/monitoring/server/types.ts b/x-pack/plugins/monitoring/server/types.ts index 14023ccce41ae..d3809117b90f2 100644 --- a/x-pack/plugins/monitoring/server/types.ts +++ b/x-pack/plugins/monitoring/server/types.ts @@ -16,6 +16,7 @@ import type { import type Boom from '@hapi/boom'; import { errors } from '@elastic/elasticsearch'; import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; +import { TypeOf } from '@kbn/config-schema'; import { LicenseFeature, ILicense } from '../../licensing/server'; import type { PluginStartContract as ActionsPluginsStartContact, @@ -35,6 +36,7 @@ import { EncryptedSavedObjectsPluginSetup } from '../../encrypted_saved_objects/ import { CloudSetup } from '../../cloud/server'; import { ElasticsearchModifiedSource } from '../common/types/es'; import { RulesByType } from '../common/types/alerts'; +import { configSchema, MonitoringConfig } from './config'; export interface MonitoringLicenseService { refresh: () => Promise; @@ -46,10 +48,6 @@ export interface MonitoringLicenseService { stop: () => void; } -export interface MonitoringElasticsearchConfig { - hosts: string[]; -} - export interface PluginsSetup { encryptedSavedObjects?: EncryptedSavedObjectsPluginSetup; usageCollection?: UsageCollectionSetup; @@ -72,10 +70,6 @@ export interface PluginsStart { licensing: LicensingPluginStart; } -export interface MonitoringCoreConfig { - get: (key: string) => string | undefined; -} - export interface RouteDependencies { cluster: ICustomClusterClient; router: IRouter; @@ -86,7 +80,7 @@ export interface RouteDependencies { } export interface MonitoringCore { - config: () => MonitoringCoreConfig; + config: MonitoringConfig; log: Logger; route: (options: any) => void; } @@ -127,11 +121,10 @@ export interface LegacyRequest { } export interface LegacyServer { + instanceUuid: string; log: Logger; route: (params: any) => void; - config: () => { - get: (key: string) => string | undefined; - }; + config: MonitoringConfig; newPlatform: { setup: { plugins: PluginsSetup; @@ -257,3 +250,5 @@ export interface PipelineVersion { lastSeen: number; hash: string; } + +export type MonitoringConfigSchema = TypeOf;