mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
Migrates spaces usage collector es client from legacy to new (#86900)
This commit is contained in:
parent
2cc2312f6d
commit
f7961998d9
2 changed files with 48 additions and 36 deletions
|
@ -12,7 +12,10 @@ import { ILicense, LicensingPluginSetup } from '../../../licensing/server';
|
|||
import { UsageStats } from '../usage_stats';
|
||||
import { usageStatsClientMock } from '../usage_stats/usage_stats_client.mock';
|
||||
import { usageStatsServiceMock } from '../usage_stats/usage_stats_service.mock';
|
||||
import { pluginInitializerContextConfigMock } from 'src/core/server/mocks';
|
||||
import {
|
||||
elasticsearchServiceMock,
|
||||
pluginInitializerContextConfigMock,
|
||||
} from 'src/core/server/mocks';
|
||||
import { createCollectorFetchContextMock } from 'src/plugins/usage_collection/server/mocks';
|
||||
|
||||
interface SetupOpts {
|
||||
|
@ -74,31 +77,39 @@ function setup({
|
|||
};
|
||||
}
|
||||
|
||||
const defaultCallClusterMock = jest.fn().mockResolvedValue({
|
||||
hits: {
|
||||
total: {
|
||||
value: 2,
|
||||
const defaultEsClientSearchMock = jest.fn().mockResolvedValue({
|
||||
body: {
|
||||
hits: {
|
||||
total: {
|
||||
value: 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
aggregations: {
|
||||
disabledFeatures: {
|
||||
buckets: [
|
||||
{
|
||||
key: 'feature1',
|
||||
doc_count: 1,
|
||||
},
|
||||
],
|
||||
aggregations: {
|
||||
disabledFeatures: {
|
||||
buckets: [
|
||||
{
|
||||
key: 'feature1',
|
||||
doc_count: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const getMockFetchContext = (mockedCallCluster: jest.Mock) => {
|
||||
const getMockFetchContext = (mockedEsClient: any) => {
|
||||
return {
|
||||
...createCollectorFetchContextMock(),
|
||||
callCluster: mockedCallCluster,
|
||||
esClient: mockedEsClient,
|
||||
};
|
||||
};
|
||||
|
||||
const getMockedEsClient = (esClientMock: jest.Mock) => {
|
||||
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
|
||||
esClient.search = esClientMock;
|
||||
return esClient;
|
||||
};
|
||||
|
||||
describe('error handling', () => {
|
||||
it('handles a 404 when searching for space usage', async () => {
|
||||
const { features, licensing, usageCollection, usageStatsService } = setup({
|
||||
|
@ -110,8 +121,10 @@ describe('error handling', () => {
|
|||
licensing,
|
||||
usageStatsServicePromise: Promise.resolve(usageStatsService),
|
||||
});
|
||||
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
|
||||
esClient.search.mockRejectedValue({ status: 404 });
|
||||
|
||||
await collector.fetch(getMockFetchContext(jest.fn().mockRejectedValue({ status: 404 })));
|
||||
await collector.fetch(getMockFetchContext(esClient));
|
||||
});
|
||||
|
||||
it('throws error for a non-404', async () => {
|
||||
|
@ -124,13 +137,13 @@ describe('error handling', () => {
|
|||
licensing,
|
||||
usageStatsServicePromise: Promise.resolve(usageStatsService),
|
||||
});
|
||||
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
|
||||
|
||||
const statusCodes = [401, 402, 403, 500];
|
||||
for (const statusCode of statusCodes) {
|
||||
const error = { status: statusCode };
|
||||
await expect(
|
||||
collector.fetch(getMockFetchContext(jest.fn().mockRejectedValue(error)))
|
||||
).rejects.toBe(error);
|
||||
esClient.search.mockRejectedValue(error);
|
||||
await expect(collector.fetch(getMockFetchContext(esClient))).rejects.toBe(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -148,9 +161,10 @@ describe('with a basic license', () => {
|
|||
licensing,
|
||||
usageStatsServicePromise: Promise.resolve(usageStatsService),
|
||||
});
|
||||
usageData = await collector.fetch(getMockFetchContext(defaultCallClusterMock));
|
||||
const esClient = getMockedEsClient(defaultEsClientSearchMock);
|
||||
usageData = await collector.fetch(getMockFetchContext(esClient));
|
||||
|
||||
expect(defaultCallClusterMock).toHaveBeenCalledWith('search', {
|
||||
expect(defaultEsClientSearchMock).toHaveBeenCalledWith({
|
||||
body: {
|
||||
aggs: {
|
||||
disabledFeatures: {
|
||||
|
@ -206,7 +220,9 @@ describe('with no license', () => {
|
|||
licensing,
|
||||
usageStatsServicePromise: Promise.resolve(usageStatsService),
|
||||
});
|
||||
usageData = await collector.fetch(getMockFetchContext(defaultCallClusterMock));
|
||||
const esClient = getMockedEsClient(defaultEsClientSearchMock);
|
||||
|
||||
usageData = await collector.fetch(getMockFetchContext(esClient));
|
||||
});
|
||||
|
||||
test('sets enabled to false', () => {
|
||||
|
@ -245,7 +261,9 @@ describe('with platinum license', () => {
|
|||
licensing,
|
||||
usageStatsServicePromise: Promise.resolve(usageStatsService),
|
||||
});
|
||||
usageData = await collector.fetch(getMockFetchContext(defaultCallClusterMock));
|
||||
const esClient = getMockedEsClient(defaultEsClientSearchMock);
|
||||
|
||||
usageData = await collector.fetch(getMockFetchContext(esClient));
|
||||
});
|
||||
|
||||
test('sets enabled to true', () => {
|
||||
|
|
|
@ -4,19 +4,13 @@
|
|||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
import { LegacyCallAPIOptions } from 'src/core/server';
|
||||
import { ElasticsearchClient } from 'src/core/server';
|
||||
import { take } from 'rxjs/operators';
|
||||
import { CollectorFetchContext, UsageCollectionSetup } from 'src/plugins/usage_collection/server';
|
||||
import { Observable } from 'rxjs';
|
||||
import { PluginsSetup } from '../plugin';
|
||||
import { UsageStats, UsageStatsServiceSetup } from '../usage_stats';
|
||||
|
||||
type CallCluster = <T = unknown>(
|
||||
endpoint: string,
|
||||
clientParams: Record<string, unknown>,
|
||||
options?: LegacyCallAPIOptions
|
||||
) => Promise<T>;
|
||||
|
||||
interface SpacesAggregationResponse {
|
||||
hits: {
|
||||
total: { value: number };
|
||||
|
@ -37,7 +31,7 @@ interface SpacesAggregationResponse {
|
|||
* @return {UsageData}
|
||||
*/
|
||||
async function getSpacesUsage(
|
||||
callCluster: CallCluster,
|
||||
esClient: ElasticsearchClient,
|
||||
kibanaIndex: string,
|
||||
features: PluginsSetup['features'],
|
||||
spacesAvailable: boolean
|
||||
|
@ -50,7 +44,7 @@ async function getSpacesUsage(
|
|||
|
||||
let resp: SpacesAggregationResponse | undefined;
|
||||
try {
|
||||
resp = await callCluster<SpacesAggregationResponse>('search', {
|
||||
({ body: resp } = await esClient.search({
|
||||
index: kibanaIndex,
|
||||
body: {
|
||||
track_total_hits: true,
|
||||
|
@ -72,7 +66,7 @@ async function getSpacesUsage(
|
|||
},
|
||||
size: 0,
|
||||
},
|
||||
});
|
||||
}));
|
||||
} catch (err) {
|
||||
if (err.status === 404) {
|
||||
return null;
|
||||
|
@ -208,14 +202,14 @@ export function getSpacesUsageCollector(
|
|||
'apiCalls.resolveCopySavedObjectsErrors.createNewCopiesEnabled.yes': { type: 'long' },
|
||||
'apiCalls.resolveCopySavedObjectsErrors.createNewCopiesEnabled.no': { type: 'long' },
|
||||
},
|
||||
fetch: async ({ callCluster }: CollectorFetchContext) => {
|
||||
fetch: async ({ esClient }: CollectorFetchContext) => {
|
||||
const { licensing, kibanaIndexConfig$, features, usageStatsServicePromise } = deps;
|
||||
const license = await licensing.license$.pipe(take(1)).toPromise();
|
||||
const available = license.isAvailable; // some form of spaces is available for all valid licenses
|
||||
|
||||
const kibanaIndex = (await kibanaIndexConfig$.pipe(take(1)).toPromise()).kibana.index;
|
||||
|
||||
const usageData = await getSpacesUsage(callCluster, kibanaIndex, features, available);
|
||||
const usageData = await getSpacesUsage(esClient, kibanaIndex, features, available);
|
||||
const usageStats = await getUsageStats(usageStatsServicePromise, available);
|
||||
|
||||
return {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue