From 25db1df1a3f399f1e7537ff9d186c152eca37223 Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Tue, 29 Jun 2021 17:07:56 -0700 Subject: [PATCH] [Monitoring] Migrated legacy Elasticsearch client for 8.0 (#101850) --- .../server/es_client/instantiate_client.ts | 6 +- .../collectors/get_usage_collector.test.ts | 19 +- .../collectors/get_usage_collector.ts | 16 +- .../kibana_monitoring/collectors/index.ts | 6 +- .../collectors/lib/fetch_es_usage.test.ts | 110 ++++++------ .../collectors/lib/fetch_es_usage.ts | 34 +--- .../collectors/lib/fetch_license_type.test.ts | 41 +++-- .../collectors/lib/fetch_license_type.ts | 11 +- .../lib/fetch_stack_product_usage.test.ts | 99 +++++++---- .../lib/fetch_stack_product_usage.ts | 14 +- .../lib/get_stack_products_usage.test.ts | 15 +- .../lib/get_stack_products_usage.ts | 6 +- .../alerts/disable_watcher_cluster_alerts.ts | 17 +- .../lib/alerts/fetch_ccr_read_exceptions.ts | 2 +- .../server/lib/alerts/fetch_cluster_health.ts | 2 +- .../server/lib/alerts/fetch_clusters.ts | 4 +- .../alerts/fetch_cpu_usage_node_stats.test.ts | 2 +- .../lib/alerts/fetch_cpu_usage_node_stats.ts | 3 +- .../lib/alerts/fetch_disk_usage_node_stats.ts | 2 +- .../alerts/fetch_elasticsearch_versions.ts | 2 +- .../lib/alerts/fetch_index_shard_size.ts | 2 +- .../lib/alerts/fetch_kibana_versions.ts | 2 +- .../server/lib/alerts/fetch_licenses.ts | 2 +- .../lib/alerts/fetch_logstash_versions.ts | 2 +- .../alerts/fetch_memory_usage_node_stats.ts | 2 +- .../alerts/fetch_missing_monitoring_data.ts | 2 +- .../alerts/fetch_nodes_from_cluster_stats.ts | 2 +- .../fetch_thread_pool_rejections_stats.ts | 2 +- .../server/lib/apm/_get_time_of_last_event.ts | 2 +- .../monitoring/server/lib/apm/get_apm_info.ts | 4 +- .../monitoring/server/lib/apm/get_apms.ts | 4 +- .../server/lib/apm/get_apms_for_clusters.js | 4 +- .../monitoring/server/lib/apm/get_stats.js | 4 +- .../server/lib/beats/get_beat_summary.ts | 4 +- .../monitoring/server/lib/beats/get_beats.ts | 4 +- .../lib/beats/get_beats_for_clusters.js | 4 +- .../server/lib/beats/get_latest_stats.js | 4 +- .../monitoring/server/lib/beats/get_stats.js | 4 +- .../lib/cluster/flag_supported_clusters.ts | 4 +- .../server/lib/cluster/get_cluster_license.ts | 4 +- .../server/lib/cluster/get_clusters_state.ts | 4 +- .../server/lib/cluster/get_clusters_stats.ts | 4 +- .../server/lib/details/get_series.js | 2 +- .../server/lib/elasticsearch/ccr.ts | 4 +- .../lib/elasticsearch/get_last_recovery.ts | 2 +- .../server/lib/elasticsearch/get_ml_jobs.ts | 8 +- .../indices/get_index_summary.ts | 2 +- .../lib/elasticsearch/indices/get_indices.ts | 4 +- .../elasticsearch/nodes/get_node_summary.ts | 2 +- .../nodes/get_nodes/get_node_ids.js | 4 +- .../nodes/get_nodes/get_nodes.ts | 4 +- .../get_indices_unassigned_shard_stats.ts | 2 +- .../shards/get_nodes_shard_count.ts | 2 +- .../shards/get_shard_allocation.ts | 2 +- .../elasticsearch/shards/get_shard_stats.ts | 2 +- .../elasticsearch/verify_monitoring_auth.js | 2 +- .../lib/elasticsearch_settings/cluster.js | 11 +- .../server/lib/kibana/get_kibana_info.ts | 4 +- .../server/lib/kibana/get_kibanas.ts | 2 +- .../lib/kibana/get_kibanas_for_clusters.js | 2 +- .../server/lib/logs/get_log_types.ts | 4 +- .../monitoring/server/lib/logs/get_logs.ts | 4 +- .../lib/logstash/get_logstash_for_clusters.js | 2 +- .../server/lib/logstash/get_node_info.ts | 4 +- .../server/lib/logstash/get_nodes.ts | 2 +- .../server/lib/logstash/get_pipeline_ids.js | 4 +- .../logstash/get_pipeline_state_document.ts | 2 +- .../get_pipeline_stats_aggregation.js | 4 +- .../lib/logstash/get_pipeline_versions.js | 2 +- .../get_pipeline_vertex_stats_aggregation.js | 4 +- .../setup/collection/get_collection_status.js | 8 +- .../monitoring/server/license_service.ts | 14 +- x-pack/plugins/monitoring/server/plugin.ts | 162 ++++++++++-------- .../server/routes/api/v1/alerts/enable.ts | 2 +- .../server/routes/api/v1/elasticsearch/ccr.ts | 2 +- .../routes/api/v1/elasticsearch/ccr_shard.ts | 2 +- .../elasticsearch_settings/check/cluster.js | 1 + .../check/internal_monitoring.ts | 9 +- .../monitoring/server/static_globals.ts | 71 ++++++-- .../get_all_stats.test.ts | 25 ++- .../telemetry_collection/get_all_stats.ts | 4 +- .../get_beats_stats.test.ts | 24 +-- .../telemetry_collection/get_beats_stats.ts | 23 +-- .../get_cluster_uuids.test.ts | 33 ++-- .../telemetry_collection/get_cluster_uuids.ts | 18 +- .../telemetry_collection/get_es_stats.test.ts | 22 +-- .../telemetry_collection/get_es_stats.ts | 18 +- .../get_high_level_stats.test.ts | 20 ++- .../get_high_level_stats.ts | 27 +-- .../telemetry_collection/get_kibana_stats.ts | 4 +- .../telemetry_collection/get_licenses.test.ts | 20 ++- .../telemetry_collection/get_licenses.ts | 18 +- .../get_logstash_stats.test.ts | 45 +++-- .../get_logstash_stats.ts | 48 +++--- ...egister_monitoring_telemetry_collection.ts | 10 +- x-pack/plugins/monitoring/server/types.ts | 7 +- 96 files changed, 685 insertions(+), 526 deletions(-) diff --git a/x-pack/plugins/monitoring/server/es_client/instantiate_client.ts b/x-pack/plugins/monitoring/server/es_client/instantiate_client.ts index a32943dc10272a..58da336a5447dc 100644 --- a/x-pack/plugins/monitoring/server/es_client/instantiate_client.ts +++ b/x-pack/plugins/monitoring/server/es_client/instantiate_client.ts @@ -6,7 +6,7 @@ */ import { ConfigOptions } from 'elasticsearch'; -import { Logger, ILegacyCustomClusterClient } from 'kibana/server'; +import { Logger, ICustomClusterClient, ElasticsearchClientConfig } from 'kibana/server'; // @ts-ignore import { monitoringBulk } from '../kibana_monitoring/lib/monitoring_bulk'; import { monitoringEndpointDisableWatches } from './monitoring_endpoint_disable_watches'; @@ -25,8 +25,8 @@ export function instantiateClient( log: Logger, createClient: ( type: string, - clientConfig?: Partial - ) => ILegacyCustomClusterClient + clientConfig?: Partial | undefined + ) => ICustomClusterClient ) { const isMonitoringCluster = hasMonitoringCluster(elasticsearchConfig); const cluster = createClient('monitoring', { 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 03a3659b49ce19..9c72c1f8d3841b 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 @@ -6,11 +6,11 @@ */ import { getMonitoringUsageCollector } from './get_usage_collector'; -import { fetchClustersLegacy } from '../../lib/alerts/fetch_clusters'; +import { fetchClusters } from '../../lib/alerts/fetch_clusters'; import { elasticsearchServiceMock } from '../../../../../../src/core/server/mocks'; jest.mock('../../lib/alerts/fetch_clusters', () => ({ - fetchClustersLegacy: jest.fn().mockImplementation(() => { + fetchClusters: jest.fn().mockImplementation(() => { return [ { clusterUuid: '1abc', @@ -59,7 +59,8 @@ jest.mock('./lib/fetch_license_type', () => ({ })); describe('getMonitoringUsageCollector', () => { - const esClient = elasticsearchServiceMock.createLegacyClusterClient(); + const esClient = elasticsearchServiceMock.createClusterClient(); + const getEsClient = () => esClient; const config: any = { ui: { ccs: { @@ -72,7 +73,7 @@ describe('getMonitoringUsageCollector', () => { const usageCollection: any = { makeUsageCollector: jest.fn(), }; - await getMonitoringUsageCollector(usageCollection, config, esClient); + getMonitoringUsageCollector(usageCollection, config, getEsClient); const mock = (usageCollection.makeUsageCollector as jest.Mock).mock; @@ -122,7 +123,7 @@ describe('getMonitoringUsageCollector', () => { makeUsageCollector: jest.fn(), }; - await getMonitoringUsageCollector(usageCollection, config, esClient); + getMonitoringUsageCollector(usageCollection, config, getEsClient); const mock = (usageCollection.makeUsageCollector as jest.Mock).mock; const args = mock.calls[0]; @@ -149,11 +150,11 @@ describe('getMonitoringUsageCollector', () => { makeUsageCollector: jest.fn(), }; - await getMonitoringUsageCollector(usageCollection, config, esClient); + getMonitoringUsageCollector(usageCollection, config, getEsClient); const mock = (usageCollection.makeUsageCollector as jest.Mock).mock; const args = mock.calls[0]; - (fetchClustersLegacy as jest.Mock).mockImplementation(() => { + (fetchClusters as jest.Mock).mockImplementation(() => { return []; }); @@ -169,11 +170,11 @@ describe('getMonitoringUsageCollector', () => { makeUsageCollector: jest.fn(), }; - await getMonitoringUsageCollector(usageCollection, config, esClient); + getMonitoringUsageCollector(usageCollection, config, getEsClient); const mock = (usageCollection.makeUsageCollector as jest.Mock).mock; const args = mock.calls[0]; - (fetchClustersLegacy as jest.Mock).mockImplementation(() => { + (fetchClusters as jest.Mock).mockImplementation(() => { return []; }); diff --git a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/get_usage_collector.ts b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/get_usage_collector.ts index 6f638b6ff8f0e0..558a79e03dcb7a 100644 --- a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/get_usage_collector.ts +++ b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/get_usage_collector.ts @@ -6,20 +6,20 @@ */ import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; -import { ILegacyClusterClient } from 'src/core/server'; +import { IClusterClient } from 'src/core/server'; import { MonitoringConfig } from '../../config'; -import { fetchAvailableCcsLegacy } from '../../lib/alerts/fetch_available_ccs'; +import { fetchAvailableCcs } from '../../lib/alerts/fetch_available_ccs'; import { getStackProductsUsage } from './lib/get_stack_products_usage'; import { fetchLicenseType } from './lib/fetch_license_type'; import { MonitoringUsage, StackProductUsage, MonitoringClusterStackProductUsage } from './types'; import { INDEX_PATTERN_ELASTICSEARCH } from '../../../common/constants'; import { getCcsIndexPattern } from '../../lib/alerts/get_ccs_index_pattern'; -import { fetchClustersLegacy } from '../../lib/alerts/fetch_clusters'; +import { fetchClusters } from '../../lib/alerts/fetch_clusters'; export function getMonitoringUsageCollector( usageCollection: UsageCollectionSetup, config: MonitoringConfig, - legacyEsClient: ILegacyClusterClient + getClient: () => IClusterClient ) { return usageCollection.makeUsageCollector({ type: 'monitoring', @@ -103,12 +103,12 @@ export function getMonitoringUsageCollector( }, fetch: async ({ kibanaRequest }) => { const callCluster = kibanaRequest - ? legacyEsClient.asScoped(kibanaRequest).callAsCurrentUser - : legacyEsClient.callAsInternalUser; + ? getClient().asScoped(kibanaRequest).asCurrentUser + : getClient().asInternalUser; const usageClusters: MonitoringClusterStackProductUsage[] = []; - const availableCcs = config.ui.ccs.enabled ? await fetchAvailableCcsLegacy(callCluster) : []; + const availableCcs = config.ui.ccs.enabled ? await fetchAvailableCcs(callCluster) : []; const elasticsearchIndex = getCcsIndexPattern(INDEX_PATTERN_ELASTICSEARCH, availableCcs); - const clusters = await fetchClustersLegacy(callCluster, elasticsearchIndex); + const clusters = await fetchClusters(callCluster, elasticsearchIndex); for (const cluster of clusters) { const license = await fetchLicenseType(callCluster, availableCcs, cluster.clusterUuid); const stackProducts = await getStackProductsUsage( diff --git a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/index.ts b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/index.ts index 5549b7aa968d0b..06d47f00447286 100644 --- a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/index.ts +++ b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/index.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { ILegacyClusterClient } from 'src/core/server'; +import { IClusterClient } from 'src/core/server'; import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import { getSettingsCollector } from './get_settings_collector'; import { getMonitoringUsageCollector } from './get_usage_collector'; @@ -16,10 +16,10 @@ export { KibanaSettingsCollector, getKibanaSettings } from './get_settings_colle export function registerCollectors( usageCollection: UsageCollectionSetup, config: MonitoringConfig, - legacyEsClient: ILegacyClusterClient + getClient: () => IClusterClient ) { usageCollection.registerCollector(getSettingsCollector(usageCollection, config)); usageCollection.registerCollector( - getMonitoringUsageCollector(usageCollection, config, legacyEsClient) + getMonitoringUsageCollector(usageCollection, config, getClient) ); } diff --git a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_es_usage.test.ts b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_es_usage.test.ts index 6d01dbc120be37..b7d616a5a3cc77 100644 --- a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_es_usage.test.ts +++ b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_es_usage.test.ts @@ -5,41 +5,45 @@ * 2.0. */ +import { ElasticsearchClient } from 'kibana/server'; import { fetchESUsage } from './fetch_es_usage'; describe('fetchESUsage', () => { const clusterUuid = '1abcde2'; const index = '.monitoring-es-*'; - const callCluster = jest.fn().mockImplementation(() => ({ - hits: { - hits: [ - { - _source: { - cluster_stats: { - nodes: { - count: { - total: 10, + const callCluster = ({ + search: jest.fn().mockImplementation(() => ({ + body: { + hits: { + hits: [ + { + _source: { + cluster_stats: { + nodes: { + count: { + total: 10, + }, + }, }, }, }, - }, + ], }, - ], - }, - aggregations: { - indices: { - buckets: [ - { - key: '.monitoring-es-2', + aggregations: { + indices: { + buckets: [ + { + key: '.monitoring-es-2', + }, + ], }, - ], + }, }, - }, - })); - const config: any = {}; + })), + } as unknown) as ElasticsearchClient; it('should return usage data for Elasticsearch', async () => { - const result = await fetchESUsage(config, callCluster, clusterUuid, index); + const result = await fetchESUsage(callCluster, clusterUuid, index); expect(result).toStrictEqual({ count: 10, enabled: true, @@ -48,33 +52,37 @@ describe('fetchESUsage', () => { }); it('should handle some indices coming from Metricbeat', async () => { - const customCallCluster = jest.fn().mockImplementation(() => ({ - hits: { - hits: [ - { - _source: { - cluster_stats: { - nodes: { - count: { - total: 10, + const customCallCluster = ({ + search: jest.fn().mockImplementation(() => ({ + body: { + hits: { + hits: [ + { + _source: { + cluster_stats: { + nodes: { + count: { + total: 10, + }, + }, }, }, }, - }, + ], }, - ], - }, - aggregations: { - indices: { - buckets: [ - { - key: '.monitoring-es-mb-2', + aggregations: { + indices: { + buckets: [ + { + key: '.monitoring-es-mb-2', + }, + ], }, - ], + }, }, - }, - })); - const result = await fetchESUsage(config, customCallCluster, clusterUuid, index); + })), + } as unknown) as ElasticsearchClient; + const result = await fetchESUsage(customCallCluster, clusterUuid, index); expect(result).toStrictEqual({ count: 10, enabled: true, @@ -83,12 +91,16 @@ describe('fetchESUsage', () => { }); it('should handle no monitoring data', async () => { - const customCallCluster = jest.fn().mockImplementation(() => ({ - hits: { - hits: [], - }, - })); - const result = await fetchESUsage(config, customCallCluster, clusterUuid, index); + const customCallCluster = ({ + search: jest.fn().mockImplementation(() => ({ + body: { + hits: { + hits: [], + }, + }, + })), + } as unknown) as ElasticsearchClient; + const result = await fetchESUsage(customCallCluster, clusterUuid, index); expect(result).toStrictEqual({ count: 0, enabled: false, diff --git a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_es_usage.ts b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_es_usage.ts index b96c119bf5ab8d..1fb2ba70f2ab11 100644 --- a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_es_usage.ts +++ b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_es_usage.ts @@ -5,30 +5,15 @@ * 2.0. */ -import { LegacyAPICaller } from 'src/core/server'; +import { ElasticsearchClient } from 'src/core/server'; import { get } from 'lodash'; -import { MonitoringConfig } from '../../../config'; +import { estypes } from '@elastic/elasticsearch'; import { StackProductUsage } from '../types'; -interface ESResponse { - hits: { - hits: ESResponseHits[]; - }; - aggregations: { - indices: { - buckets: ESIndicesBucket; - }; - }; -} - interface ESIndicesBucket { key: string; } -interface ESResponseHits { - _source: ClusterStats; -} - interface ClusterStats { cluster_stats: { nodes: { @@ -41,16 +26,15 @@ interface ClusterStats { } export async function fetchESUsage( - config: MonitoringConfig, - callCluster: LegacyAPICaller, + callCluster: ElasticsearchClient, clusterUuid: string, index: string ): Promise { - const params = { + const params: estypes.SearchRequest = { index, size: 1, - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ 'hits.hits._source.cluster_stats.nodes.count.total', 'aggregations.indices.buckets', ], @@ -101,8 +85,8 @@ export async function fetchESUsage( }, }; - const response = await callCluster('search', params); - const esResponse = response as ESResponse; + const { body: response } = await callCluster.search(params); + const esResponse = response as estypes.SearchResponse; if (esResponse.hits.hits.length === 0) { return { count: 0, @@ -112,7 +96,7 @@ export async function fetchESUsage( } const hit = esResponse.hits.hits[0]._source; - const count = hit.cluster_stats.nodes.count.total; + const count = hit?.cluster_stats.nodes.count.total || 0; const buckets = get(esResponse, 'aggregations.indices.buckets', []) as ESIndicesBucket[]; const metricbeatUsed = Boolean(buckets.find((indexBucket) => indexBucket.key.includes('-mb-'))); diff --git a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_license_type.test.ts b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_license_type.test.ts index b5c3293af40209..e4d801cfac0c3a 100644 --- a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_license_type.test.ts +++ b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_license_type.test.ts @@ -5,24 +5,29 @@ * 2.0. */ +import { ElasticsearchClient } from 'kibana/server'; import { fetchLicenseType } from './fetch_license_type'; describe('fetchLicenseType', () => { const clusterUuid = '1abcde2'; const availableCcs: string[] = []; - const callCluster = jest.fn().mockImplementation(() => ({ - hits: { - hits: [ - { - _source: { - license: { - type: 'trial', + const callCluster = ({ + search: jest.fn().mockImplementation(() => ({ + body: { + hits: { + hits: [ + { + _source: { + license: { + type: 'trial', + }, + }, }, - }, + ], }, - ], - }, - })); + }, + })), + } as unknown) as ElasticsearchClient; it('should get the license type', async () => { const result = await fetchLicenseType(callCluster, availableCcs, clusterUuid); @@ -30,11 +35,15 @@ describe('fetchLicenseType', () => { }); it('should handle no license data', async () => { - const customCallCluster = jest.fn().mockImplementation(() => ({ - hits: { - hits: [], - }, - })); + const customCallCluster = ({ + search: jest.fn().mockImplementation(() => ({ + body: { + hits: { + hits: [], + }, + }, + })), + } as unknown) as ElasticsearchClient; const result = await fetchLicenseType(customCallCluster, availableCcs, clusterUuid); expect(result).toStrictEqual(null); }); diff --git a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_license_type.ts b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_license_type.ts index eed177a6647f20..f42623ff851ce7 100644 --- a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_license_type.ts +++ b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_license_type.ts @@ -6,12 +6,13 @@ */ import { get } from 'lodash'; -import { LegacyAPICaller } from 'src/core/server'; +import { ElasticsearchClient } from 'src/core/server'; +import { estypes } from '@elastic/elasticsearch'; import { INDEX_PATTERN_ELASTICSEARCH } from '../../../../common/constants'; import { getCcsIndexPattern } from '../../../lib/alerts/get_ccs_index_pattern'; export async function fetchLicenseType( - callCluster: LegacyAPICaller, + client: ElasticsearchClient, availableCcs: string[], clusterUuid: string ) { @@ -19,9 +20,9 @@ export async function fetchLicenseType( if (availableCcs) { index = getCcsIndexPattern(index, availableCcs); } - const params = { + const params: estypes.SearchRequest = { index, - filterPath: ['hits.hits._source.license'], + filter_path: ['hits.hits._source.license'], body: { size: 1, sort: [ @@ -54,6 +55,6 @@ export async function fetchLicenseType( }, }, }; - const response = await callCluster('search', params); + const { body: response } = await client.search(params); return get(response, 'hits.hits[0]._source.license.type', null); } diff --git a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_stack_product_usage.test.ts b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_stack_product_usage.test.ts index 4a2f1abdc96ef2..a8650c375a0dfc 100644 --- a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_stack_product_usage.test.ts +++ b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_stack_product_usage.test.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { ElasticsearchClient } from 'kibana/server'; import { fetchStackProductUsage } from './fetch_stack_product_usage'; describe('fetchStackProductUsage', () => { @@ -16,7 +17,27 @@ describe('fetchStackProductUsage', () => { }; it('should use appropiate query parameters', async () => { - const callCluster = jest.fn(); + const searchMock = jest.fn().mockImplementation(() => ({ + body: { + aggregations: { + uuids: { + buckets: [ + { + key: 'sadfsdf', + indices: { + buckets: [ + { + key: '.monitoring-kibana-8', + }, + ], + }, + }, + ], + }, + }, + }, + })); + const callCluster = ({ search: searchMock } as unknown) as ElasticsearchClient; await fetchStackProductUsage( config, callCluster, @@ -34,7 +55,7 @@ describe('fetchStackProductUsage', () => { }, ] ); - const params = callCluster.mock.calls[0][1]; + const params = searchMock.mock.calls[0][0]; expect(params.body.query.bool.must[0].term.type.value).toBe('kibana_stats'); expect(params.body.query.bool.must[1].term.cluster_uuid.value).toBe(clusterUuid); expect(params.body.query.bool.must[2].range.timestamp.gte).toBe('now-1h'); @@ -42,24 +63,28 @@ describe('fetchStackProductUsage', () => { }); it('should get the usage data', async () => { - const callCluster = jest.fn().mockImplementation(() => ({ - aggregations: { - uuids: { - buckets: [ - { - key: 'sadfsdf', - indices: { - buckets: [ - { - key: '.monitoring-kibana-8', + const callCluster = ({ + search: jest.fn().mockImplementation(() => ({ + body: { + aggregations: { + uuids: { + buckets: [ + { + key: 'sadfsdf', + indices: { + buckets: [ + { + key: '.monitoring-kibana-8', + }, + ], }, - ], - }, + }, + ], }, - ], + }, }, - }, - })); + })), + } as unknown) as ElasticsearchClient; const result = await fetchStackProductUsage( config, @@ -78,27 +103,31 @@ describe('fetchStackProductUsage', () => { }); it('should handle both collection types', async () => { - const callCluster = jest.fn().mockImplementation(() => ({ - aggregations: { - uuids: { - buckets: [ - { - key: 'sadfsdf', - indices: { - buckets: [ - { - key: '.monitoring-kibana-8', + const callCluster = ({ + search: jest.fn().mockImplementation(() => ({ + body: { + aggregations: { + uuids: { + buckets: [ + { + key: 'sadfsdf', + indices: { + buckets: [ + { + key: '.monitoring-kibana-8', + }, + { + key: '.monitoring-kibana-mb-8', + }, + ], }, - { - key: '.monitoring-kibana-mb-8', - }, - ], - }, + }, + ], }, - ], + }, }, - }, - })); + })), + } as unknown) as ElasticsearchClient; const result = await fetchStackProductUsage( config, diff --git a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_stack_product_usage.ts b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_stack_product_usage.ts index 2160e159ad1008..527ed503c8fafc 100644 --- a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_stack_product_usage.ts +++ b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/fetch_stack_product_usage.ts @@ -6,7 +6,8 @@ */ import { get } from 'lodash'; -import { LegacyAPICaller } from 'src/core/server'; +import { ElasticsearchClient } from 'src/core/server'; +import { estypes } from '@elastic/elasticsearch'; import { MonitoringConfig } from '../../../config'; // @ts-ignore import { prefixIndexPattern } from '../../../lib/ccs_utils'; @@ -33,7 +34,7 @@ interface KeyBucket { export async function fetchStackProductUsage( config: MonitoringConfig, - callCluster: LegacyAPICaller, + callCluster: ElasticsearchClient, clusterUuid: string, index: string, type: string, @@ -41,11 +42,11 @@ export async function fetchStackProductUsage( filters: any[] = [] ): Promise { const size = config.ui.max_bucket_size; - const params = { + const params: estypes.SearchRequest = { index, size: 0, - ignoreUnavailable: true, - filterPath: ['aggregations.uuids.buckets'], + ignore_unavailable: true, + filter_path: ['aggregations.uuids.buckets'], body: { query: { bool: { @@ -94,7 +95,8 @@ export async function fetchStackProductUsage( }, }; - const response = (await callCluster('search', params)) as ESResponse; + const { body: responseBody } = await callCluster.search(params); + const response = responseBody as estypes.SearchResponse; const uuidBuckets = get(response, 'aggregations.uuids.buckets', []) as UuidBucket[]; const count = uuidBuckets.length; const metricbeatUsed = Boolean( diff --git a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/get_stack_products_usage.test.ts b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/get_stack_products_usage.test.ts index 8a3135ff832a98..928d7efcdd0d1c 100644 --- a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/get_stack_products_usage.test.ts +++ b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/get_stack_products_usage.test.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { ElasticsearchClient } from 'kibana/server'; import { getStackProductsUsage } from './get_stack_products_usage'; describe('getStackProductsUsage', () => { @@ -15,11 +16,15 @@ describe('getStackProductsUsage', () => { }; const clusterUuid = '1abcde2'; const availableCcs: string[] = []; - const callCluster = jest.fn().mockImplementation(() => ({ - hits: { - hits: [], - }, - })); + const callCluster = ({ + search: jest.fn().mockImplementation(() => ({ + body: { + hits: { + hits: [], + }, + }, + })), + } as unknown) as ElasticsearchClient; it('should get all stack products', async () => { const result = await getStackProductsUsage(config, callCluster, availableCcs, clusterUuid); diff --git a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/get_stack_products_usage.ts b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/get_stack_products_usage.ts index 0fd214f2fa8557..7cce1b392112f9 100644 --- a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/get_stack_products_usage.ts +++ b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/lib/get_stack_products_usage.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { LegacyAPICaller } from 'src/core/server'; +import { ElasticsearchClient } from 'src/core/server'; import { MonitoringClusterStackProductUsage } from '../types'; import { fetchESUsage } from './fetch_es_usage'; import { MonitoringConfig } from '../../../config'; @@ -24,7 +24,7 @@ import { getCcsIndexPattern } from '../../../lib/alerts/get_ccs_index_pattern'; export const getStackProductsUsage = async ( config: MonitoringConfig, - callCluster: LegacyAPICaller, + callCluster: ElasticsearchClient, availableCcs: string[], clusterUuid: string ): Promise< @@ -38,7 +38,7 @@ export const getStackProductsUsage = async ( const logstashIndex = getCcsIndexPattern(INDEX_PATTERN_LOGSTASH, availableCcs); const beatsIndex = getCcsIndexPattern(INDEX_PATTERN_BEATS, availableCcs); const [elasticsearch, kibana, logstash, beats, apm] = await Promise.all([ - fetchESUsage(config, callCluster, clusterUuid, elasticsearchIndex), + fetchESUsage(callCluster, clusterUuid, elasticsearchIndex), fetchStackProductUsage( config, callCluster, diff --git a/x-pack/plugins/monitoring/server/lib/alerts/disable_watcher_cluster_alerts.ts b/x-pack/plugins/monitoring/server/lib/alerts/disable_watcher_cluster_alerts.ts index dff828d05e2b60..e1dfb893869898 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/disable_watcher_cluster_alerts.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/disable_watcher_cluster_alerts.ts @@ -6,7 +6,7 @@ */ import { Logger } from 'kibana/server'; -import { LegacyAPICaller } from 'src/core/server'; +import { ElasticsearchClient } from 'src/core/server'; interface DisableWatchesResponse { exporters: Array< @@ -22,9 +22,13 @@ interface DisableWatchesResponse { >; } -async function callMigrationApi(callCluster: LegacyAPICaller, logger: Logger) { +async function callMigrationApi(callCluster: ElasticsearchClient, logger: Logger) { try { - return await callCluster('monitoring.disableWatches'); + const { body: response } = await callCluster.transport.request({ + method: 'post', + path: '/monitoring.disableWatches', + }); + return response as DisableWatchesResponse; } catch (err) { logger.warn( `Unable to call migration api to disable cluster alert watches. Message=${err.message}` @@ -33,8 +37,11 @@ async function callMigrationApi(callCluster: LegacyAPICaller, logger: Logger) { } } -export async function disableWatcherClusterAlerts(callCluster: LegacyAPICaller, logger: Logger) { - const response: DisableWatchesResponse = await callMigrationApi(callCluster, logger); +export async function disableWatcherClusterAlerts( + callCluster: ElasticsearchClient, + logger: Logger +) { + const response: DisableWatchesResponse | undefined = await callMigrationApi(callCluster, logger); if (!response || response.exporters.length === 0) { return true; } diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_ccr_read_exceptions.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_ccr_read_exceptions.ts index a96a7454ea744a..560751d1297d53 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_ccr_read_exceptions.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_ccr_read_exceptions.ts @@ -18,7 +18,7 @@ export async function fetchCCRReadExceptions( ): Promise { const params = { index, - filterPath: ['aggregations.remote_clusters.buckets'], + filter_path: ['aggregations.remote_clusters.buckets'], body: { size: 0, query: { diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_health.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_health.ts index f622418c77910f..85bfbd9dbd0490 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_health.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_health.ts @@ -15,7 +15,7 @@ export async function fetchClusterHealth( ): Promise { const params = { index, - filterPath: [ + filter_path: [ 'hits.hits._source.cluster_state.status', 'hits.hits._source.cluster_uuid', 'hits.hits._index', diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_clusters.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_clusters.ts index bbaea8d9f206e6..8b5803d588e12e 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_clusters.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_clusters.ts @@ -23,7 +23,7 @@ export async function fetchClusters( ): Promise { const params = { index, - filterPath: [ + filter_path: [ 'hits.hits._source.cluster_settings.cluster.metadata.display_name', 'hits.hits._source.cluster_uuid', 'hits.hits._source.cluster_name', @@ -70,7 +70,7 @@ export async function fetchClustersLegacy( ): Promise { const params = { index, - filterPath: [ + filter_path: [ 'hits.hits._source.cluster_settings.cluster.metadata.display_name', 'hits.hits._source.cluster_uuid', 'hits.hits._source.cluster_name', diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.test.ts index 9cb773c81923b1..90cd456f18037a 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.test.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.test.ts @@ -204,7 +204,7 @@ describe('fetchCpuUsageNodeStats', () => { await fetchCpuUsageNodeStats(esClient, clusters, index, startMs, endMs, size); expect(params).toStrictEqual({ index: '.monitoring-es-*', - filterPath: ['aggregations'], + filter_path: ['aggregations'], body: { size: 0, query: { diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.ts index 07ca3572ad6b3f..6f7d27916a7b1b 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.ts @@ -34,10 +34,9 @@ export async function fetchCpuUsageNodeStats( // Using pure MS didn't seem to work well with the date_histogram interval // but minutes does const intervalInMinutes = moment.duration(endMs - startMs).asMinutes(); - const filterPath = ['aggregations']; const params = { index, - filterPath, + filter_path: ['aggregations'], body: { size: 0, query: { diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_disk_usage_node_stats.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_disk_usage_node_stats.ts index 2e8b5c7478e152..70f05991d42291 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_disk_usage_node_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_disk_usage_node_stats.ts @@ -19,7 +19,7 @@ export async function fetchDiskUsageNodeStats( const clustersIds = clusters.map((cluster) => cluster.clusterUuid); const params = { index, - filterPath: ['aggregations'], + filter_path: ['aggregations'], body: { size: 0, query: { diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_elasticsearch_versions.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_elasticsearch_versions.ts index f25f1dbe594db9..f2f311ac870a51 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_elasticsearch_versions.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_elasticsearch_versions.ts @@ -16,7 +16,7 @@ export async function fetchElasticsearchVersions( ): Promise { const params = { index, - filterPath: [ + filter_path: [ 'hits.hits._source.cluster_stats.nodes.versions', 'hits.hits._index', 'hits.hits._source.cluster_uuid', diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_index_shard_size.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_index_shard_size.ts index 117894c0d823b5..7e7ea5e6bfdd22 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_index_shard_size.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_index_shard_size.ts @@ -39,7 +39,7 @@ export async function fetchIndexShardSize( ): Promise { const params = { index, - filterPath: ['aggregations.clusters.buckets'], + filter_path: ['aggregations.clusters.buckets'], body: { size: 0, query: { diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_kibana_versions.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_kibana_versions.ts index cb2f201e2586ec..e57b45e2570fa1 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_kibana_versions.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_kibana_versions.ts @@ -20,7 +20,7 @@ export async function fetchKibanaVersions( ): Promise { const params = { index, - filterPath: ['aggregations'], + filter_path: ['aggregations'], body: { size: 0, query: { diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.ts index 5178b6c4c53a71..38ff82cf298329 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.ts @@ -15,7 +15,7 @@ export async function fetchLicenses( ): Promise { const params = { index, - filterPath: [ + filter_path: [ 'hits.hits._source.license.*', 'hits.hits._source.cluster_uuid', 'hits.hits._index', diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_logstash_versions.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_logstash_versions.ts index 6fb54857d40e43..774ee2551ec073 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_logstash_versions.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_logstash_versions.ts @@ -20,7 +20,7 @@ export async function fetchLogstashVersions( ): Promise { const params = { index, - filterPath: ['aggregations'], + filter_path: ['aggregations'], body: { size: 0, query: { diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_memory_usage_node_stats.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_memory_usage_node_stats.ts index 46bb9c794a6a69..f34a8dcff1db73 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_memory_usage_node_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_memory_usage_node_stats.ts @@ -20,7 +20,7 @@ export async function fetchMemoryUsageNodeStats( const clustersIds = clusters.map((cluster) => cluster.clusterUuid); const params = { index, - filterPath: ['aggregations'], + filter_path: ['aggregations'], body: { size: 0, query: { diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_missing_monitoring_data.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_missing_monitoring_data.ts index a7b4a3a0232072..856ca7c9198851 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_missing_monitoring_data.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_missing_monitoring_data.ts @@ -52,7 +52,7 @@ export async function fetchMissingMonitoringData( const endMs = nowInMs; const params = { index, - filterPath: ['aggregations.clusters.buckets'], + filter_path: ['aggregations.clusters.buckets'], body: { size: 0, query: { diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_nodes_from_cluster_stats.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_nodes_from_cluster_stats.ts index 5f867ca5b6edf5..dcc8e6516c69bd 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_nodes_from_cluster_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_nodes_from_cluster_stats.ts @@ -30,7 +30,7 @@ export async function fetchNodesFromClusterStats( ): Promise { const params = { index, - filterPath: ['aggregations.clusters.buckets'], + filter_path: ['aggregations.clusters.buckets'], body: { size: 0, sort: [ diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_thread_pool_rejections_stats.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_thread_pool_rejections_stats.ts index 954ec3877144fe..132f7692a7579a 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_thread_pool_rejections_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_thread_pool_rejections_stats.ts @@ -41,7 +41,7 @@ export async function fetchThreadPoolRejectionStats( const clustersIds = clusters.map((cluster) => cluster.clusterUuid); const params = { index, - filterPath: ['aggregations'], + filter_path: ['aggregations'], body: { size: 0, query: { diff --git a/x-pack/plugins/monitoring/server/lib/apm/_get_time_of_last_event.ts b/x-pack/plugins/monitoring/server/lib/apm/_get_time_of_last_event.ts index 93ff966b5def54..398428f89a4baa 100644 --- a/x-pack/plugins/monitoring/server/lib/apm/_get_time_of_last_event.ts +++ b/x-pack/plugins/monitoring/server/lib/apm/_get_time_of_last_event.ts @@ -30,7 +30,7 @@ export async function getTimeOfLastEvent({ const params = { index: apmIndexPattern, size: 1, - ignoreUnavailable: true, + ignore_unavailable: true, body: { _source: ['beats_stats.timestamp', '@timestamp'], sort: [ 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 53a4aeb06bcc16..3721bf873a4177 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 @@ -97,8 +97,8 @@ export async function getApmInfo( const params = { index: apmIndexPattern, size: 1, - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ 'hits.hits._source.beats_stats.beat.host', 'hits.hits._source.beats_stats.beat.version', 'hits.hits._source.beats_stats.beat.name', 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 b606eaf5fe7939..be3bb6fdfd6615 100644 --- a/x-pack/plugins/monitoring/server/lib/apm/get_apms.ts +++ b/x-pack/plugins/monitoring/server/lib/apm/get_apms.ts @@ -109,8 +109,8 @@ export async function getApms(req: LegacyRequest, apmIndexPattern: string, clust const params = { index: apmIndexPattern, size: config.get('monitoring.ui.max_bucket_size'), // FIXME - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ // only filter path can filter for inner_hits 'hits.hits._source.timestamp', 'hits.hits._source.@timestamp', diff --git a/x-pack/plugins/monitoring/server/lib/apm/get_apms_for_clusters.js b/x-pack/plugins/monitoring/server/lib/apm/get_apms_for_clusters.js index 45bbe35ebd59d1..0c96e0e2305850 100644 --- a/x-pack/plugins/monitoring/server/lib/apm/get_apms_for_clusters.js +++ b/x-pack/plugins/monitoring/server/lib/apm/get_apms_for_clusters.js @@ -46,8 +46,8 @@ export function getApmsForClusters(req, apmIndexPattern, clusters) { const params = { index: apmIndexPattern, size: 0, - ignoreUnavailable: true, - filterPath: apmAggFilterPath, + ignore_unavailable: true, + filter_path: apmAggFilterPath, body: { query: createApmQuery({ start, diff --git a/x-pack/plugins/monitoring/server/lib/apm/get_stats.js b/x-pack/plugins/monitoring/server/lib/apm/get_stats.js index 7dd1b652254cb8..2abd81e325f5ec 100644 --- a/x-pack/plugins/monitoring/server/lib/apm/get_stats.js +++ b/x-pack/plugins/monitoring/server/lib/apm/get_stats.js @@ -34,9 +34,9 @@ export async function getStats(req, apmIndexPattern, clusterUuid) { const params = { index: apmIndexPattern, - filterPath: apmAggFilterPath, + filter_path: apmAggFilterPath, size: 0, - ignoreUnavailable: true, + ignore_unavailable: true, body: { query: createApmQuery({ start, diff --git a/x-pack/plugins/monitoring/server/lib/beats/get_beat_summary.ts b/x-pack/plugins/monitoring/server/lib/beats/get_beat_summary.ts index 13b4f0041c99b7..d67f32e64ba71d 100644 --- a/x-pack/plugins/monitoring/server/lib/beats/get_beat_summary.ts +++ b/x-pack/plugins/monitoring/server/lib/beats/get_beat_summary.ts @@ -89,8 +89,8 @@ export async function getBeatSummary( const params = { index: beatsIndexPattern, size: 1, - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ 'hits.hits._source.beats_stats.beat.host', 'hits.hits._source.beat.stats.beat.host', 'hits.hits._source.beats_stats.beat.version', 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 641039331bbcb6..fff2b55cf26163 100644 --- a/x-pack/plugins/monitoring/server/lib/beats/get_beats.ts +++ b/x-pack/plugins/monitoring/server/lib/beats/get_beats.ts @@ -121,8 +121,8 @@ export async function getBeats(req: LegacyRequest, beatsIndexPattern: string, cl const params = { index: beatsIndexPattern, size: config.get('monitoring.ui.max_bucket_size'), // FIXME - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ // only filter path can filter for inner_hits 'hits.hits._source.beats_stats.beat.uuid', 'hits.hits._source.beat.stats.beat.uuid', diff --git a/x-pack/plugins/monitoring/server/lib/beats/get_beats_for_clusters.js b/x-pack/plugins/monitoring/server/lib/beats/get_beats_for_clusters.js index 5ceca7a02d89b0..e7c4771dd601cc 100644 --- a/x-pack/plugins/monitoring/server/lib/beats/get_beats_for_clusters.js +++ b/x-pack/plugins/monitoring/server/lib/beats/get_beats_for_clusters.js @@ -44,8 +44,8 @@ export function getBeatsForClusters(req, beatsIndexPattern, clusters) { const params = { index: beatsIndexPattern, size: 0, - ignoreUnavailable: true, - filterPath: beatsAggFilterPath, + ignore_unavailable: true, + filter_path: beatsAggFilterPath, body: { query: createBeatsQuery({ start, diff --git a/x-pack/plugins/monitoring/server/lib/beats/get_latest_stats.js b/x-pack/plugins/monitoring/server/lib/beats/get_latest_stats.js index f3f296fc09a389..fb40df115d19ac 100644 --- a/x-pack/plugins/monitoring/server/lib/beats/get_latest_stats.js +++ b/x-pack/plugins/monitoring/server/lib/beats/get_latest_stats.js @@ -81,8 +81,8 @@ export function getLatestStats(req, beatsIndexPattern, clusterUuid) { const params = { index: beatsIndexPattern, size: 0, - ignoreUnavailable: true, - filterPath: 'aggregations', + ignore_unavailable: true, + filter_path: 'aggregations', body: { query: createBeatsQuery({ clusterUuid, diff --git a/x-pack/plugins/monitoring/server/lib/beats/get_stats.js b/x-pack/plugins/monitoring/server/lib/beats/get_stats.js index aa3d3947adccf5..3af51d909697ff 100644 --- a/x-pack/plugins/monitoring/server/lib/beats/get_stats.js +++ b/x-pack/plugins/monitoring/server/lib/beats/get_stats.js @@ -33,9 +33,9 @@ export async function getStats(req, beatsIndexPattern, clusterUuid) { const params = { index: beatsIndexPattern, - filterPath: beatsAggFilterPath, + filter_path: beatsAggFilterPath, size: 0, - ignoreUnavailable: true, + ignore_unavailable: true, body: { query: createBeatsQuery({ start, 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 42041832cfee5d..820b1bf24c6a13 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 @@ -30,8 +30,8 @@ async function findSupportedBasicLicenseCluster( const kibanaDataResult: ElasticsearchResponse = (await callWithRequest(req, 'search', { index: kbnIndexPattern, size: 1, - ignoreUnavailable: true, - filterPath: ['hits.hits._source.cluster_uuid', 'hits.hits._source.cluster.id'], + ignore_unavailable: true, + filter_path: ['hits.hits._source.cluster_uuid', 'hits.hits._source.cluster.id'], body: { sort: { timestamp: { order: 'desc', unmapped_type: 'long' } }, query: { diff --git a/x-pack/plugins/monitoring/server/lib/cluster/get_cluster_license.ts b/x-pack/plugins/monitoring/server/lib/cluster/get_cluster_license.ts index d1fa0efcbca2de..8ed5578e574a0c 100644 --- a/x-pack/plugins/monitoring/server/lib/cluster/get_cluster_license.ts +++ b/x-pack/plugins/monitoring/server/lib/cluster/get_cluster_license.ts @@ -20,8 +20,8 @@ export function getClusterLicense(req: LegacyRequest, esIndexPattern: string, cl const params = { index: esIndexPattern, size: 1, - ignoreUnavailable: true, - filterPath: 'hits.hits._source.license', + ignore_unavailable: true, + filter_path: ['hits.hits._source.license'], body: { sort: { timestamp: { order: 'desc', unmapped_type: 'long' } }, query: createQuery({ diff --git a/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_state.ts b/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_state.ts index 2cec89b18aecda..d732b43bc203b4 100644 --- a/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_state.ts +++ b/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_state.ts @@ -66,8 +66,8 @@ export function getClustersState( const params = { index: esIndexPattern, size: clusterUuids.length, - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ 'hits.hits._source.cluster_uuid', 'hits.hits._source.elasticsearch.cluster.id', 'hits.hits._source.cluster_state', 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 4067293e7829da..6eb21165d7256b 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,8 +53,8 @@ function fetchClusterStats(req: LegacyRequest, esIndexPattern: string, clusterUu const params = { index: esIndexPattern, size: config.get('monitoring.ui.max_bucket_size'), - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ 'hits.hits._index', 'hits.hits._source.cluster_uuid', 'hits.hits._source.elasticsearch.cluster.id', diff --git a/x-pack/plugins/monitoring/server/lib/details/get_series.js b/x-pack/plugins/monitoring/server/lib/details/get_series.js index ca062ad5599fac..d06ff950449dc0 100644 --- a/x-pack/plugins/monitoring/server/lib/details/get_series.js +++ b/x-pack/plugins/monitoring/server/lib/details/get_series.js @@ -150,7 +150,7 @@ async function fetchSeries( const params = { index: indexPattern, size: 0, - ignoreUnavailable: true, + ignore_unavailable: true, body: { query: createQuery({ start: adjustedMin, diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/ccr.ts b/x-pack/plugins/monitoring/server/lib/elasticsearch/ccr.ts index d0abed2ad8b8da..482cbd3601993c 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/ccr.ts +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/ccr.ts @@ -27,7 +27,7 @@ export async function checkCcrEnabled(req: LegacyRequest, esIndexPattern: string const params = { index: esIndexPattern, size: 1, - ignoreUnavailable: true, + ignore_unavailable: true, body: { query: createQuery({ type: 'cluster_stats', @@ -38,7 +38,7 @@ export async function checkCcrEnabled(req: LegacyRequest, esIndexPattern: string }), sort: [{ timestamp: { order: 'desc', unmapped_type: 'long' } }], }, - filterPath: [ + filter_path: [ 'hits.hits._source.stack_stats.xpack.ccr', 'hits.hits._source.elasticsearch.cluster.stats.stack.xpack.ccr', ], diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/get_last_recovery.ts b/x-pack/plugins/monitoring/server/lib/elasticsearch/get_last_recovery.ts index b6669342649332..43527f875cf729 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/get_last_recovery.ts +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/get_last_recovery.ts @@ -97,7 +97,7 @@ export async function getLastRecovery(req: LegacyRequest, esIndexPattern: string const legacyParams = { index: esIndexPattern, size: 1, - ignoreUnavailable: true, + ignore_unavailable: true, body: { _source: ['index_recovery.shards'], sort: { timestamp: { order: 'desc', unmapped_type: 'long' } }, 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 8b89416f14e9fa..df44036cd0cd82 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 @@ -47,8 +47,8 @@ export function getMlJobs(req: LegacyRequest, esIndexPattern: string) { const params = { index: esIndexPattern, size: maxBucketSize, - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ 'hits.hits._source.job_stats.job_id', 'hits.hits._source.elasticsearch.ml.job.id', 'hits.hits._source.job_stats.state', @@ -95,8 +95,8 @@ export function getMlJobsForCluster( const params = { index: esIndexPattern, size: 0, - ignoreUnavailable: true, - filterPath: 'aggregations.jobs_count.value', + ignore_unavailable: true, + filter_path: 'aggregations.jobs_count.value', body: { query: createQuery({ types: ['ml_job', 'job_stats'], start, end, clusterUuid, metric }), aggs: { diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/indices/get_index_summary.ts b/x-pack/plugins/monitoring/server/lib/elasticsearch/indices/get_index_summary.ts index ee4988d773974f..8a03027a93a565 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/indices/get_index_summary.ts +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/indices/get_index_summary.ts @@ -89,7 +89,7 @@ export function getIndexSummary( const params = { index: esIndexPattern, size: 1, - ignoreUnavailable: true, + ignore_unavailable: true, body: { sort: { timestamp: { order: 'desc', unmapped_type: 'long' } }, query: createQuery({ 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 9a61d6c603b973..0a9993769a6e5c 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 @@ -116,8 +116,8 @@ export function buildGetIndicesQuery( return { index: esIndexPattern, size, - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ // only filter path can filter for inner_hits 'hits.hits._source.index_stats.index', 'hits.hits._source.elasticsearch.index.name', diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_node_summary.ts b/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_node_summary.ts index c7ca66753c73bf..79c44286717b45 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_node_summary.ts +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_node_summary.ts @@ -124,7 +124,7 @@ export function getNodeSummary( const params = { index: esIndexPattern, size: 1, - ignoreUnavailable: true, + ignore_unavailable: true, body: { sort: { timestamp: { order: 'desc', unmapped_type: 'long' } }, query: createQuery({ type: 'node_stats', start, end, clusterUuid, metric, filters }), diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_node_ids.js b/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_node_ids.js index 96b434b945ad46..87781417a07e5f 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_node_ids.js +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_node_ids.js @@ -17,8 +17,8 @@ export async function getNodeIds(req, indexPattern, { clusterUuid }, size) { const params = { index: indexPattern, size: 0, - ignoreUnavailable: true, - filterPath: ['aggregations.composite_data.buckets'], + ignore_unavailable: true, + filter_path: ['aggregations.composite_data.buckets'], body: { query: createQuery({ type: 'node_stats', 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 2235d3d2f3224d..442a2b1b9b9c9b 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 @@ -76,7 +76,7 @@ export async function getNodes( const params = { index: esIndexPattern, size: config.get('monitoring.ui.max_bucket_size'), - ignoreUnavailable: true, + ignore_unavailable: true, body: { query: createQuery({ type: 'node_stats', @@ -110,7 +110,7 @@ export async function getNodes( }, sort: [{ timestamp: { order: 'desc', unmapped_type: 'long' } }], }, - filterPath: [ + filter_path: [ 'hits.hits._source.source_node', 'hits.hits._source.service.address', 'hits.hits._source.elasticsearch.node', 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 d3acf8ccaf4437..87f79ff5b9b44d 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 @@ -41,7 +41,7 @@ async function getUnassignedShardData( const params = { index: esIndexPattern, size: 0, - ignoreUnavailable: true, + ignore_unavailable: true, body: { sort: { timestamp: { order: 'desc', unmapped_type: 'long' } }, query: createQuery({ 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 7cda87bf09af83..12ce144ebf5c5a 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 @@ -39,7 +39,7 @@ async function getShardCountPerNode( const params = { index: esIndexPattern, size: 0, - ignoreUnavailable: true, + ignore_unavailable: true, body: { sort: { timestamp: { order: 'desc', unmapped_type: 'long' } }, query: createQuery({ 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 b739b4a6533db2..d39e38eab21e93 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 @@ -103,7 +103,7 @@ export function getShardAllocation( const params = { index: esIndexPattern, size: config.get('monitoring.ui.max_bucket_size'), - ignoreUnavailable: true, + ignore_unavailable: true, body: { query: createQuery({ types: ['shard', 'shards'], clusterUuid, metric, filters }), }, 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 5219a2e9286f5b..e0e6854dba05f2 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 @@ -96,7 +96,7 @@ export function getShardStats( const params = { index: esIndexPattern, size: 0, - ignoreUnavailable: true, + ignore_unavailable: true, body: { sort: { timestamp: { order: 'desc', unmapped_type: 'long' } }, query: createQuery({ diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/verify_monitoring_auth.js b/x-pack/plugins/monitoring/server/lib/elasticsearch/verify_monitoring_auth.js index 1895c36699e50f..e4cee4d4455cab 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/verify_monitoring_auth.js +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/verify_monitoring_auth.js @@ -53,7 +53,7 @@ async function verifyHasPrivileges(req) { }, ], }, - ignoreUnavailable: true, // we allow 404 incase the user shutdown security in-between the check and now + ignore_unavailable: true, // we allow 404 incase the user shutdown security in-between the check and now }); } catch (err) { if ( diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch_settings/cluster.js b/x-pack/plugins/monitoring/server/lib/elasticsearch_settings/cluster.js index a1ece9e302b2a7..1c20634c102204 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch_settings/cluster.js +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch_settings/cluster.js @@ -34,15 +34,6 @@ export async function checkClusterSettings(req) { const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('admin'); const { cloud } = req.server.newPlatform.setup.plugins; const isCloudEnabled = !!(cloud && cloud.isCloudEnabled); - const response = await callWithRequest(req, 'transport.request', { - method: 'GET', - path: '/_cluster/settings?include_defaults', - filter_path: [ - 'persistent.xpack.monitoring', - 'transient.xpack.monitoring', - 'defaults.xpack.monitoring', - ], - }); - + const response = await callWithRequest(req, 'cluster.getSettings', { include_defaults: true }); return handleResponse(response, isCloudEnabled); } diff --git a/x-pack/plugins/monitoring/server/lib/kibana/get_kibana_info.ts b/x-pack/plugins/monitoring/server/lib/kibana/get_kibana_info.ts index 3b0af657947e54..15cc9904dd060c 100644 --- a/x-pack/plugins/monitoring/server/lib/kibana/get_kibana_info.ts +++ b/x-pack/plugins/monitoring/server/lib/kibana/get_kibana_info.ts @@ -36,8 +36,8 @@ export function getKibanaInfo( const params = { index: kbnIndexPattern, size: 1, - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ 'hits.hits._source.kibana_stats.kibana', 'hits.hits._source.kibana.kibana', 'hits.hits._source.kibana_stats.os.memory.free_in_bytes', 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 4da4c40b25568e..b89ff7a326d80f 100644 --- a/x-pack/plugins/monitoring/server/lib/kibana/get_kibanas.ts +++ b/x-pack/plugins/monitoring/server/lib/kibana/get_kibanas.ts @@ -71,7 +71,7 @@ export async function getKibanas( const params = { index: kbnIndexPattern, size: config.get('monitoring.ui.max_bucket_size'), - ignoreUnavailable: true, + ignore_unavailable: true, body: { query: createQuery({ types: ['kibana_stats', 'stats'], diff --git a/x-pack/plugins/monitoring/server/lib/kibana/get_kibanas_for_clusters.js b/x-pack/plugins/monitoring/server/lib/kibana/get_kibanas_for_clusters.js index 7d648601229224..141596ffd2f6fb 100644 --- a/x-pack/plugins/monitoring/server/lib/kibana/get_kibanas_for_clusters.js +++ b/x-pack/plugins/monitoring/server/lib/kibana/get_kibanas_for_clusters.js @@ -37,7 +37,7 @@ export function getKibanasForClusters(req, kbnIndexPattern, clusters) { const params = { index: kbnIndexPattern, size: 0, - ignoreUnavailable: true, + ignore_unavailable: true, body: { query: createQuery({ types: ['stats', 'kibana_stats'], diff --git a/x-pack/plugins/monitoring/server/lib/logs/get_log_types.ts b/x-pack/plugins/monitoring/server/lib/logs/get_log_types.ts index 82b73c6b87f060..bbb48c43033da3 100644 --- a/x-pack/plugins/monitoring/server/lib/logs/get_log_types.ts +++ b/x-pack/plugins/monitoring/server/lib/logs/get_log_types.ts @@ -84,8 +84,8 @@ export async function getLogTypes( const params = { index: filebeatIndexPattern, size: 0, - filterPath: ['aggregations.levels.buckets', 'aggregations.types.buckets'], - ignoreUnavailable: true, + filter_path: ['aggregations.levels.buckets', 'aggregations.types.buckets'], + ignore_unavailable: true, body: { sort: { '@timestamp': { order: 'desc', unmapped_type: 'long' } }, query: { 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 11e8f6550bcb7d..4c21422a5d0cf7 100644 --- a/x-pack/plugins/monitoring/server/lib/logs/get_logs.ts +++ b/x-pack/plugins/monitoring/server/lib/logs/get_logs.ts @@ -100,7 +100,7 @@ export async function getLogs( const params = { index: filebeatIndexPattern, size: Math.min(50, config.get('monitoring.ui.elasticsearch.logFetchCount')), - filterPath: [ + filter_path: [ 'hits.hits._source.message', 'hits.hits._source.log.level', 'hits.hits._source.@timestamp', @@ -109,7 +109,7 @@ export async function getLogs( 'hits.hits._source.elasticsearch.index.name', 'hits.hits._source.elasticsearch.node.name', ], - ignoreUnavailable: true, + ignore_unavailable: true, body: { sort: { '@timestamp': { order: 'desc', unmapped_type: 'long' } }, query: { diff --git a/x-pack/plugins/monitoring/server/lib/logstash/get_logstash_for_clusters.js b/x-pack/plugins/monitoring/server/lib/logstash/get_logstash_for_clusters.js index 58155e35ad52f9..17f76834b333ad 100644 --- a/x-pack/plugins/monitoring/server/lib/logstash/get_logstash_for_clusters.js +++ b/x-pack/plugins/monitoring/server/lib/logstash/get_logstash_for_clusters.js @@ -48,7 +48,7 @@ export function getLogstashForClusters(req, lsIndexPattern, clusters) { const params = { index: lsIndexPattern, size: 0, - ignoreUnavailable: true, + ignore_unavailable: true, body: { query: createQuery({ types: ['stats', 'logstash_stats'], diff --git a/x-pack/plugins/monitoring/server/lib/logstash/get_node_info.ts b/x-pack/plugins/monitoring/server/lib/logstash/get_node_info.ts index 954b78c432374f..d047729a0b3c22 100644 --- a/x-pack/plugins/monitoring/server/lib/logstash/get_node_info.ts +++ b/x-pack/plugins/monitoring/server/lib/logstash/get_node_info.ts @@ -45,8 +45,8 @@ export function getNodeInfo( const params = { index: lsIndexPattern, size: 1, - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ 'hits.hits._source.logstash_stats.events', 'hits.hits._source.logstash.node.stats.events', 'hits.hits._source.logstash_stats.jvm.uptime_in_millis', 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 9db59fec25530e..42d1b69aee5f37 100644 --- a/x-pack/plugins/monitoring/server/lib/logstash/get_nodes.ts +++ b/x-pack/plugins/monitoring/server/lib/logstash/get_nodes.ts @@ -82,7 +82,7 @@ export async function getNodes( const params = { index: lsIndexPattern, size: config.get('monitoring.ui.max_bucket_size'), // FIXME - ignoreUnavailable: true, + ignore_unavailable: true, body: { query: createQuery({ start, diff --git a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_ids.js b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_ids.js index 1521c5d3773d0e..2846a968bfed6d 100644 --- a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_ids.js +++ b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_ids.js @@ -27,8 +27,8 @@ export async function getLogstashPipelineIds( const params = { index: logstashIndexPattern, size: 0, - ignoreUnavailable: true, - filterPath: ['aggregations.nest.id.buckets', 'aggregations.nest_mb.id.buckets'], + ignore_unavailable: true, + filter_path: ['aggregations.nest.id.buckets', 'aggregations.nest_mb.id.buckets'], body: { query: createQuery({ start, diff --git a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_state_document.ts b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_state_document.ts index e261505e2d1b01..61c99c3a069b32 100644 --- a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_state_document.ts +++ b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_state_document.ts @@ -42,7 +42,7 @@ export async function getPipelineStateDocument( const params = { index: logstashIndexPattern, size: 1, - ignoreUnavailable: true, + ignore_unavailable: true, body: { _source: { excludes: 'logstash_state.pipeline.representation.plugins' }, sort: { timestamp: { order: 'desc', unmapped_type: 'long' } }, diff --git a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_stats_aggregation.js b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_stats_aggregation.js index 2e35a4639fa5a6..4d9d2a720a1627 100644 --- a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_stats_aggregation.js +++ b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_stats_aggregation.js @@ -106,8 +106,8 @@ function fetchPipelineLatestStats( const params = { index: logstashIndexPattern, size: 0, - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ 'aggregations.pipelines.scoped.vertices.vertex_id.buckets.key', 'aggregations.pipelines.scoped.vertices.vertex_id.buckets.events_in_total', 'aggregations.pipelines.scoped.vertices.vertex_id.buckets.events_out_total', diff --git a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_versions.js b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_versions.js index d1121c78407ffc..c52d41a363055f 100644 --- a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_versions.js +++ b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_versions.js @@ -82,7 +82,7 @@ function fetchPipelineVersions(...args) { const params = { index: logstashIndexPattern, size: 0, - ignoreUnavailable: true, + ignore_unavailable: true, body: { sort: { timestamp: { order: 'desc', unmapped_type: 'long' } }, query, diff --git a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_vertex_stats_aggregation.js b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_vertex_stats_aggregation.js index 81d1f2bf572176..97a8c463a2259f 100644 --- a/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_vertex_stats_aggregation.js +++ b/x-pack/plugins/monitoring/server/lib/logstash/get_pipeline_vertex_stats_aggregation.js @@ -155,8 +155,8 @@ function fetchPipelineVertexTimeSeriesStats( const params = { index: logstashIndexPattern, size: 0, - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ 'aggregations.timeseries.buckets.key', 'aggregations.timeseries.buckets.pipelines.scoped.vertices.vertex_id.events_in_total', 'aggregations.timeseries.buckets.pipelines.scoped.vertices.vertex_id.events_out_total', diff --git a/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.js b/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.js index 36a48002005b3d..b9ce355b44b625 100644 --- a/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.js +++ b/x-pack/plugins/monitoring/server/lib/setup/collection/get_collection_status.js @@ -57,8 +57,8 @@ const getRecentMonitoringDocuments = async (req, indexPatterns, clusterUuid, nod const params = { index: Object.values(indexPatterns), size: 0, - ignoreUnavailable: true, - filterPath: ['aggregations.indices.buckets'], + ignore_unavailable: true, + filter_path: ['aggregations.indices.buckets'], body: { query: { bool: { @@ -206,8 +206,8 @@ async function doesIndexExist(req, index) { index, size: 0, terminate_after: 1, - ignoreUnavailable: true, - filterPath: ['hits.total.value'], + ignore_unavailable: true, + filter_path: ['hits.total.value'], }; const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('monitoring'); const response = await callWithRequest(req, 'search', params); diff --git a/x-pack/plugins/monitoring/server/license_service.ts b/x-pack/plugins/monitoring/server/license_service.ts index fde22aaefa5dd2..ab10193fc93cbd 100644 --- a/x-pack/plugins/monitoring/server/license_service.ts +++ b/x-pack/plugins/monitoring/server/license_service.ts @@ -6,16 +6,17 @@ */ import { Subscription } from 'rxjs'; -import { ILegacyCustomClusterClient } from 'kibana/server'; +import { IClusterClient, ILegacyClusterClient } from 'kibana/server'; import { ILicense, LicenseFeature } from '../../licensing/common/types'; import { LicensingPluginStart } from '../../licensing/server'; import { MonitoringConfig } from './config'; import { Logger } from '../../../../src/core/server'; import { MonitoringLicenseService } from './types'; +import { EndpointTypes, Globals, ClientParams } from './static_globals'; interface SetupDeps { licensing: LicensingPluginStart; - monitoringClient: ILegacyCustomClusterClient; + monitoringClient: IClusterClient; config: MonitoringConfig; log: Logger; } @@ -27,8 +28,15 @@ const defaultLicenseFeature: LicenseFeature = { export class LicenseService { public setup({ licensing, monitoringClient, config, log }: SetupDeps): MonitoringLicenseService { + // TODO: This needs to be changed to an IClusterClient as when the Licensing server + // is upgraded to the new client. + const fakeLegacyClusterClient = { + callAsInternalUser: (endpoint: EndpointTypes, options: ClientParams) => + Globals.app.getLegacyClusterShim(monitoringClient.asInternalUser, endpoint, options), + } as ILegacyClusterClient; + const { refresh, license$ } = licensing.createLicensePoller( - monitoringClient, + fakeLegacyClusterClient, config.licensing.api_polling_frequency.asMilliseconds() ); diff --git a/x-pack/plugins/monitoring/server/plugin.ts b/x-pack/plugins/monitoring/server/plugin.ts index 10724594ce576d..ca8d87bd300a08 100644 --- a/x-pack/plugins/monitoring/server/plugin.ts +++ b/x-pack/plugins/monitoring/server/plugin.ts @@ -15,11 +15,12 @@ import { KibanaRequest, KibanaResponseFactory, CoreSetup, - ILegacyCustomClusterClient, + ICustomClusterClient, CoreStart, CustomHttpResponseOptions, ResponseError, Plugin, + SharedGlobalConfig, } from 'kibana/server'; import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/server'; import { @@ -33,8 +34,6 @@ import { MonitoringConfig, createConfig, configSchema } from './config'; import { requireUIRoutes } from './routes'; import { initBulkUploader } from './kibana_monitoring'; import { initInfraSource } from './lib/logs/init_infra_source'; -import { mbSafeQuery } from './lib/mb_safe_query'; -import { instantiateClient } from './es_client/instantiate_client'; import { registerCollectors } from './kibana_monitoring/collectors'; import { registerMonitoringTelemetryCollection } from './telemetry_collection'; import { LicenseService } from './license_service'; @@ -51,7 +50,8 @@ import { RequestHandlerContextMonitoringPlugin, } from './types'; -import { Globals } from './static_globals'; +import { Globals, EndpointTypes } from './static_globals'; +import { instantiateClient } from './es_client/instantiate_client'; // This is used to test the version of kibana const snapshotRegex = /-snapshot/i; @@ -71,50 +71,68 @@ export class MonitoringPlugin private readonly initializerContext: PluginInitializerContext; private readonly log: Logger; private readonly getLogger: (...scopes: string[]) => Logger; - private cluster = {} as ILegacyCustomClusterClient; + private cluster = {} as ICustomClusterClient; private licenseService = {} as MonitoringLicenseService; private monitoringCore = {} as MonitoringCore; private legacyShimDependencies = {} as LegacyShimDependencies; - private bulkUploader: IBulkUploader | undefined; + private bulkUploader?: IBulkUploader; + + private readonly config: MonitoringConfig; + private readonly legacyConfig: SharedGlobalConfig; + private coreSetup?: CoreSetup; + private setupPlugins?: PluginsSetup; constructor(initializerContext: PluginInitializerContext) { 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(); } - setup(core: CoreSetup, plugins: PluginsSetup) { - const config = createConfig(this.initializerContext.config.get>()); - const legacyConfig = this.initializerContext.config.legacy.get(); + setup(coreSetup: CoreSetup, plugins: PluginsSetup) { + this.coreSetup = coreSetup; + this.setupPlugins = plugins; - const router = core.http.createRouter(); - this.legacyShimDependencies = { - router, - instanceUuid: this.initializerContext.env.instanceUuid, - esDataClient: core.elasticsearch.legacy.client, - kibanaStatsCollector: plugins.usageCollection?.getCollectorByType( - KIBANA_STATS_TYPE_MONITORING - ), - }; + const serverInfo = coreSetup.http.getServerInfo(); + const kibanaMonitoringLog = this.getLogger(KIBANA_MONITORING_LOGGING_TAG); + this.bulkUploader = initBulkUploader({ + config: this.config, + log: kibanaMonitoringLog, + opsMetrics$: coreSetup.metrics.getOpsMetrics$(), + statusGetter$: coreSetup.status.overall$, + kibanaStats: { + uuid: this.initializerContext.env.instanceUuid, + name: serverInfo.name, + index: this.legacyConfig.kibana.index, + host: serverInfo.hostname, + locale: i18n.getLocale(), + port: serverInfo.port.toString(), + transport_address: `${serverInfo.hostname}:${serverInfo.port}`, + version: this.initializerContext.env.packageInfo.version, + snapshot: snapshotRegex.test(this.initializerContext.env.packageInfo.version), + }, + }); - // Monitoring creates and maintains a connection to a potentially - // separate ES cluster - create this first - const cluster = (this.cluster = instantiateClient( - config.ui.elasticsearch, - this.log, - core.elasticsearch.legacy.createClient - )); + Globals.init({ + initializerContext: this.initializerContext, + config: this.config!, + getLogger: this.getLogger, + log: this.log, + legacyConfig: this.legacyConfig, + coreSetup: this.coreSetup!, + setupPlugins: this.setupPlugins!, + }); - Globals.init(core, plugins.cloud, cluster, config, this.getLogger); - const serverInfo = core.http.getServerInfo(); const alerts = AlertsFactory.getAll(); for (const alert of alerts) { plugins.alerting?.registerType(alert.getAlertType()); } + const config = createConfig(this.initializerContext.config.get>()); // Register collector objects for stats to show up in the APIs if (plugins.usageCollection) { - core.savedObjects.registerType({ + coreSetup.savedObjects.registerType({ name: SAVED_OBJECT_TELEMETRY, hidden: true, namespaceType: 'agnostic', @@ -127,33 +145,40 @@ export class MonitoringPlugin }, }); - registerCollectors(plugins.usageCollection, config, cluster); + registerCollectors(plugins.usageCollection, config, () => this.cluster); registerMonitoringTelemetryCollection( plugins.usageCollection, - cluster, + () => this.cluster, config.ui.max_bucket_size ); } + if (config.ui.enabled) { + this.registerPluginInUI(plugins); + } - // Always create the bulk uploader - const kibanaMonitoringLog = this.getLogger(KIBANA_MONITORING_LOGGING_TAG); - const bulkUploader = (this.bulkUploader = initBulkUploader({ - config, - log: kibanaMonitoringLog, - opsMetrics$: core.metrics.getOpsMetrics$(), - statusGetter$: core.status.overall$, - kibanaStats: { - uuid: this.initializerContext.env.instanceUuid, - name: serverInfo.name, - index: get(legacyConfig, 'kibana.index'), - host: serverInfo.hostname, - locale: i18n.getLocale(), - port: serverInfo.port.toString(), - transport_address: `${serverInfo.hostname}:${serverInfo.port}`, - version: this.initializerContext.env.packageInfo.version, - snapshot: snapshotRegex.test(this.initializerContext.env.packageInfo.version), - }, - })); + return { + // OSS stats api needs to call this in order to centralize how + // we fetch kibana specific stats + getKibanaStats: () => this.bulkUploader?.getKibanaStats() || {}, + }; + } + + init(cluster: ICustomClusterClient, coreStart: CoreStart) { + const config = createConfig(this.initializerContext.config.get>()); + const legacyConfig = this.initializerContext.config.legacy.get(); + const coreSetup = this.coreSetup!; + const plugins = this.setupPlugins!; + + const router = coreSetup.http.createRouter(); + // const [{ elasticsearch }] = await core.getStartServices(); + this.legacyShimDependencies = { + router, + instanceUuid: this.initializerContext.env.instanceUuid, + esDataClient: coreStart.elasticsearch.client.asInternalUser, + kibanaStatsCollector: plugins.usageCollection?.getCollectorByType( + KIBANA_STATS_TYPE_MONITORING + ), + }; // If the UI is enabled, then we want to register it so it shows up // and start any other UI-related setup tasks @@ -162,12 +187,11 @@ export class MonitoringPlugin this.monitoringCore = this.getLegacyShim( config, legacyConfig, - core.getStartServices as () => Promise<[CoreStart, PluginsStart, {}]>, - this.cluster, + coreSetup.getStartServices as () => Promise<[CoreStart, PluginsStart, {}]>, + cluster, plugins ); - this.registerPluginInUI(plugins); requireUIRoutes(this.monitoringCore, { cluster, router, @@ -177,16 +201,18 @@ export class MonitoringPlugin }); initInfraSource(config, plugins.infra); } - - return { - // OSS stats api needs to call this in order to centralize how - // we fetch kibana specific stats - getKibanaStats: () => bulkUploader.getKibanaStats(), - }; } - async start(core: CoreStart, { licensing }: PluginsStart) { - const config = createConfig(this.initializerContext.config.get>()); + async start(coreStart: CoreStart, { licensing }: PluginsStart) { + const config = this.config!; + this.cluster = instantiateClient( + config.ui.elasticsearch, + this.log, + coreStart.elasticsearch.createClient + ); + + this.init(this.cluster, coreStart); + // Start our license service which will ensure // the appropriate licenses are present this.licenseService = new LicenseService().setup({ @@ -209,7 +235,7 @@ export class MonitoringPlugin const monitoringBulkEnabled = mainMonitoring && mainMonitoring.isAvailable && mainMonitoring.isEnabled; if (monitoringBulkEnabled) { - this.bulkUploader?.start(core.elasticsearch.client.asInternalUser); + this.bulkUploader?.start(coreStart.elasticsearch.client.asInternalUser); } else { this.bulkUploader?.handleNotEnabled(); } @@ -227,7 +253,7 @@ export class MonitoringPlugin } stop() { - if (this.cluster) { + if (this.cluster && this.cluster.close) { this.cluster.close(); } if (this.licenseService && this.licenseService.stop) { @@ -281,7 +307,7 @@ export class MonitoringPlugin config: MonitoringConfig, legacyConfig: any, getCoreServices: () => Promise<[CoreStart, PluginsStart, {}]>, - cluster: ILegacyCustomClusterClient, + cluster: ICustomClusterClient, setupPlugins: PluginsSetup ): MonitoringCore { const router = this.legacyShimDependencies.router; @@ -354,12 +380,12 @@ export class MonitoringPlugin }, elasticsearch: { getCluster: (name: string) => ({ - callWithRequest: async (_req: any, endpoint: string, params: any) => { + callWithRequest: async (_req: any, endpoint: EndpointTypes, params: any) => { const client = - name === 'monitoring' ? cluster : this.legacyShimDependencies.esDataClient; - return mbSafeQuery(() => - client.asScoped(req).callAsCurrentUser(endpoint, params) - ); + name === 'monitoring' + ? cluster.asScoped(req).asCurrentUser + : context.core.elasticsearch.client.asCurrentUser; + return await Globals.app.getLegacyClusterShim(client, endpoint, params); }, }), }, diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/alerts/enable.ts b/x-pack/plugins/monitoring/server/routes/api/v1/alerts/enable.ts index e48f424a3d8ee4..e21304e8458e37 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/alerts/enable.ts +++ b/x-pack/plugins/monitoring/server/routes/api/v1/alerts/enable.ts @@ -93,7 +93,7 @@ export function enableAlertsRoute(server: LegacyServer, npRoute: RouteDependenci let createdAlerts: Array> = []; const disabledWatcherClusterAlerts = await disableWatcherClusterAlerts( - npRoute.cluster.asScoped(request).callAsCurrentUser, + npRoute.cluster.asScoped(request).asCurrentUser, npRoute.logger ); 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 5cc7046777e265..73b646126ce988 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 @@ -99,7 +99,7 @@ function buildRequest( return { index: esIndexPattern, size: maxBucketSize, - filterPath: [ + filter_path: [ 'hits.hits.inner_hits.by_shard.hits.hits._source.ccr_stats.read_exceptions', 'hits.hits.inner_hits.by_shard.hits.hits._source.elasticsearch.ccr.read_exceptions', 'hits.hits.inner_hits.by_shard.hits.hits._source.ccr_stats.follower_index', diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ccr_shard.ts b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ccr_shard.ts index ac5563430fb7ca..5ecb84d97618b0 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ccr_shard.ts +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ccr_shard.ts @@ -35,7 +35,7 @@ async function getCcrStat(req: LegacyRequest, esIndexPattern: string, filters: u const params = { index: esIndexPattern, size: 1, - filterPath: [ + filter_path: [ 'hits.hits._source.ccr_stats', 'hits.hits._source.elasticsearch.ccr', 'hits.hits._source.timestamp', diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/cluster.js b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/cluster.js index b8cdec9ed9b00f..6996c4885d25dc 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/cluster.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch_settings/check/cluster.js @@ -23,6 +23,7 @@ export function clusterSettingsCheckRoute(server) { const response = await checkClusterSettings(req); // needs to be try/catch to handle privilege error return response; } catch (err) { + console.log(err); throw handleSettingsError(err); } }, 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 1c4ccdada095f6..c2bad7b905c5b1 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 @@ -7,6 +7,7 @@ import { schema } from '@kbn/config-schema'; import { RequestHandlerContext } from 'kibana/server'; +import { estypes } from '@elastic/elasticsearch'; import { INDEX_PATTERN_ELASTICSEARCH, INDEX_PATTERN_KIBANA, @@ -44,11 +45,11 @@ const queryBody = { }; const checkLatestMonitoringIsLegacy = async (context: RequestHandlerContext, index: string) => { - const { client: esClient } = context.core.elasticsearch.legacy; - const result = await esClient.callAsCurrentUser('search', { + const client = context.core.elasticsearch.client.asCurrentUser; + const { body: result } = await client.search>({ index, body: queryBody, - }); + } as estypes.SearchRequest); const { aggregations } = result; const counts = { @@ -62,7 +63,7 @@ const checkLatestMonitoringIsLegacy = async (context: RequestHandlerContext, ind const { types: { buckets }, - } = aggregations; + } = aggregations as { types: { buckets: Array<{ key: string }> } }; counts.mbIndicesCount = buckets.filter(({ key }: { key: string }) => key.includes('-mb-')).length; counts.legacyIndicesCount = buckets.length - counts.mbIndicesCount; diff --git a/x-pack/plugins/monitoring/server/static_globals.ts b/x-pack/plugins/monitoring/server/static_globals.ts index 1bb08d6cafc384..fc4ece32314864 100644 --- a/x-pack/plugins/monitoring/server/static_globals.ts +++ b/x-pack/plugins/monitoring/server/static_globals.ts @@ -5,20 +5,50 @@ * 2.0. */ -import { CoreSetup, ILegacyCustomClusterClient, Logger } from 'kibana/server'; +import { + CoreSetup, + ElasticsearchClient, + Logger, + SharedGlobalConfig, + PluginInitializerContext, +} from 'kibana/server'; import url from 'url'; -import { CloudSetup } from '../../cloud/server'; +import { estypes } from '@elastic/elasticsearch'; import { MonitoringConfig } from './config'; - +import { PluginsSetup } from './types'; +import { mbSafeQuery } from './lib/mb_safe_query'; type GetLogger = (...scopes: string[]) => Logger; +interface InitSetupOptions { + initializerContext: PluginInitializerContext; + coreSetup: CoreSetup; + config: MonitoringConfig; + getLogger: GetLogger; + log: Logger; + legacyConfig: SharedGlobalConfig; + setupPlugins: PluginsSetup; +} + +export type EndpointTypes = + | 'search' + | 'msearch' + | 'transport.request' + | 'cluster.putSettings' + | 'cluster.getSettings' + | string; +export type ClientParams = estypes.SearchRequest | undefined; + interface IAppGlobals { url: string; isCloud: boolean; - monitoringCluster: ILegacyCustomClusterClient; config: MonitoringConfig; getLogger: GetLogger; getKeyStoreValue: (key: string, storeValueMethod?: () => unknown) => unknown; + getLegacyClusterShim: ( + client: ElasticsearchClient, + endpoint: EndpointTypes, + params: ClientParams + ) => any; } interface KeyStoreData { @@ -37,22 +67,35 @@ const getKeyStoreValue = (key: string, storeValueMethod?: () => unknown) => { export class Globals { private static _app: IAppGlobals; - public static init( - coreSetup: CoreSetup, - cloud: CloudSetup | undefined, - monitoringCluster: ILegacyCustomClusterClient, - config: MonitoringConfig, - getLogger: GetLogger - ) { + public static init(options: InitSetupOptions) { + const { coreSetup, setupPlugins, config, getLogger } = options; + const getLegacyClusterShim = async ( + client: ElasticsearchClient, + endpoint: EndpointTypes, + params: ClientParams + ): Promise => + await mbSafeQuery(async () => { + const endpointMap: { [key: string]: (params: any) => any } = { + search: (p) => client.search(p), + msearch: (p) => client.msearch(p), + 'transport.request': (p) => client.transport.request(p), + 'cluster.getSettings': (p) => client.cluster.getSettings(p), + 'cluster.putSettings': (p) => client.cluster.putSettings(p), + }; + const { body } = await endpointMap[endpoint](params); + return body; + }); + const { protocol, hostname, port } = coreSetup.http.getServerInfo(); const pathname = coreSetup.http.basePath.serverBasePath; + Globals._app = { url: url.format({ protocol, hostname, port, pathname }), - isCloud: cloud?.isCloudEnabled || false, - monitoringCluster, + isCloud: setupPlugins.cloud?.isCloudEnabled || false, config, getLogger, getKeyStoreValue, + getLegacyClusterShim, }; } @@ -64,4 +107,6 @@ export class Globals { } return Globals._app; } + + public static stop() {} } diff --git a/x-pack/plugins/monitoring/server/telemetry_collection/get_all_stats.test.ts b/x-pack/plugins/monitoring/server/telemetry_collection/get_all_stats.test.ts index 33487ecafd8c52..8933833bb068a2 100644 --- a/x-pack/plugins/monitoring/server/telemetry_collection/get_all_stats.test.ts +++ b/x-pack/plugins/monitoring/server/telemetry_collection/get_all_stats.test.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { ElasticsearchClient } from 'kibana/server'; import sinon from 'sinon'; import { getStackStats, getAllStats, handleAllStats } from './get_all_stats'; import { ESClusterStats } from './get_es_stats'; @@ -13,7 +14,11 @@ import { LogstashStatsByClusterUuid } from './get_logstash_stats'; describe('get_all_stats', () => { const timestamp = Date.now(); - const callCluster = sinon.stub(); + const searchMock = sinon.stub(); + const callCluster = ({ search: searchMock } as unknown) as ElasticsearchClient; + afterEach(() => { + searchMock.reset(); + }); const esClusters = [ { cluster_uuid: 'a' }, @@ -157,18 +162,20 @@ describe('get_all_stats', () => { }, ]; - callCluster - .withArgs('search') + searchMock .onCall(0) - .returns(Promise.resolve(esStatsResponse)) + .returns(Promise.resolve({ body: esStatsResponse })) .onCall(1) - .returns(Promise.resolve(kibanaStatsResponse)) + .returns(Promise.resolve({ body: kibanaStatsResponse })) .onCall(2) - .returns(Promise.resolve(logstashStatsResponse)) + .returns(Promise.resolve({ body: logstashStatsResponse })) + .returns(Promise.resolve({ body: logstashStatsResponse })) .onCall(3) - .returns(Promise.resolve({})) // Beats stats + .returns(Promise.resolve({ body: {} })) // Beats stats .onCall(4) - .returns(Promise.resolve({})); // Beats state + .returns(Promise.resolve({ body: {} })) // Beats state + .onCall(5) + .returns(Promise.resolve({ body: {} })); // Logstash state expect(await getAllStats(['a'], callCluster, timestamp, 1)).toStrictEqual(allClusters); }); @@ -178,7 +185,7 @@ describe('get_all_stats', () => { aggregations: { cluster_uuids: { buckets: [] } }, }; - callCluster.withArgs('search').returns(Promise.resolve(clusterUuidsResponse)); + searchMock.returns(Promise.resolve({ body: clusterUuidsResponse })); expect(await getAllStats([], callCluster, timestamp, 1)).toStrictEqual([]); }); diff --git a/x-pack/plugins/monitoring/server/telemetry_collection/get_all_stats.ts b/x-pack/plugins/monitoring/server/telemetry_collection/get_all_stats.ts index 60b107cb293426..a0bad277dacf66 100644 --- a/x-pack/plugins/monitoring/server/telemetry_collection/get_all_stats.ts +++ b/x-pack/plugins/monitoring/server/telemetry_collection/get_all_stats.ts @@ -9,7 +9,7 @@ import { set } from '@elastic/safer-lodash-set'; import { get, merge } from 'lodash'; import moment from 'moment'; -import { LegacyAPICaller } from 'kibana/server'; +import { ElasticsearchClient } from 'kibana/server'; import { LOGSTASH_SYSTEM_ID, KIBANA_SYSTEM_ID, @@ -28,7 +28,7 @@ import { getLogstashStats, LogstashStatsByClusterUuid } from './get_logstash_sta */ export async function getAllStats( clusterUuids: string[], - callCluster: LegacyAPICaller, // TODO: To be changed to the new ES client when the plugin migrates + callCluster: ElasticsearchClient, timestamp: number, maxBucketSize: number ) { diff --git a/x-pack/plugins/monitoring/server/telemetry_collection/get_beats_stats.test.ts b/x-pack/plugins/monitoring/server/telemetry_collection/get_beats_stats.test.ts index 3637ace3aeb63e..36477cb48cd891 100644 --- a/x-pack/plugins/monitoring/server/telemetry_collection/get_beats_stats.test.ts +++ b/x-pack/plugins/monitoring/server/telemetry_collection/get_beats_stats.test.ts @@ -7,6 +7,7 @@ import { fetchBeatsStats, processResults } from './get_beats_stats'; import sinon from 'sinon'; +import { ElasticsearchClient } from 'kibana/server'; // eslint-disable-next-line @typescript-eslint/no-var-requires const beatsStatsResultSet = require('./__mocks__/fixtures/beats_stats_results'); @@ -23,40 +24,41 @@ describe('Get Beats Stats', () => { const clusterUuids = ['aCluster', 'bCluster', 'cCluster']; const start = new Date().toISOString(); const end = new Date().toISOString(); - let callCluster = sinon.stub(); + const searchMock = sinon.stub(); + const callCluster = ({ search: searchMock } as unknown) as ElasticsearchClient; beforeEach(() => { const getStub = { get: sinon.stub() }; getStub.get.withArgs('xpack.monitoring.beats.index_pattern').returns('beats-indices-*'); - callCluster = sinon.stub(); + searchMock.reset(); }); it('should set `from: 0, to: 10000` in the query', async () => { + searchMock.returns(Promise.resolve({ body: {} })); await fetchBeatsStats(callCluster, clusterUuids, start, end, {} as any); - const { args } = callCluster.firstCall; - const [api, { body }] = args; + const { args } = searchMock.firstCall; + const [{ body }] = args; - expect(api).toEqual('search'); expect(body.from).toEqual(0); expect(body.size).toEqual(10000); }); it('should set `from: 10000, from: 10000` in the query', async () => { + searchMock.returns(Promise.resolve({ body: {} })); await fetchBeatsStats(callCluster, clusterUuids, start, end, { page: 1 } as any); - const { args } = callCluster.firstCall; - const [api, { body }] = args; + const { args } = searchMock.firstCall; + const [{ body }] = args; - expect(api).toEqual('search'); expect(body.from).toEqual(10000); expect(body.size).toEqual(10000); }); it('should set `from: 20000, from: 10000` in the query', async () => { + searchMock.returns(Promise.resolve({ body: {} })); await fetchBeatsStats(callCluster, clusterUuids, start, end, { page: 2 } as any); - const { args } = callCluster.firstCall; - const [api, { body }] = args; + const { args } = searchMock.firstCall; + const [{ body }] = args; - expect(api).toEqual('search'); expect(body.from).toEqual(20000); expect(body.size).toEqual(10000); }); diff --git a/x-pack/plugins/monitoring/server/telemetry_collection/get_beats_stats.ts b/x-pack/plugins/monitoring/server/telemetry_collection/get_beats_stats.ts index ba83584e346545..c62fbf4e8a1cfb 100644 --- a/x-pack/plugins/monitoring/server/telemetry_collection/get_beats_stats.ts +++ b/x-pack/plugins/monitoring/server/telemetry_collection/get_beats_stats.ts @@ -7,7 +7,8 @@ import { get } from 'lodash'; import { SearchResponse } from 'elasticsearch'; -import { LegacyAPICaller } from 'kibana/server'; +import { ElasticsearchClient } from 'kibana/server'; +import { estypes } from '@elastic/elasticsearch'; import { createQuery } from './create_query'; import { INDEX_PATTERN_BEATS } from '../../common/constants'; @@ -319,17 +320,17 @@ export function processResults( * @return {Promise} */ async function fetchBeatsByType( - callCluster: LegacyAPICaller, + callCluster: ElasticsearchClient, clusterUuids: string[], start: string, end: string, { page = 0, ...options }: { page?: number } & BeatsProcessOptions, type: string ): Promise { - const params = { + const params: estypes.SearchRequest = { index: INDEX_PATTERN_BEATS, - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ 'hits.hits._source.cluster_uuid', 'hits.hits._source.type', 'hits.hits._source.beats_stats.beat.version', @@ -353,7 +354,7 @@ async function fetchBeatsByType( }, }, ], - }), + }) as estypes.QueryDslQueryContainer, from: page * HITS_SIZE, collapse: { field: `${type}.beat.uuid` }, sort: [{ [`${type}.timestamp`]: { order: 'desc', unmapped_type: 'long' } }], @@ -361,11 +362,11 @@ async function fetchBeatsByType( }, }; - const results = await callCluster>('search', params); + const { body: results } = await callCluster.search(params); const hitsLength = results?.hits?.hits.length || 0; if (hitsLength > 0) { // further augment the clusters object with more stats - processResults(results, options); + processResults(results as SearchResponse, options); if (hitsLength === HITS_SIZE) { // call recursively @@ -383,7 +384,7 @@ async function fetchBeatsByType( } export async function fetchBeatsStats( - callCluster: LegacyAPICaller, + callCluster: ElasticsearchClient, clusterUuids: string[], start: string, end: string, @@ -393,7 +394,7 @@ export async function fetchBeatsStats( } export async function fetchBeatsStates( - callCluster: LegacyAPICaller, + callCluster: ElasticsearchClient, clusterUuids: string[], start: string, end: string, @@ -411,7 +412,7 @@ export interface BeatsStatsByClusterUuid { * @return {Object} - Beats stats in an object keyed by the cluster UUIDs */ export async function getBeatsStats( - callCluster: LegacyAPICaller, + callCluster: ElasticsearchClient, clusterUuids: string[], start: string, end: string diff --git a/x-pack/plugins/monitoring/server/telemetry_collection/get_cluster_uuids.test.ts b/x-pack/plugins/monitoring/server/telemetry_collection/get_cluster_uuids.test.ts index 07d623867bc1c6..7e7ec23b69daf3 100644 --- a/x-pack/plugins/monitoring/server/telemetry_collection/get_cluster_uuids.test.ts +++ b/x-pack/plugins/monitoring/server/telemetry_collection/get_cluster_uuids.test.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { ElasticsearchClient } from 'kibana/server'; import sinon from 'sinon'; import { getClusterUuids, @@ -13,40 +14,52 @@ import { } from './get_cluster_uuids'; describe('get_cluster_uuids', () => { - const callCluster = sinon.stub(); + const searchMock = sinon.stub(); + const callCluster = ({ search: searchMock } as unknown) as ElasticsearchClient; + + afterEach(() => { + searchMock.reset(); + }); + const response = { - aggregations: { - cluster_uuids: { - buckets: [{ key: 'abc' }, { key: 'xyz' }, { key: '123' }], + body: { + aggregations: { + cluster_uuids: { + buckets: [{ key: 'abc' }, { key: 'xyz' }, { key: '123' }], + }, }, }, }; - const expectedUuids = response.aggregations.cluster_uuids.buckets.map((bucket) => bucket.key); + + const expectedUuids = response.body.aggregations.cluster_uuids.buckets.map( + (bucket) => bucket.key + ); + const timestamp = Date.now(); describe('getClusterUuids', () => { it('returns cluster UUIDs', async () => { - callCluster.withArgs('search').returns(Promise.resolve(response)); + searchMock.returns(Promise.resolve(response)); expect(await getClusterUuids(callCluster, timestamp, 1)).toStrictEqual(expectedUuids); }); }); describe('fetchClusterUuids', () => { it('searches for clusters', async () => { - callCluster.returns(Promise.resolve(response)); - expect(await fetchClusterUuids(callCluster, timestamp, 1)).toStrictEqual(response); + searchMock.returns(Promise.resolve(response)); + expect(await fetchClusterUuids(callCluster, timestamp, 1)).toStrictEqual(response.body); }); }); describe('handleClusterUuidsResponse', () => { - // filterPath makes it easy to ignore anything unexpected because it will come back empty + // filter_path makes it easy to ignore anything unexpected because it will come back empty it('handles unexpected response', () => { const clusterUuids = handleClusterUuidsResponse({}); expect(clusterUuids.length).toStrictEqual(0); }); it('handles valid response', () => { - const clusterUuids = handleClusterUuidsResponse(response); + const clusterUuids = handleClusterUuidsResponse(response.body); expect(clusterUuids).toStrictEqual(expectedUuids); }); diff --git a/x-pack/plugins/monitoring/server/telemetry_collection/get_cluster_uuids.ts b/x-pack/plugins/monitoring/server/telemetry_collection/get_cluster_uuids.ts index db7a031385b4a7..7cf4ca2b94ce01 100644 --- a/x-pack/plugins/monitoring/server/telemetry_collection/get_cluster_uuids.ts +++ b/x-pack/plugins/monitoring/server/telemetry_collection/get_cluster_uuids.ts @@ -7,7 +7,8 @@ import { get } from 'lodash'; import moment from 'moment'; -import { LegacyAPICaller } from 'kibana/server'; +import { ElasticsearchClient } from 'kibana/server'; +import { estypes } from '@elastic/elasticsearch'; import { createQuery } from './create_query'; import { INDEX_PATTERN_ELASTICSEARCH, @@ -18,7 +19,7 @@ import { * Get a list of Cluster UUIDs that exist within the specified timespan. */ export async function getClusterUuids( - callCluster: LegacyAPICaller, // TODO: To be changed to the new ES client when the plugin migrates + callCluster: ElasticsearchClient, timestamp: number, maxBucketSize: number ) { @@ -30,7 +31,7 @@ export async function getClusterUuids( * Fetch the aggregated Cluster UUIDs from the monitoring cluster. */ export async function fetchClusterUuids( - callCluster: LegacyAPICaller, + callCluster: ElasticsearchClient, timestamp: number, maxBucketSize: number ) { @@ -38,13 +39,13 @@ export async function fetchClusterUuids( const end = moment(timestamp).toISOString(); - const params = { + const params: estypes.SearchRequest = { index: INDEX_PATTERN_ELASTICSEARCH, size: 0, - ignoreUnavailable: true, - filterPath: 'aggregations.cluster_uuids.buckets.key', + ignore_unavailable: true, + filter_path: 'aggregations.cluster_uuids.buckets.key', body: { - query: createQuery({ type: 'cluster_stats', start, end }), + query: createQuery({ type: 'cluster_stats', start, end }) as estypes.QueryDslQueryContainer, aggs: { cluster_uuids: { terms: { @@ -56,7 +57,8 @@ export async function fetchClusterUuids( }, }; - return await callCluster('search', params); + const { body: response } = await callCluster.search(params); + return response; } /** diff --git a/x-pack/plugins/monitoring/server/telemetry_collection/get_es_stats.test.ts b/x-pack/plugins/monitoring/server/telemetry_collection/get_es_stats.test.ts index 033b3ebffe5f16..3b5f654be42223 100644 --- a/x-pack/plugins/monitoring/server/telemetry_collection/get_es_stats.test.ts +++ b/x-pack/plugins/monitoring/server/telemetry_collection/get_es_stats.test.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { ElasticsearchClient } from 'kibana/server'; import sinon from 'sinon'; import { fetchElasticsearchStats, @@ -13,8 +14,9 @@ import { } from './get_es_stats'; describe('get_es_stats', () => { - const callWith = sinon.stub(); - const response = { + const searchMock = sinon.stub(); + const client = ({ search: searchMock } as unknown) as ElasticsearchClient; + const body = { hits: { hits: [ { _id: 'abc', _source: { cluster_uuid: 'abc' } }, @@ -23,15 +25,15 @@ describe('get_es_stats', () => { ], }, }; - const expectedClusters = response.hits.hits.map((hit) => hit._source); + const expectedClusters = body.hits.hits.map((hit) => hit._source); const clusterUuids = expectedClusters.map((cluster) => cluster.cluster_uuid); const maxBucketSize = 1; describe('getElasticsearchStats', () => { it('returns clusters', async () => { - callWith.withArgs('search').returns(Promise.resolve(response)); + searchMock.returns(Promise.resolve({ body })); - expect(await getElasticsearchStats(callWith, clusterUuids, maxBucketSize)).toStrictEqual( + expect(await getElasticsearchStats(client, clusterUuids, maxBucketSize)).toStrictEqual( expectedClusters ); }); @@ -39,16 +41,16 @@ describe('get_es_stats', () => { describe('fetchElasticsearchStats', () => { it('searches for clusters', async () => { - callWith.returns(response); + searchMock.returns({ body }); - expect(await fetchElasticsearchStats(callWith, clusterUuids, maxBucketSize)).toStrictEqual( - response + expect(await fetchElasticsearchStats(client, clusterUuids, maxBucketSize)).toStrictEqual( + body ); }); }); describe('handleElasticsearchStats', () => { - // filterPath makes it easy to ignore anything unexpected because it will come back empty + // filter_path makes it easy to ignore anything unexpected because it will come back empty it('handles unexpected response', () => { const clusters = handleElasticsearchStats({} as any); @@ -56,7 +58,7 @@ describe('get_es_stats', () => { }); it('handles valid response', () => { - const clusters = handleElasticsearchStats(response as any); + const clusters = handleElasticsearchStats(body as any); expect(clusters).toStrictEqual(expectedClusters); }); diff --git a/x-pack/plugins/monitoring/server/telemetry_collection/get_es_stats.ts b/x-pack/plugins/monitoring/server/telemetry_collection/get_es_stats.ts index f309e01c961187..217ab70ab0ecaa 100644 --- a/x-pack/plugins/monitoring/server/telemetry_collection/get_es_stats.ts +++ b/x-pack/plugins/monitoring/server/telemetry_collection/get_es_stats.ts @@ -6,7 +6,8 @@ */ import { SearchResponse } from 'elasticsearch'; -import { LegacyAPICaller } from 'kibana/server'; +import { ElasticsearchClient } from 'kibana/server'; +import { estypes } from '@elastic/elasticsearch'; import { INDEX_PATTERN_ELASTICSEARCH } from '../../common/constants'; /** @@ -17,7 +18,7 @@ import { INDEX_PATTERN_ELASTICSEARCH } from '../../common/constants'; * @param {Array} clusterUuids The string Cluster UUIDs to fetch details for */ export async function getElasticsearchStats( - callCluster: LegacyAPICaller, + callCluster: ElasticsearchClient, clusterUuids: string[], maxBucketSize: number ) { @@ -34,16 +35,16 @@ export async function getElasticsearchStats( * * Returns the response for the aggregations to fetch details for the product. */ -export function fetchElasticsearchStats( - callCluster: LegacyAPICaller, +export async function fetchElasticsearchStats( + callCluster: ElasticsearchClient, clusterUuids: string[], maxBucketSize: number ) { - const params = { + const params: estypes.SearchRequest = { index: INDEX_PATTERN_ELASTICSEARCH, size: maxBucketSize, - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ 'hits.hits._source.cluster_uuid', 'hits.hits._source.timestamp', 'hits.hits._source.cluster_name', @@ -69,7 +70,8 @@ export function fetchElasticsearchStats( }, }; - return callCluster('search', params); + const { body: response } = await callCluster.search(params); + return response as SearchResponse; } export interface ESClusterStats { diff --git a/x-pack/plugins/monitoring/server/telemetry_collection/get_high_level_stats.test.ts b/x-pack/plugins/monitoring/server/telemetry_collection/get_high_level_stats.test.ts index e2eed7381f4511..00a14f7b12f7ed 100644 --- a/x-pack/plugins/monitoring/server/telemetry_collection/get_high_level_stats.test.ts +++ b/x-pack/plugins/monitoring/server/telemetry_collection/get_high_level_stats.test.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { ElasticsearchClient } from 'kibana/server'; import sinon from 'sinon'; import { fetchHighLevelStats, @@ -13,12 +14,13 @@ import { } from './get_high_level_stats'; describe('get_high_level_stats', () => { - const callWith = sinon.stub(); + const searchMock = sinon.stub(); + const callCluster = ({ search: searchMock } as unknown) as ElasticsearchClient; const product = 'xyz'; const cloudName = 'bare-metal'; const start = new Date().toISOString(); const end = new Date().toISOString(); - const response = { + const body = { hits: { hits: [ { @@ -232,26 +234,26 @@ describe('get_high_level_stats', () => { describe('getHighLevelStats', () => { it('returns clusters', async () => { - callWith.withArgs('search').returns(Promise.resolve(response)); + searchMock.returns(Promise.resolve({ body })); expect( - await getHighLevelStats(callWith, clusterUuids, start, end, product, maxBucketSize) + await getHighLevelStats(callCluster, clusterUuids, start, end, product, maxBucketSize) ).toStrictEqual(expectedClusters); }); }); describe('fetchHighLevelStats', () => { it('searches for clusters', async () => { - callWith.returns(Promise.resolve(response)); + searchMock.returns(Promise.resolve({ body })); expect( - await fetchHighLevelStats(callWith, clusterUuids, start, end, product, maxBucketSize) - ).toStrictEqual(response); + await fetchHighLevelStats(callCluster, clusterUuids, start, end, product, maxBucketSize) + ).toStrictEqual(body); }); }); describe('handleHighLevelStatsResponse', () => { - // filterPath makes it easy to ignore anything unexpected because it will come back empty + // filter_path makes it easy to ignore anything unexpected because it will come back empty it('handles unexpected response', () => { const clusters = handleHighLevelStatsResponse({} as any, product); @@ -259,7 +261,7 @@ describe('get_high_level_stats', () => { }); it('handles valid response', () => { - const clusters = handleHighLevelStatsResponse(response as any, product); + const clusters = handleHighLevelStatsResponse(body as any, product); expect(clusters).toStrictEqual(expectedClusters); }); diff --git a/x-pack/plugins/monitoring/server/telemetry_collection/get_high_level_stats.ts b/x-pack/plugins/monitoring/server/telemetry_collection/get_high_level_stats.ts index 63188be142fdd6..2fb3814ac40224 100644 --- a/x-pack/plugins/monitoring/server/telemetry_collection/get_high_level_stats.ts +++ b/x-pack/plugins/monitoring/server/telemetry_collection/get_high_level_stats.ts @@ -7,7 +7,8 @@ import { get } from 'lodash'; import { SearchResponse } from 'elasticsearch'; -import { LegacyAPICaller } from 'kibana/server'; +import { ElasticsearchClient } from 'kibana/server'; +import { estypes } from '@elastic/elasticsearch'; import { createQuery } from './create_query'; import { INDEX_PATTERN_KIBANA, @@ -248,7 +249,7 @@ function getIndexPatternForStackProduct(product: string) { * Returns an object keyed by the cluster UUIDs to make grouping easier. */ export async function getHighLevelStats( - callCluster: LegacyAPICaller, + callCluster: ElasticsearchClient, clusterUuids: string[], start: string, end: string, @@ -269,7 +270,7 @@ export async function getHighLevelStats( export async function fetchHighLevelStats< T extends { cluster_uuid?: string } = { cluster_uuid?: string } >( - callCluster: LegacyAPICaller, + callCluster: ElasticsearchClient, clusterUuids: string[], start: string, end: string, @@ -300,14 +301,11 @@ export async function fetchHighLevelStats< filters.push(kibanaFilter); } - const params = { - index: getIndexPatternForStackProduct(product), + const params: estypes.SearchRequest = { + index: getIndexPatternForStackProduct(product) as string, size: maxBucketSize, - headers: { - 'X-QUERY-SOURCE': TELEMETRY_QUERY_SOURCE, - }, - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ 'hits.hits._source.cluster_uuid', `hits.hits._source.${product}_stats.${product}.version`, `hits.hits._source.${product}_stats.os`, @@ -325,7 +323,7 @@ export async function fetchHighLevelStats< end, type: `${product}_stats`, filters, - }), + }) as estypes.QueryDslQueryContainer, collapse: { // a more ideal field would be the concatenation of the uuid + transport address for duped UUIDs (copied installations) field: `${product}_stats.${product}.uuid`, @@ -334,7 +332,12 @@ export async function fetchHighLevelStats< }, }; - return callCluster('search', params); + const { body: response } = await callCluster.search(params, { + headers: { + 'X-QUERY-SOURCE': TELEMETRY_QUERY_SOURCE, + }, + }); + return response as SearchResponse; } /** diff --git a/x-pack/plugins/monitoring/server/telemetry_collection/get_kibana_stats.ts b/x-pack/plugins/monitoring/server/telemetry_collection/get_kibana_stats.ts index 59a77bd7a6b4c0..4be5e02dff3a66 100644 --- a/x-pack/plugins/monitoring/server/telemetry_collection/get_kibana_stats.ts +++ b/x-pack/plugins/monitoring/server/telemetry_collection/get_kibana_stats.ts @@ -8,7 +8,7 @@ import moment from 'moment'; import { isEmpty } from 'lodash'; import { SearchResponse } from 'elasticsearch'; -import { LegacyAPICaller } from 'kibana/server'; +import { ElasticsearchClient } from 'kibana/server'; import { KIBANA_SYSTEM_ID, TELEMETRY_COLLECTION_INTERVAL } from '../../common/constants'; import { fetchHighLevelStats, @@ -183,7 +183,7 @@ export function ensureTimeSpan( * specialized usage data that comes with kibana stats (kibana_stats.usage). */ export async function getKibanaStats( - callCluster: LegacyAPICaller, + callCluster: ElasticsearchClient, clusterUuids: string[], start: string, end: string, diff --git a/x-pack/plugins/monitoring/server/telemetry_collection/get_licenses.test.ts b/x-pack/plugins/monitoring/server/telemetry_collection/get_licenses.test.ts index ce4bf523b3e1ae..98f1daf57a85a6 100644 --- a/x-pack/plugins/monitoring/server/telemetry_collection/get_licenses.test.ts +++ b/x-pack/plugins/monitoring/server/telemetry_collection/get_licenses.test.ts @@ -5,12 +5,14 @@ * 2.0. */ +import { ElasticsearchClient } from 'kibana/server'; import sinon from 'sinon'; import { getLicenses, handleLicenses, fetchLicenses } from './get_licenses'; describe('get_licenses', () => { - const callWith = sinon.stub(); - const response = { + const searchMock = sinon.stub(); + const client = ({ search: searchMock } as unknown) as ElasticsearchClient; + const body = { hits: { hits: [ { _id: 'abc', _source: { cluster_uuid: 'abc', license: { type: 'basic' } } }, @@ -19,7 +21,7 @@ describe('get_licenses', () => { ], }, }; - const expectedClusters = response.hits.hits.map((hit) => hit._source); + const expectedClusters = body.hits.hits.map((hit) => hit._source); const clusterUuids = expectedClusters.map((cluster) => cluster.cluster_uuid); const expectedLicenses = { abc: { type: 'basic' }, @@ -29,22 +31,22 @@ describe('get_licenses', () => { describe('getLicenses', () => { it('returns clusters', async () => { - callWith.withArgs('search').returns(Promise.resolve(response)); + searchMock.returns(Promise.resolve({ body })); - expect(await getLicenses(clusterUuids, callWith, 1)).toStrictEqual(expectedLicenses); + expect(await getLicenses(clusterUuids, client, 1)).toStrictEqual(expectedLicenses); }); }); describe('fetchLicenses', () => { it('searches for clusters', async () => { - callWith.returns(response); + searchMock.returns({ body }); - expect(await fetchLicenses(callWith, clusterUuids, 1)).toStrictEqual(response); + expect(await fetchLicenses(client, clusterUuids, 1)).toStrictEqual(body); }); }); describe('handleLicenses', () => { - // filterPath makes it easy to ignore anything unexpected because it will come back empty + // filter_path makes it easy to ignore anything unexpected because it will come back empty it('handles unexpected response', () => { const clusters = handleLicenses({} as any); @@ -52,7 +54,7 @@ describe('get_licenses', () => { }); it('handles valid response', () => { - const clusters = handleLicenses(response as any); + const clusters = handleLicenses(body as any); expect(clusters).toStrictEqual(expectedLicenses); }); diff --git a/x-pack/plugins/monitoring/server/telemetry_collection/get_licenses.ts b/x-pack/plugins/monitoring/server/telemetry_collection/get_licenses.ts index 3115e2adbdad27..633e37a2049bdd 100644 --- a/x-pack/plugins/monitoring/server/telemetry_collection/get_licenses.ts +++ b/x-pack/plugins/monitoring/server/telemetry_collection/get_licenses.ts @@ -6,7 +6,8 @@ */ import { SearchResponse } from 'elasticsearch'; -import { LegacyAPICaller } from 'kibana/server'; +import { ElasticsearchClient } from 'kibana/server'; +import { estypes } from '@elastic/elasticsearch'; import { ESLicense } from '../../../telemetry_collection_xpack/server'; import { INDEX_PATTERN_ELASTICSEARCH } from '../../common/constants'; @@ -15,7 +16,7 @@ import { INDEX_PATTERN_ELASTICSEARCH } from '../../common/constants'; */ export async function getLicenses( clusterUuids: string[], - callCluster: LegacyAPICaller, // TODO: To be changed to the new ES client when the plugin migrates + callCluster: ElasticsearchClient, // TODO: To be changed to the new ES client when the plugin migrates maxBucketSize: number ): Promise<{ [clusterUuid: string]: ESLicense | undefined }> { const response = await fetchLicenses(callCluster, clusterUuids, maxBucketSize); @@ -31,16 +32,16 @@ export async function getLicenses( * * Returns the response for the aggregations to fetch details for the product. */ -export function fetchLicenses( - callCluster: LegacyAPICaller, +export async function fetchLicenses( + callCluster: ElasticsearchClient, clusterUuids: string[], maxBucketSize: number ) { - const params = { + const params: estypes.SearchRequest = { index: INDEX_PATTERN_ELASTICSEARCH, size: maxBucketSize, - ignoreUnavailable: true, - filterPath: ['hits.hits._source.cluster_uuid', 'hits.hits._source.license'], + ignore_unavailable: true, + filter_path: ['hits.hits._source.cluster_uuid', 'hits.hits._source.license'], body: { query: { bool: { @@ -59,7 +60,8 @@ export function fetchLicenses( }, }; - return callCluster('search', params); + const { body: response } = await callCluster.search(params); + return response as SearchResponse; } export interface ESClusterStatsWithLicense { diff --git a/x-pack/plugins/monitoring/server/telemetry_collection/get_logstash_stats.test.ts b/x-pack/plugins/monitoring/server/telemetry_collection/get_logstash_stats.test.ts index cf1574f8d3f0ee..cd77d28f6c430d 100644 --- a/x-pack/plugins/monitoring/server/telemetry_collection/get_logstash_stats.test.ts +++ b/x-pack/plugins/monitoring/server/telemetry_collection/get_logstash_stats.test.ts @@ -12,6 +12,7 @@ import { processLogstashStateResults, } from './get_logstash_stats'; import sinon from 'sinon'; +import { ElasticsearchClient } from 'kibana/server'; // eslint-disable-next-line @typescript-eslint/no-var-requires const logstashStatsResultSet = require('./__mocks__/fixtures/logstash_stats_results'); @@ -33,10 +34,15 @@ const getBaseOptions = () => ({ describe('Get Logstash Stats', () => { const clusterUuids = ['aCluster', 'bCluster', 'cCluster']; - let callCluster = sinon.stub(); + const searchMock = sinon.stub(); + const callCluster = ({ search: searchMock } as unknown) as ElasticsearchClient; beforeEach(() => { - callCluster = sinon.stub(); + searchMock.returns(Promise.resolve({ body: {} })); + }); + + afterEach(() => { + searchMock.reset(); }); describe('fetchLogstashState', () => { @@ -63,17 +69,15 @@ describe('Get Logstash Stats', () => { }; await fetchLogstashState(callCluster, clusterUuid, ephemeralIds, {} as any); - const { args } = callCluster.firstCall; - const [api, { body }] = args; - expect(api).toEqual('search'); + const { args } = searchMock.firstCall; + const [{ body }] = args; expect(body.query).toEqual(expected); }); it('should set `from: 0, to: 10000` in the query', async () => { await fetchLogstashState(callCluster, clusterUuid, ephemeralIds, {} as any); - const { args } = callCluster.firstCall; - const [api, { body }] = args; - expect(api).toEqual('search'); + const { args } = searchMock.firstCall; + const [{ body }] = args; expect(body.from).toEqual(0); expect(body.size).toEqual(10000); }); @@ -82,10 +86,9 @@ describe('Get Logstash Stats', () => { await fetchLogstashState(callCluster, clusterUuid, ephemeralIds, { page: 1, } as any); - const { args } = callCluster.firstCall; - const [api, { body }] = args; + const { args } = searchMock.firstCall; + const [{ body }] = args; - expect(api).toEqual('search'); expect(body.from).toEqual(10000); expect(body.size).toEqual(10000); }); @@ -94,10 +97,9 @@ describe('Get Logstash Stats', () => { await fetchLogstashState(callCluster, clusterUuid, ephemeralIds, { page: 2, } as any); - const { args } = callCluster.firstCall; - const [api, { body }] = args; + const { args } = searchMock.firstCall; + const [{ body }] = args; - expect(api).toEqual('search'); expect(body.from).toEqual(20000); expect(body.size).toEqual(10000); }); @@ -106,30 +108,27 @@ describe('Get Logstash Stats', () => { describe('fetchLogstashStats', () => { it('should set `from: 0, to: 10000` in the query', async () => { await fetchLogstashStats(callCluster, clusterUuids, {} as any); - const { args } = callCluster.firstCall; - const [api, { body }] = args; + const { args } = searchMock.firstCall; + const [{ body }] = args; - expect(api).toEqual('search'); expect(body.from).toEqual(0); expect(body.size).toEqual(10000); }); it('should set `from: 10000, to: 10000` in the query', async () => { await fetchLogstashStats(callCluster, clusterUuids, { page: 1 } as any); - const { args } = callCluster.firstCall; - const [api, { body }] = args; + const { args } = searchMock.firstCall; + const [{ body }] = args; - expect(api).toEqual('search'); expect(body.from).toEqual(10000); expect(body.size).toEqual(10000); }); it('should set `from: 20000, to: 10000` in the query', async () => { await fetchLogstashStats(callCluster, clusterUuids, { page: 2 } as any); - const { args } = callCluster.firstCall; - const [api, { body }] = args; + const { args } = searchMock.firstCall; + const [{ body }] = args; - expect(api).toEqual('search'); expect(body.from).toEqual(20000); expect(body.size).toEqual(10000); }); diff --git a/x-pack/plugins/monitoring/server/telemetry_collection/get_logstash_stats.ts b/x-pack/plugins/monitoring/server/telemetry_collection/get_logstash_stats.ts index fafb52d6862b55..ac3f5fceb47ade 100644 --- a/x-pack/plugins/monitoring/server/telemetry_collection/get_logstash_stats.ts +++ b/x-pack/plugins/monitoring/server/telemetry_collection/get_logstash_stats.ts @@ -6,7 +6,8 @@ */ import { SearchResponse } from 'elasticsearch'; -import { LegacyAPICaller } from 'kibana/server'; +import { ElasticsearchClient } from 'kibana/server'; +import { estypes } from '@elastic/elasticsearch'; import { createQuery } from './create_query'; import { mapToList } from './get_high_level_stats'; import { incrementByKey } from './get_high_level_stats'; @@ -261,17 +262,14 @@ export function processLogstashStateResults( } export async function fetchLogstashStats( - callCluster: LegacyAPICaller, + callCluster: ElasticsearchClient, clusterUuids: string[], { page = 0, ...options }: { page?: number } & LogstashProcessOptions ): Promise { - const params = { - headers: { - 'X-QUERY-SOURCE': TELEMETRY_QUERY_SOURCE, - }, + const params: estypes.SearchRequest = { index: INDEX_PATTERN_LOGSTASH, - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ 'hits.hits._source.cluster_uuid', 'hits.hits._source.type', 'hits.hits._source.source_node', @@ -295,7 +293,7 @@ export async function fetchLogstashStats( }, }, ], - }), + }) as estypes.QueryDslQueryContainer, from: page * HITS_SIZE, collapse: { field: 'logstash_stats.logstash.uuid' }, sort: [{ ['logstash_stats.timestamp']: { order: 'desc', unmapped_type: 'long' } }], @@ -303,12 +301,16 @@ export async function fetchLogstashStats( }, }; - const results = await callCluster>('search', params); + const { body: results } = await callCluster.search(params, { + headers: { + 'X-QUERY-SOURCE': TELEMETRY_QUERY_SOURCE, + }, + }); const hitsLength = results?.hits?.hits.length || 0; if (hitsLength > 0) { // further augment the clusters object with more stats - processStatsResults(results, options); + processStatsResults(results as SearchResponse, options); if (hitsLength === HITS_SIZE) { // call recursively @@ -325,18 +327,15 @@ export async function fetchLogstashStats( } export async function fetchLogstashState( - callCluster: LegacyAPICaller, + callCluster: ElasticsearchClient, clusterUuid: string, ephemeralIds: string[], { page = 0, ...options }: { page?: number } & LogstashProcessOptions ): Promise { - const params = { - headers: { - 'X-QUERY-SOURCE': TELEMETRY_QUERY_SOURCE, - }, + const params: estypes.SearchRequest = { index: INDEX_PATTERN_LOGSTASH, - ignoreUnavailable: true, - filterPath: [ + ignore_unavailable: true, + filter_path: [ 'hits.hits._source.logstash_state.pipeline.batch_size', 'hits.hits._source.logstash_state.pipeline.workers', 'hits.hits._source.logstash_state.pipeline.representation.graph.vertices.config_name', @@ -355,7 +354,7 @@ export async function fetchLogstashState( }, }, ], - }), + }) as estypes.QueryDslQueryContainer, from: page * HITS_SIZE, collapse: { field: 'logstash_state.pipeline.ephemeral_id' }, sort: [{ ['timestamp']: { order: 'desc', unmapped_type: 'long' } }], @@ -363,11 +362,16 @@ export async function fetchLogstashState( }, }; - const results = await callCluster>('search', params); + const { body: results } = await callCluster.search>(params, { + headers: { + 'X-QUERY-SOURCE': TELEMETRY_QUERY_SOURCE, + }, + }); + const hitsLength = results?.hits?.hits.length || 0; if (hitsLength > 0) { // further augment the clusters object with more stats - processLogstashStateResults(results, clusterUuid, options); + processLogstashStateResults(results as SearchResponse, clusterUuid, options); if (hitsLength === HITS_SIZE) { // call recursively @@ -392,7 +396,7 @@ export interface LogstashStatsByClusterUuid { * @return {Object} - Logstash stats in an object keyed by the cluster UUIDs */ export async function getLogstashStats( - callCluster: LegacyAPICaller, + callCluster: ElasticsearchClient, clusterUuids: string[] ): Promise { const options: LogstashProcessOptions = { diff --git a/x-pack/plugins/monitoring/server/telemetry_collection/register_monitoring_telemetry_collection.ts b/x-pack/plugins/monitoring/server/telemetry_collection/register_monitoring_telemetry_collection.ts index 23c61393899170..bb5d379f3c80c4 100644 --- a/x-pack/plugins/monitoring/server/telemetry_collection/register_monitoring_telemetry_collection.ts +++ b/x-pack/plugins/monitoring/server/telemetry_collection/register_monitoring_telemetry_collection.ts @@ -5,7 +5,7 @@ * 2.0. */ -import type { ILegacyClusterClient } from 'kibana/server'; +import type { IClusterClient } from 'kibana/server'; import type { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import type { UsageStatsPayload } from '../../../../../src/plugins/telemetry_collection_manager/server'; import type { LogstashBaseStats } from './get_logstash_stats'; @@ -31,7 +31,7 @@ interface MonitoringTelemetryUsage { export function registerMonitoringTelemetryCollection( usageCollection: UsageCollectionSetup, - legacyEsClient: ILegacyClusterClient, + getClient: () => IClusterClient, maxBucketSize: number ) { const monitoringStatsCollector = usageCollection.makeStatsCollector< @@ -137,15 +137,13 @@ export function registerMonitoringTelemetryCollection( }, }, }, - fetch: async ({ kibanaRequest }) => { + fetch: async ({ kibanaRequest, esClient }) => { const timestamp = Date.now(); // Collect the telemetry from the monitoring indices for this moment. // NOTE: Usually, the monitoring indices index stats for each product every 10s (by default). // However, some data may be delayed up-to 24h because monitoring only collects extended Kibana stats in that interval // to avoid overloading of the system when retrieving data from the collectors (that delay is dealt with in the Kibana Stats getter inside the `getAllStats` method). // By 8.x, we expect to stop collecting the Kibana extended stats and keep only the monitoring-related metrics. - const callCluster = kibanaRequest - ? legacyEsClient.asScoped(kibanaRequest).callAsCurrentUser - : legacyEsClient.callAsInternalUser; + const callCluster = kibanaRequest ? esClient : getClient().asInternalUser; const clusterDetails = await getClusterUuids(callCluster, timestamp, maxBucketSize); const [licenses, stats] = await Promise.all([ getLicenses(clusterDetails, callCluster, maxBucketSize), diff --git a/x-pack/plugins/monitoring/server/types.ts b/x-pack/plugins/monitoring/server/types.ts index 3dcf6862b72325..c4a0687bef497b 100644 --- a/x-pack/plugins/monitoring/server/types.ts +++ b/x-pack/plugins/monitoring/server/types.ts @@ -8,9 +8,8 @@ import { Observable } from 'rxjs'; import type { IRouter, - ILegacyClusterClient, Logger, - ILegacyCustomClusterClient, + ICustomClusterClient, RequestHandlerContext, ElasticsearchClient, } from 'kibana/server'; @@ -71,7 +70,7 @@ export interface MonitoringCoreConfig { } export interface RouteDependencies { - cluster: ILegacyCustomClusterClient; + cluster: ICustomClusterClient; router: IRouter; licenseService: MonitoringLicenseService; encryptedSavedObjects?: EncryptedSavedObjectsPluginSetup; @@ -87,7 +86,7 @@ export interface MonitoringCore { export interface LegacyShimDependencies { router: IRouter; instanceUuid: string; - esDataClient: ILegacyClusterClient; + esDataClient: ElasticsearchClient; kibanaStatsCollector: any; }