diff --git a/src/plugins/kibana_usage_collection/server/collectors/application_usage/telemetry_application_usage_collector.test.ts b/src/plugins/kibana_usage_collection/server/collectors/application_usage/telemetry_application_usage_collector.test.ts index 6cb104416ef58..47dc26e0ab3d8 100644 --- a/src/plugins/kibana_usage_collection/server/collectors/application_usage/telemetry_application_usage_collector.test.ts +++ b/src/plugins/kibana_usage_collection/server/collectors/application_usage/telemetry_application_usage_collector.test.ts @@ -40,7 +40,7 @@ describe('telemetry_application_usage', () => { const logger = loggingSystemMock.createLogger(); - let collector: Collector; + let collector: Collector; const usageCollectionMock = createUsageCollectionSetupMock(); usageCollectionMock.makeUsageCollector.mockImplementation((config) => { diff --git a/src/plugins/kibana_usage_collection/server/collectors/core/core_usage_collector.ts b/src/plugins/kibana_usage_collection/server/collectors/core/core_usage_collector.ts index 297baf016e9e6..3fd011b0bded2 100644 --- a/src/plugins/kibana_usage_collection/server/collectors/core/core_usage_collector.ts +++ b/src/plugins/kibana_usage_collection/server/collectors/core/core_usage_collector.ts @@ -24,7 +24,7 @@ export function getCoreUsageCollector( usageCollection: UsageCollectionSetup, getCoreUsageDataService: () => CoreUsageDataStart ) { - return usageCollection.makeUsageCollector({ + return usageCollection.makeUsageCollector({ type: 'core', isReady: () => typeof getCoreUsageDataService() !== 'undefined', schema: { diff --git a/src/plugins/kibana_usage_collection/server/collectors/core/index.test.ts b/src/plugins/kibana_usage_collection/server/collectors/core/index.test.ts index e31437a744e29..335d73fb60847 100644 --- a/src/plugins/kibana_usage_collection/server/collectors/core/index.test.ts +++ b/src/plugins/kibana_usage_collection/server/collectors/core/index.test.ts @@ -29,7 +29,7 @@ import { CoreUsageData } from 'src/core/server/'; const logger = loggingSystemMock.createLogger(); describe('telemetry_core', () => { - let collector: Collector; + let collector: Collector; const usageCollectionMock = createUsageCollectionSetupMock(); usageCollectionMock.makeUsageCollector.mockImplementation((config) => { diff --git a/src/plugins/kibana_usage_collection/server/collectors/kibana/index.test.ts b/src/plugins/kibana_usage_collection/server/collectors/kibana/index.test.ts index 16a60eca60f47..83cac1d456a3a 100644 --- a/src/plugins/kibana_usage_collection/server/collectors/kibana/index.test.ts +++ b/src/plugins/kibana_usage_collection/server/collectors/kibana/index.test.ts @@ -31,7 +31,7 @@ import { registerKibanaUsageCollector } from './'; const logger = loggingSystemMock.createLogger(); describe('telemetry_kibana', () => { - let collector: Collector; + let collector: Collector; const usageCollectionMock = createUsageCollectionSetupMock(); usageCollectionMock.makeUsageCollector.mockImplementation((config) => { @@ -66,23 +66,4 @@ describe('telemetry_kibana', () => { timelion_sheet: { total: 0 }, }); }); - - test('formatForBulkUpload', async () => { - const resultFromFetch = { - index: '.kibana-tests', - dashboard: { total: 0 }, - visualization: { total: 0 }, - search: { total: 0 }, - index_pattern: { total: 0 }, - graph_workspace: { total: 0 }, - timelion_sheet: { total: 0 }, - }; - - expect(collector.formatForBulkUpload!(resultFromFetch)).toStrictEqual({ - type: 'kibana_stats', - payload: { - usage: resultFromFetch, - }, - }); - }); }); diff --git a/src/plugins/kibana_usage_collection/server/collectors/kibana/kibana_usage_collector.ts b/src/plugins/kibana_usage_collection/server/collectors/kibana/kibana_usage_collector.ts index d292b2d5ace0e..6c2e0a2c926ad 100644 --- a/src/plugins/kibana_usage_collection/server/collectors/kibana/kibana_usage_collector.ts +++ b/src/plugins/kibana_usage_collection/server/collectors/kibana/kibana_usage_collector.ts @@ -21,7 +21,6 @@ import { Observable } from 'rxjs'; import { take } from 'rxjs/operators'; import { SharedGlobalConfig } from 'kibana/server'; import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; -import { KIBANA_STATS_TYPE } from '../../../common/constants'; import { getSavedObjectsCounts, KibanaSavedObjectCounts } from './get_saved_object_counts'; interface KibanaUsage extends KibanaSavedObjectCounts { @@ -32,7 +31,7 @@ export function getKibanaUsageCollector( usageCollection: UsageCollectionSetup, legacyConfig$: Observable ) { - return usageCollection.makeUsageCollector({ + return usageCollection.makeUsageCollector({ type: 'kibana', isReady: () => true, schema: { @@ -53,20 +52,6 @@ export function getKibanaUsageCollector( ...(await getSavedObjectsCounts(callCluster, index)), }; }, - - /* - * Format the response data into a model for internal upload - * 1. Make this data part of the "kibana_stats" type - * 2. Organize the payload in the usage namespace of the data payload (usage.index, etc) - */ - formatForBulkUpload: (result) => { - return { - type: KIBANA_STATS_TYPE, - payload: { - usage: result, - }, - }; - }, }); } diff --git a/src/plugins/kibana_usage_collection/server/collectors/management/index.test.ts b/src/plugins/kibana_usage_collection/server/collectors/management/index.test.ts index 0aafee01cf49d..bd63c5b40735a 100644 --- a/src/plugins/kibana_usage_collection/server/collectors/management/index.test.ts +++ b/src/plugins/kibana_usage_collection/server/collectors/management/index.test.ts @@ -29,7 +29,7 @@ import { registerManagementUsageCollector } from './'; const logger = loggingSystemMock.createLogger(); describe('telemetry_application_usage_collector', () => { - let collector: Collector; + let collector: Collector; const usageCollectionMock = createUsageCollectionSetupMock(); usageCollectionMock.makeUsageCollector.mockImplementation((config) => { diff --git a/src/plugins/kibana_usage_collection/server/collectors/ops_stats/index.test.ts b/src/plugins/kibana_usage_collection/server/collectors/ops_stats/index.test.ts index 8db7010e64026..1aa60e2c8e961 100644 --- a/src/plugins/kibana_usage_collection/server/collectors/ops_stats/index.test.ts +++ b/src/plugins/kibana_usage_collection/server/collectors/ops_stats/index.test.ts @@ -31,7 +31,7 @@ import { loggingSystemMock } from '../../../../../core/server/mocks'; const logger = loggingSystemMock.createLogger(); describe('telemetry_ops_stats', () => { - let collector: Collector; + let collector: Collector; const usageCollectionMock = createUsageCollectionSetupMock(); usageCollectionMock.makeStatsCollector.mockImplementation((config) => { diff --git a/src/plugins/kibana_usage_collection/server/collectors/ui_metric/index.test.ts b/src/plugins/kibana_usage_collection/server/collectors/ui_metric/index.test.ts index 90e3b7110e1dc..d852e505027fa 100644 --- a/src/plugins/kibana_usage_collection/server/collectors/ui_metric/index.test.ts +++ b/src/plugins/kibana_usage_collection/server/collectors/ui_metric/index.test.ts @@ -29,7 +29,7 @@ import { registerUiMetricUsageCollector } from './'; const logger = loggingSystemMock.createLogger(); describe('telemetry_ui_metric', () => { - let collector: Collector; + let collector: Collector; const usageCollectionMock = createUsageCollectionSetupMock(); usageCollectionMock.makeUsageCollector.mockImplementation((config) => { diff --git a/src/plugins/usage_collection/server/collector/collector.test.ts b/src/plugins/usage_collection/server/collector/collector.test.ts index 875414fbeec48..fe52b9b2ca03e 100644 --- a/src/plugins/usage_collection/server/collector/collector.test.ts +++ b/src/plugins/usage_collection/server/collector/collector.test.ts @@ -19,7 +19,6 @@ import { loggingSystemMock } from '../../../../core/server/mocks'; import { Collector } from './collector'; -import { UsageCollector } from './usage_collector'; const logger = loggingSystemMock.createLogger(); @@ -88,49 +87,6 @@ describe('collector', () => { }); }); - describe('formatForBulkUpload', () => { - it('should use the default formatter', () => { - const fetchOutput = { testPass: 100 }; - const collector = new Collector(logger, { - type: 'my_test_collector', - isReady: () => false, - fetch: () => fetchOutput, - }); - expect(collector.formatForBulkUpload(fetchOutput)).toStrictEqual({ - type: 'my_test_collector', - payload: fetchOutput, - }); - }); - - it('should use a custom formatter', () => { - const fetchOutput = { testPass: 100 }; - const collector = new Collector(logger, { - type: 'my_test_collector', - isReady: () => false, - fetch: () => fetchOutput, - formatForBulkUpload: (a) => ({ type: 'other_value', payload: { nested: a } }), - }); - expect(collector.formatForBulkUpload(fetchOutput)).toStrictEqual({ - type: 'other_value', - payload: { nested: fetchOutput }, - }); - }); - - it("should use UsageCollector's default formatter", () => { - const fetchOutput = { testPass: 100 }; - const collector = new UsageCollector(logger, { - type: 'my_test_collector', - isReady: () => false, - fetch: () => fetchOutput, - schema: { testPass: { type: 'long' } }, - }); - expect(collector.formatForBulkUpload(fetchOutput)).toStrictEqual({ - type: 'kibana_stats', - payload: { usage: { my_test_collector: fetchOutput } }, - }); - }); - }); - describe('schema TS validations', () => { // These tests below are used to ensure types inference is working as expected. // We don't intend to test any logic as such, just the relation between the types in `fetch` and `schema`. diff --git a/src/plugins/usage_collection/server/collector/collector.ts b/src/plugins/usage_collection/server/collector/collector.ts index 797fdaa06a620..8e86bc3d1cd26 100644 --- a/src/plugins/usage_collection/server/collector/collector.ts +++ b/src/plugins/usage_collection/server/collector/collector.ts @@ -26,8 +26,6 @@ import { KibanaRequest, } from 'src/core/server'; -export type CollectorFormatForBulkUpload = (result: T) => { type: string; payload: U }; - export type AllowedSchemaNumberTypes = 'long' | 'integer' | 'short' | 'byte' | 'double' | 'float'; export type AllowedSchemaTypes = AllowedSchemaNumberTypes | 'keyword' | 'text' | 'boolean' | 'date'; @@ -87,7 +85,7 @@ export type CollectorFetchMethod< TReturn, ExtraOptions extends object = {} > = ( - this: Collector & ExtraOptions, // Specify the context of `this` for this.log and others to become available + this: Collector & ExtraOptions, // Specify the context of `this` for this.log and others to become available context: CollectorFetchContext ) => Promise | TReturn; @@ -108,7 +106,6 @@ export type CollectorOptionsFetchExtendedContext< export type CollectorOptions< TFetchReturn = unknown, - UFormatBulkUploadPayload = TFetchReturn, // TODO: Once we remove bulk_uploader's dependency on usageCollection, we'll be able to remove this type WithKibanaRequest extends boolean = boolean, ExtraOptions extends object = {} > = { @@ -130,13 +127,6 @@ export type CollectorOptions< * @param collectorFetchContext {@link CollectorFetchContext} */ fetch: CollectorFetchMethod; - /** - * A hook for allowing the fetched data payload to be organized into a typed - * data model for internal bulk upload. See defaultFormatterForBulkUpload for - * a generic example. - * @deprecated Used only by the Legacy Monitoring collection (to be removed in 8.0) - */ - formatForBulkUpload?: CollectorFormatForBulkUpload; } & ExtraOptions & (WithKibanaRequest extends true // If enforced to true via Types, the config must be enforced ? { @@ -146,28 +136,16 @@ export type CollectorOptions< extendFetchContext?: CollectorOptionsFetchExtendedContext; }); -export class Collector< - TFetchReturn, - UFormatBulkUploadPayload = TFetchReturn, - ExtraOptions extends object = {} -> { +export class Collector { public readonly extendFetchContext: CollectorOptionsFetchExtendedContext; - public readonly type: CollectorOptions['type']; - public readonly init?: CollectorOptions['init']; + public readonly type: CollectorOptions['type']; + public readonly init?: CollectorOptions['init']; public readonly fetch: CollectorFetchMethod; - public readonly isReady: CollectorOptions['isReady']; - private readonly _formatForBulkUpload?: CollectorFormatForBulkUpload< - TFetchReturn, - UFormatBulkUploadPayload - >; - /* - * @param {Object} logger - logger object - * @param {String} options.type - property name as the key for the data - * @param {Function} options.init (optional) - initialization function - * @param {Function} options.fetch - function to query data - * @param {Function} options.formatForBulkUpload - optional - * @param {Function} options.isReady - method that returns a boolean or Promise of a boolean to indicate the collector is ready to report data - * @param {Function} options.rest - optional other properties + public readonly isReady: CollectorOptions['isReady']; + /** + * @private Constructor of a Collector. It should be called via the CollectorSet factory methods: `makeStatsCollector` and `makeUsageCollector` + * @param log {@link Logger} + * @param collectorDefinition {@link CollectorOptions} */ constructor( public readonly log: Logger, @@ -175,11 +153,10 @@ export class Collector< type, init, fetch, - formatForBulkUpload, isReady, extendFetchContext = {}, ...options - }: CollectorOptions + }: CollectorOptions ) { if (type === undefined) { throw new Error('Collector must be instantiated with a options.type string property'); @@ -200,21 +177,5 @@ export class Collector< this.fetch = fetch; this.isReady = typeof isReady === 'function' ? isReady : () => true; this.extendFetchContext = extendFetchContext; - this._formatForBulkUpload = formatForBulkUpload; - } - - public formatForBulkUpload(result: TFetchReturn) { - if (this._formatForBulkUpload) { - return this._formatForBulkUpload(result); - } else { - return this.defaultFormatterForBulkUpload(result); - } - } - - protected defaultFormatterForBulkUpload(result: TFetchReturn) { - return { - type: this.type, - payload: (result as unknown) as UFormatBulkUploadPayload, - }; } } diff --git a/src/plugins/usage_collection/server/collector/collector_set.test.ts b/src/plugins/usage_collection/server/collector/collector_set.test.ts index fc17ce131430c..90a69043e0635 100644 --- a/src/plugins/usage_collection/server/collector/collector_set.test.ts +++ b/src/plugins/usage_collection/server/collector/collector_set.test.ts @@ -20,7 +20,7 @@ import { noop } from 'lodash'; import { Collector } from './collector'; import { CollectorSet } from './collector_set'; -import { UsageCollector, UsageCollectorOptions } from './usage_collector'; +import { UsageCollector } from './usage_collector'; import { elasticsearchServiceMock, loggingSystemMock, @@ -163,29 +163,6 @@ describe('CollectorSet', () => { }, ]); }); - - it('should infer the types from the implementations of fetch and formatForBulkUpload', async () => { - const collectors = new CollectorSet({ logger }); - collectors.registerCollector( - new Collector(logger, { - type: 'MY_TEST_COLLECTOR', - fetch: () => ({ test: 1 }), - formatForBulkUpload: (result) => ({ - type: 'MY_TEST_COLLECTOR', - payload: { test: result.test * 2 }, - }), - isReady: () => true, - }) - ); - - const result = await collectors.bulkFetch(mockCallCluster, mockEsClient, mockSoClient, req); - expect(result).toStrictEqual([ - { - type: 'MY_TEST_COLLECTOR', - result: { test: 1 }, // It matches the return of `fetch`. `formatForBulkUpload` is used later on - }, - ]); - }); }); describe('toApiFieldNames', () => { @@ -253,29 +230,6 @@ describe('CollectorSet', () => { }); }); - describe('isUsageCollector', () => { - const collectorOptions: UsageCollectorOptions = { - type: 'MY_TEST_COLLECTOR', - fetch: () => ({ test: 1 }), - isReady: () => true, - schema: { test: { type: 'long' } }, - }; - - it('returns true only for UsageCollector instances', () => { - const collectors = new CollectorSet({ logger }); - const usageCollector = new UsageCollector(logger, collectorOptions); - const collector = new Collector(logger, collectorOptions); - const randomClass = new (class Random {})(); - expect(collectors.isUsageCollector(usageCollector)).toEqual(true); - expect(collectors.isUsageCollector(collector)).toEqual(false); - expect(collectors.isUsageCollector(randomClass)).toEqual(false); - expect(collectors.isUsageCollector({})).toEqual(false); - expect(collectors.isUsageCollector(null)).toEqual(false); - expect(collectors.isUsageCollector('')).toEqual(false); - expect(collectors.isUsageCollector(void 0)).toEqual(false); - }); - }); - describe('makeStatsCollector', () => { const collectorSet = new CollectorSet({ logger }); test('TS should hide kibanaRequest when not opted-in', () => { @@ -402,7 +356,7 @@ describe('CollectorSet', () => { return { test: kibanaRequest ? 1 : 0 }; }, }); - collectorSet.makeUsageCollector<{ test: number }, unknown, false>({ + collectorSet.makeUsageCollector<{ test: number }, false>({ type: 'MY_TEST_COLLECTOR', isReady: () => true, schema: { test: { type: 'long' } }, @@ -415,7 +369,7 @@ describe('CollectorSet', () => { kibanaRequest: false, }, }); - collectorSet.makeUsageCollector<{ test: number }, unknown, false>({ + collectorSet.makeUsageCollector<{ test: number }, false>({ type: 'MY_TEST_COLLECTOR', isReady: () => true, schema: { test: { type: 'long' } }, @@ -442,7 +396,7 @@ describe('CollectorSet', () => { kibanaRequest: true, }, }); - collectorSet.makeUsageCollector<{ test: number }, unknown, false>({ + collectorSet.makeUsageCollector<{ test: number }, false>({ type: 'MY_TEST_COLLECTOR', isReady: () => true, schema: { test: { type: 'long' } }, @@ -460,7 +414,7 @@ describe('CollectorSet', () => { test('TS should allow `true` when types explicitly declare `true` and do not allow `false` or undefined', () => { // false is the default when at least 1 type is specified - collectorSet.makeUsageCollector<{ test: number }, unknown, true>({ + collectorSet.makeUsageCollector<{ test: number }, true>({ type: 'MY_TEST_COLLECTOR', isReady: () => true, schema: { test: { type: 'long' } }, @@ -472,7 +426,7 @@ describe('CollectorSet', () => { kibanaRequest: true, }, }); - collectorSet.makeUsageCollector<{ test: number }, unknown, true>({ + collectorSet.makeUsageCollector<{ test: number }, true>({ type: 'MY_TEST_COLLECTOR', isReady: () => true, schema: { test: { type: 'long' } }, @@ -485,7 +439,7 @@ describe('CollectorSet', () => { kibanaRequest: false, }, }); - collectorSet.makeUsageCollector<{ test: number }, unknown, true>({ + collectorSet.makeUsageCollector<{ test: number }, true>({ type: 'MY_TEST_COLLECTOR', isReady: () => true, schema: { test: { type: 'long' } }, @@ -498,7 +452,7 @@ describe('CollectorSet', () => { kibanaRequest: undefined, }, }); - collectorSet.makeUsageCollector<{ test: number }, unknown, true>({ + collectorSet.makeUsageCollector<{ test: number }, true>({ type: 'MY_TEST_COLLECTOR', isReady: () => true, schema: { test: { type: 'long' } }, @@ -509,7 +463,7 @@ describe('CollectorSet', () => { // @ts-expect-error extendFetchContext: {}, }); - collectorSet.makeUsageCollector<{ test: number }, unknown, true>( + collectorSet.makeUsageCollector<{ test: number }, true>( // @ts-expect-error { type: 'MY_TEST_COLLECTOR', diff --git a/src/plugins/usage_collection/server/collector/collector_set.ts b/src/plugins/usage_collection/server/collector/collector_set.ts index cda4ce36d4e23..3555b05518fdb 100644 --- a/src/plugins/usage_collection/server/collector/collector_set.ts +++ b/src/plugins/usage_collection/server/collector/collector_set.ts @@ -29,8 +29,7 @@ import { import { Collector, CollectorOptions } from './collector'; import { UsageCollector, UsageCollectorOptions } from './usage_collector'; -type AnyCollector = Collector; -type AnyUsageCollector = UsageCollector; +type AnyCollector = Collector; interface CollectorSetConfig { logger: Logger; @@ -38,6 +37,22 @@ interface CollectorSetConfig { collectors?: AnyCollector[]; } +/** + * Public interface of the CollectorSet (makes it easier to mock only the public methods) + */ +export type CollectorSetPublic = Pick< + CollectorSet, + | 'makeStatsCollector' + | 'makeUsageCollector' + | 'registerCollector' + | 'getCollectorByType' + | 'areAllCollectorsReady' + | 'bulkFetch' + | 'bulkFetchUsage' + | 'toObject' + | 'toApiFieldNames' +>; + export class CollectorSet { private _waitingForAllCollectorsTimestamp?: number; private readonly logger: Logger; @@ -55,13 +70,12 @@ export class CollectorSet { */ public makeStatsCollector = < TFetchReturn, - TFormatForBulkUpload, WithKibanaRequest extends boolean, ExtraOptions extends object = {} >( - options: CollectorOptions + options: CollectorOptions ) => { - return new Collector(this.logger, options); + return new Collector(this.logger, options); }; /** @@ -70,32 +84,23 @@ export class CollectorSet { */ public makeUsageCollector = < TFetchReturn, - TFormatForBulkUpload = { usage: { [key: string]: TFetchReturn } }, // TODO: Right now, users will need to explicitly claim `true` for TS to allow `kibanaRequest` usage. // If we improve `telemetry-check-tools` so plugins do not need to specify TFetchReturn, // we'll be able to remove the type defaults and TS will successfully infer the config value as provided in JS. WithKibanaRequest extends boolean = false, ExtraOptions extends object = {} >( - options: UsageCollectorOptions< - TFetchReturn, - TFormatForBulkUpload, - WithKibanaRequest, - ExtraOptions - > + options: UsageCollectorOptions ) => { - return new UsageCollector( - this.logger, - options - ); + return new UsageCollector(this.logger, options); }; /** * Registers a collector to be used when collecting all the usage and stats data * @param collector Collector to be added to the set (previously created via `makeUsageCollector` or `makeStatsCollector`) */ - public registerCollector = ( - collector: Collector + public registerCollector = ( + collector: Collector ) => { // check instanceof if (!(collector instanceof Collector)) { @@ -118,10 +123,6 @@ export class CollectorSet { return [...this.collectors.values()].find((c) => c.type === type); }; - public isUsageCollector = (x: AnyUsageCollector | any): x is AnyUsageCollector => { - return x instanceof UsageCollector; - }; - public areAllCollectorsReady = async (collectorSet: CollectorSet = this) => { if (!(collectorSet instanceof CollectorSet)) { throw new Error( @@ -205,7 +206,7 @@ export class CollectorSet { /* * @return {new CollectorSet} */ - public getFilteredCollectorSet = (filter: (col: AnyCollector) => boolean) => { + private getFilteredCollectorSet = (filter: (col: AnyCollector) => boolean) => { const filtered = [...this.collectors.values()].filter(filter); return this.makeCollectorSetFromArray(filtered); }; @@ -267,16 +268,6 @@ export class CollectorSet { }, {}); }; - // TODO: remove - public map = (mapFn: any) => { - return [...this.collectors.values()].map(mapFn); - }; - - // TODO: remove - public some = (someFn: any) => { - return [...this.collectors.values()].some(someFn); - }; - private makeCollectorSetFromArray = (collectors: AnyCollector[]) => { return new CollectorSet({ logger: this.logger, diff --git a/src/plugins/usage_collection/server/collector/index.ts b/src/plugins/usage_collection/server/collector/index.ts index 2f8be884a8a7b..1bbb995154690 100644 --- a/src/plugins/usage_collection/server/collector/index.ts +++ b/src/plugins/usage_collection/server/collector/index.ts @@ -17,7 +17,7 @@ * under the License. */ -export { CollectorSet } from './collector_set'; +export { CollectorSet, CollectorSetPublic } from './collector_set'; export { Collector, AllowedSchemaTypes, diff --git a/src/plugins/usage_collection/server/collector/usage_collector.ts b/src/plugins/usage_collection/server/collector/usage_collector.ts index a042ea113d5cc..57f6d109d6d20 100644 --- a/src/plugins/usage_collection/server/collector/usage_collector.ts +++ b/src/plugins/usage_collection/server/collector/usage_collector.ts @@ -18,43 +18,24 @@ */ import { Logger } from 'src/core/server'; -import { KIBANA_STATS_TYPE } from '../../common/constants'; import { Collector, CollectorOptions } from './collector'; // Enforce the `schema` property for UsageCollectors export type UsageCollectorOptions< TFetchReturn = unknown, - UFormatBulkUploadPayload = { usage: { [key: string]: TFetchReturn } }, WithKibanaRequest extends boolean = false, ExtraOptions extends object = {} -> = CollectorOptions & - Required, 'schema'>>; +> = CollectorOptions & + Required, 'schema'>>; -export class UsageCollector< +export class UsageCollector extends Collector< TFetchReturn, - UFormatBulkUploadPayload = { usage: { [key: string]: TFetchReturn } }, - ExtraOptions extends object = {} -> extends Collector { + ExtraOptions +> { constructor( - public readonly log: Logger, - collectorOptions: UsageCollectorOptions< - TFetchReturn, - UFormatBulkUploadPayload, - any, - ExtraOptions - > + log: Logger, + collectorOptions: UsageCollectorOptions ) { super(log, collectorOptions); } - - protected defaultFormatterForBulkUpload(result: TFetchReturn) { - return { - type: KIBANA_STATS_TYPE, - payload: ({ - usage: { - [this.type]: result, - }, - } as unknown) as UFormatBulkUploadPayload, - }; - } } diff --git a/src/plugins/usage_collection/server/plugin.ts b/src/plugins/usage_collection/server/plugin.ts index 9a8876446d01e..31e4f0ea253ea 100644 --- a/src/plugins/usage_collection/server/plugin.ts +++ b/src/plugins/usage_collection/server/plugin.ts @@ -27,10 +27,10 @@ import { Plugin, } from 'src/core/server'; import { ConfigType } from './config'; -import { CollectorSet } from './collector'; +import { CollectorSet, CollectorSetPublic } from './collector'; import { setupRoutes } from './routes'; -export type UsageCollectionSetup = CollectorSet; +export type UsageCollectionSetup = CollectorSetPublic; export class UsageCollectionPlugin implements Plugin { private readonly logger: Logger; private savedObjects?: ISavedObjectsRepository; diff --git a/src/plugins/usage_collection/server/usage_collection.mock.ts b/src/plugins/usage_collection/server/usage_collection.mock.ts index 05dae8fa85164..fb0a2e56ff3c9 100644 --- a/src/plugins/usage_collection/server/usage_collection.mock.ts +++ b/src/plugins/usage_collection/server/usage_collection.mock.ts @@ -37,13 +37,6 @@ export const createUsageCollectionSetupMock = () => { bulkFetch: jest.fn(), bulkFetchUsage: jest.fn(), getCollectorByType: jest.fn(), - getFilteredCollectorSet: jest.fn(), - // @ts-ignore jest.fn doesn't play nice with type guards - isUsageCollector: jest.fn(), - makeCollectorSetFromArray: jest.fn(), - map: jest.fn(), - maximumWaitTimeForAllCollectorsInS: 0, - some: jest.fn(), toApiFieldNames: jest.fn(), toObject: jest.fn(), makeStatsCollector: jest.fn().mockImplementation((cfg) => new Collector(logger, cfg)), diff --git a/x-pack/plugins/monitoring/server/kibana_monitoring/__tests__/bulk_uploader.js b/x-pack/plugins/monitoring/server/kibana_monitoring/__tests__/bulk_uploader.js index da12bde966091..1aa9e49bc5f35 100644 --- a/x-pack/plugins/monitoring/server/kibana_monitoring/__tests__/bulk_uploader.js +++ b/x-pack/plugins/monitoring/server/kibana_monitoring/__tests__/bulk_uploader.js @@ -71,7 +71,6 @@ describe('BulkUploader', () => { type: 'type_collector_test', fetch: noop, // empty payloads, isReady: () => true, - formatForBulkUpload: (result) => result, }, ]); @@ -111,13 +110,11 @@ describe('BulkUploader', () => { type: 'type_collector_test', fetch: noop, // empty payloads, isReady: () => false, - formatForBulkUpload: (result) => result, }, { type: 'type_collector_test2', fetch: noop, // empty payloads, isReady: () => true, - formatForBulkUpload: (result) => result, }, ]); @@ -153,7 +150,6 @@ describe('BulkUploader', () => { { fetch: () => ({ type: 'type_collector_test', result: { testData: 12345 } }), isReady: () => true, - formatForBulkUpload: (result) => result, }, ]); const uploader = new BulkUploader({ ...server, interval: FETCH_INTERVAL }); @@ -191,13 +187,11 @@ describe('BulkUploader', () => { { fetch: usageCollectorFetch, isReady: () => true, - formatForBulkUpload: (result) => result, isUsageCollector: true, }, { fetch: collectorFetch, isReady: () => true, - formatForBulkUpload: (result) => result, isUsageCollector: false, }, ]); @@ -223,7 +217,6 @@ describe('BulkUploader', () => { { fetch: usageCollectorFetch, isReady: () => true, - formatForBulkUpload: (result) => result, isUsageCollector: true, }, ]); @@ -253,13 +246,11 @@ describe('BulkUploader', () => { { fetch: statsCollectorFetch, isReady: () => true, - formatForBulkUpload: (result) => result, isUsageCollector: false, }, { fetch: usageCollectorFetch, isReady: () => true, - formatForBulkUpload: (result) => result, isUsageCollector: true, }, ]); @@ -290,13 +281,11 @@ describe('BulkUploader', () => { { fetch: usageCollectorFetch, isReady: () => true, - formatForBulkUpload: (result) => result, isUsageCollector: true, }, { fetch: collectorFetch, isReady: () => true, - formatForBulkUpload: (result) => result, isUsageCollector: false, }, ]); diff --git a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/get_settings_collector.ts b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/get_settings_collector.ts index eaf200f7d5a68..58d0ec861e6ca 100644 --- a/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/get_settings_collector.ts +++ b/x-pack/plugins/monitoring/server/kibana_monitoring/collectors/get_settings_collector.ts @@ -115,7 +115,6 @@ export function getSettingsCollector( ) { return usageCollection.makeStatsCollector< EmailSettingData | undefined, - unknown, false, KibanaSettingsCollectorExtraOptions >({ 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 038042f109817..e5171c4901dd4 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 @@ -20,7 +20,7 @@ export function getMonitoringUsageCollector( config: MonitoringConfig, legacyEsClient: ILegacyClusterClient ) { - return usageCollection.makeUsageCollector({ + return usageCollection.makeUsageCollector({ type: 'monitoring', isReady: () => true, schema: { 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 3bf1d087b973a..57e75d9c9c12e 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 @@ -114,7 +114,6 @@ export function getUsageStats(rawStats: SearchResponse) { } = currUsage; // Stats filtered by telemetry collectors need to be flattened since they're pulled in a generic way. - // A plugin might not provide flat stats if it implements formatForBulkUpload in its collector. // e.g: we want `xpack.reporting` to just be `reporting` const plugins = { ...pluginsTop, ...xpack }; 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 91d6c2374acba..8351c30a4f712 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 @@ -23,7 +23,6 @@ export function registerMonitoringTelemetryCollection( ) { const monitoringStatsCollector = usageCollection.makeStatsCollector< UsageStatsPayload[], - unknown, true, MonitoringCollectorOptions >({ diff --git a/x-pack/plugins/reporting/server/usage/reporting_usage_collector.test.ts b/x-pack/plugins/reporting/server/usage/reporting_usage_collector.test.ts index fff18353c58b0..7cae5e9b6f956 100644 --- a/x-pack/plugins/reporting/server/usage/reporting_usage_collector.test.ts +++ b/x-pack/plugins/reporting/server/usage/reporting_usage_collector.test.ts @@ -463,7 +463,6 @@ describe('Ready for collection observable', () => { expect(args).toMatchInlineSnapshot(` Object { "fetch": [Function], - "formatForBulkUpload": [Function], "isReady": [Function], "schema": Object { "PNG": Object { diff --git a/x-pack/plugins/reporting/server/usage/reporting_usage_collector.ts b/x-pack/plugins/reporting/server/usage/reporting_usage_collector.ts index 2ef7a7995b839..f4209730b68ce 100644 --- a/x-pack/plugins/reporting/server/usage/reporting_usage_collector.ts +++ b/x-pack/plugins/reporting/server/usage/reporting_usage_collector.ts @@ -14,16 +14,6 @@ import { getReportingUsage } from './get_reporting_usage'; import { ReportingUsageType } from './types'; import { reportingSchema } from './schema'; -// places the reporting data as kibana stats -const METATYPE = 'kibana_stats'; - -interface XpackBulkUpload { - usage: { - xpack: { - reporting: ReportingUsageType; - }; - }; -} /* * @return {Object} kibana usage stats type collection object */ @@ -34,7 +24,7 @@ export function getReportingUsageCollector( exportTypesRegistry: ExportTypesRegistry, isReady: () => Promise ) { - return usageCollection.makeUsageCollector({ + return usageCollection.makeUsageCollector({ type: 'reporting', fetch: ({ callCluster }: CollectorFetchContext) => { const config = reporting.getConfig(); @@ -42,23 +32,6 @@ export function getReportingUsageCollector( }, isReady, schema: reportingSchema, - /* - * Format the response data into a model for internal upload - * 1. Make this data part of the "kibana_stats" type - * 2. Organize the payload in the usage.xpack.reporting namespace of the data payload - */ - formatForBulkUpload: (result: ReportingUsageType) => { - return { - type: METATYPE, - payload: { - usage: { - xpack: { - reporting: result, - }, - }, - }, - }; - }, }); } diff --git a/x-pack/plugins/spaces/server/usage_collection/spaces_usage_collector.ts b/x-pack/plugins/spaces/server/usage_collection/spaces_usage_collector.ts index 0e31c930a926b..d563a4a9b100d 100644 --- a/x-pack/plugins/spaces/server/usage_collection/spaces_usage_collector.ts +++ b/x-pack/plugins/spaces/server/usage_collection/spaces_usage_collector.ts @@ -8,7 +8,6 @@ import { LegacyCallAPIOptions } from 'src/core/server'; import { take } from 'rxjs/operators'; import { CollectorFetchContext, UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import { Observable } from 'rxjs'; -import { KIBANA_STATS_TYPE_MONITORING } from '../../../monitoring/common/constants'; import { PluginsSetup } from '../plugin'; type CallCluster = ( @@ -146,11 +145,6 @@ interface CollectorDeps { licensing: PluginsSetup['licensing']; } -interface BulkUpload { - usage: { - spaces: UsageStats; - }; -} /* * @param {Object} server * @return {Object} kibana usage stats type collection object @@ -159,7 +153,7 @@ export function getSpacesUsageCollector( usageCollection: UsageCollectionSetup, deps: CollectorDeps ) { - return usageCollection.makeUsageCollector({ + return usageCollection.makeUsageCollector({ type: 'spaces', isReady: () => true, schema: { @@ -202,22 +196,6 @@ export function getSpacesUsageCollector( ...usageStats, } as UsageStats; }, - - /* - * Format the response data into a model for internal upload - * 1. Make this data part of the "kibana_stats" type - * 2. Organize the payload in the usage.xpack.spaces namespace of the data payload - */ - formatForBulkUpload: (result: UsageStats) => { - return { - type: KIBANA_STATS_TYPE_MONITORING, - payload: { - usage: { - spaces: result, - }, - }, - }; - }, }); }