Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Monitoring] Migrated legacy Elasticsearch client for 8.0 #101850

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions x-pack/plugins/licensing/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { Observable } from 'rxjs';
import type { ILegacyClusterClient, IRouter, RequestHandlerContext } from 'src/core/server';
import type { IClusterClient, IRouter, RequestHandlerContext } from 'src/core/server';
import { ILicense, LicenseStatus, LicenseType } from '../common/types';
import { FeatureUsageServiceSetup, FeatureUsageServiceStart } from './services';

Expand Down Expand Up @@ -82,7 +82,7 @@ export interface LicensingPluginSetup {
* @deprecated in favour of the counterpart provided from start contract
*/
createLicensePoller: (
clusterClient: ILegacyClusterClient,
clusterClient: IClusterClient,
pollingFrequency: number
) => { license$: Observable<ILicense>; refresh(): Promise<ILicense> };
/**
Expand All @@ -107,7 +107,7 @@ export interface LicensingPluginStart {
* given polling frequency.
*/
createLicensePoller: (
clusterClient: ILegacyClusterClient,
clusterClient: IClusterClient,
pollingFrequency: number
) => { license$: Observable<ILicense>; refresh(): Promise<ILicense> };
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -25,8 +25,8 @@ export function instantiateClient(
log: Logger,
createClient: (
type: string,
clientConfig?: Partial<ESClusterConfig>
) => ILegacyCustomClusterClient
clientConfig?: Partial<ElasticsearchClientConfig> | undefined
) => ICustomClusterClient
) {
const isMonitoringCluster = hasMonitoringCluster(elasticsearchConfig);
const cluster = createClient('monitoring', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

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 { getStackProductsUsage } from './lib/get_stack_products_usage';
Expand All @@ -19,7 +19,7 @@ import { fetchClustersLegacy } from '../../lib/alerts/fetch_clusters';
export function getMonitoringUsageCollector(
usageCollection: UsageCollectionSetup,
config: MonitoringConfig,
legacyEsClient: ILegacyClusterClient
legacyEsClient: IClusterClient
simianhacker marked this conversation as resolved.
Show resolved Hide resolved
) {
return usageCollection.makeUsageCollector<MonitoringUsage, true>({
type: 'monitoring',
Expand Down Expand Up @@ -103,8 +103,8 @@ export function getMonitoringUsageCollector(
},
fetch: async ({ kibanaRequest }) => {
const callCluster = kibanaRequest
? legacyEsClient.asScoped(kibanaRequest).callAsCurrentUser
: legacyEsClient.callAsInternalUser;
? legacyEsClient.asScoped(kibanaRequest).asCurrentUser
: legacyEsClient.asInternalUser;
const usageClusters: MonitoringClusterStackProductUsage[] = [];
const availableCcs = config.ui.ccs.enabled ? await fetchAvailableCcsLegacy(callCluster) : [];
const elasticsearchIndex = getCcsIndexPattern(INDEX_PATTERN_ELASTICSEARCH, availableCcs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -16,7 +16,7 @@ export { KibanaSettingsCollector, getKibanaSettings } from './get_settings_colle
export function registerCollectors(
usageCollection: UsageCollectionSetup,
config: MonitoringConfig,
legacyEsClient: ILegacyClusterClient
legacyEsClient: IClusterClient
) {
usageCollection.registerCollector(getSettingsCollector(usageCollection, config));
usageCollection.registerCollector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -41,16 +26,15 @@ interface ClusterStats {
}

export async function fetchESUsage(
config: MonitoringConfig,
callCluster: LegacyAPICaller,
callCluster: ElasticsearchClient,
clusterUuid: string,
index: string
): Promise<StackProductUsage> {
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',
],
Expand Down Expand Up @@ -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<ClusterStats>;
if (esResponse.hits.hits.length === 0) {
return {
count: 0,
Expand All @@ -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-')));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@
*/

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,
callCluster: ElasticsearchClient,
availableCcs: string[],
clusterUuid: string
) {
let index = INDEX_PATTERN_ELASTICSEARCH;
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: [
Expand Down Expand Up @@ -54,6 +55,6 @@ export async function fetchLicenseType(
},
},
};
const response = await callCluster('search', params);
const { body: response } = await callCluster.search(params);
return get(response, 'hits.hits[0]._source.license.type', null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -33,19 +34,19 @@ interface KeyBucket {

export async function fetchStackProductUsage(
config: MonitoringConfig,
callCluster: LegacyAPICaller,
callCluster: ElasticsearchClient,
clusterUuid: string,
index: string,
type: string,
uuidPath: string,
filters: any[] = []
): Promise<StackProductUsage> {
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: {
Expand Down Expand Up @@ -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<ESResponse>;
const uuidBuckets = get(response, 'aggregations.uuids.buckets', []) as UuidBucket[];
const count = uuidBuckets.length;
const metricbeatUsed = Boolean(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand All @@ -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}`
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function fetchCCRReadExceptions(
): Promise<CCRReadExceptionsStats[]> {
const params = {
index,
filterPath: ['aggregations.remote_clusters.buckets'],
filter_path: ['aggregations.remote_clusters.buckets'],
body: {
size: 0,
query: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function fetchClusterHealth(
): Promise<AlertClusterHealth[]> {
const params = {
index,
filterPath: [
filter_path: [
'hits.hits._source.cluster_state.status',
'hits.hits._source.cluster_uuid',
'hits.hits._index',
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/monitoring/server/lib/alerts/fetch_clusters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function fetchClusters(
): Promise<AlertCluster[]> {
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',
Expand Down Expand Up @@ -70,7 +70,7 @@ export async function fetchClustersLegacy(
): Promise<AlertCluster[]> {
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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function fetchElasticsearchVersions(
): Promise<AlertVersions[]> {
const params = {
index,
filterPath: [
filter_path: [
'hits.hits._source.cluster_stats.nodes.versions',
'hits.hits._index',
'hits.hits._source.cluster_uuid',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function fetchIndexShardSize(
): Promise<IndexShardSizeStats[]> {
const params = {
index,
filterPath: ['aggregations.clusters.buckets'],
filter_path: ['aggregations.clusters.buckets'],
body: {
size: 0,
query: {
Expand Down
Loading