Skip to content

Commit

Permalink
[Monitoring] Migrated legacy Elasticsearch client for 8.0 (#101850)
Browse files Browse the repository at this point in the history
  • Loading branch information
simianhacker committed Jun 30, 2021
1 parent fee7348 commit 25db1df
Show file tree
Hide file tree
Showing 96 changed files with 685 additions and 526 deletions.
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,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',
Expand Down Expand Up @@ -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: {
Expand All @@ -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;

Expand Down Expand Up @@ -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];

Expand All @@ -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 [];
});

Expand All @@ -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 [];
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MonitoringUsage, true>({
type: 'monitoring',
Expand Down Expand Up @@ -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(
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,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)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
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
Loading

0 comments on commit 25db1df

Please sign in to comment.