Skip to content

Commit 3e48359

Browse files
committed
[TS] Allow Collector to be extended via options
1 parent 07a4a6c commit 3e48359

File tree

4 files changed

+86
-45
lines changed

4 files changed

+86
-45
lines changed

src/plugins/usage_collection/server/collector/collector.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,12 @@ export type CollectorFetchContext<WithKibanaRequest extends boolean | undefined
8282
}
8383
: {});
8484

85-
export type CollectorFetchMethod<WithKibanaRequest extends boolean | undefined, TReturn> = (
86-
this: Collector<TReturn, unknown>, // Specify the context of `this` for this.log and others to become available
85+
export type CollectorFetchMethod<
86+
WithKibanaRequest extends boolean | undefined,
87+
TReturn,
88+
ExtraOptions extends object = {}
89+
> = (
90+
this: Collector<TReturn, unknown> & ExtraOptions, // Specify the context of `this` for this.log and others to become available
8791
context: CollectorFetchContext<WithKibanaRequest>
8892
) => Promise<TReturn> | TReturn;
8993

@@ -105,7 +109,8 @@ export type CollectorOptionsFetchExtendedContext<
105109
export type CollectorOptions<
106110
TFetchReturn = unknown,
107111
UFormatBulkUploadPayload = TFetchReturn, // TODO: Once we remove bulk_uploader's dependency on usageCollection, we'll be able to remove this type
108-
WithKibanaRequest extends boolean = boolean
112+
WithKibanaRequest extends boolean = boolean,
113+
ExtraOptions extends object = {}
109114
> = {
110115
/**
111116
* Unique string identifier for the collector
@@ -124,27 +129,32 @@ export type CollectorOptions<
124129
* The method that will collect and return the data in the final format.
125130
* @param collectorFetchContext {@link CollectorFetchContext}
126131
*/
127-
fetch: CollectorFetchMethod<WithKibanaRequest, TFetchReturn>;
132+
fetch: CollectorFetchMethod<WithKibanaRequest, TFetchReturn, ExtraOptions>;
128133
/**
129134
* A hook for allowing the fetched data payload to be organized into a typed
130135
* data model for internal bulk upload. See defaultFormatterForBulkUpload for
131136
* a generic example.
132137
* @deprecated Used only by the Legacy Monitoring collection (to be removed in 8.0)
133138
*/
134139
formatForBulkUpload?: CollectorFormatForBulkUpload<TFetchReturn, UFormatBulkUploadPayload>;
135-
} & (WithKibanaRequest extends true // If enforced to true via Types, the config must be enforced
136-
? {
137-
extendFetchContext: CollectorOptionsFetchExtendedContext<WithKibanaRequest>;
138-
}
139-
: {
140-
extendFetchContext?: CollectorOptionsFetchExtendedContext<WithKibanaRequest>;
141-
});
142-
143-
export class Collector<TFetchReturn, UFormatBulkUploadPayload = TFetchReturn> {
140+
} & ExtraOptions &
141+
(WithKibanaRequest extends true // If enforced to true via Types, the config must be enforced
142+
? {
143+
extendFetchContext: CollectorOptionsFetchExtendedContext<WithKibanaRequest>;
144+
}
145+
: {
146+
extendFetchContext?: CollectorOptionsFetchExtendedContext<WithKibanaRequest>;
147+
});
148+
149+
export class Collector<
150+
TFetchReturn,
151+
UFormatBulkUploadPayload = TFetchReturn,
152+
ExtraOptions extends object = {}
153+
> {
144154
public readonly extendFetchContext: CollectorOptionsFetchExtendedContext<any>;
145155
public readonly type: CollectorOptions<TFetchReturn, UFormatBulkUploadPayload, any>['type'];
146156
public readonly init?: CollectorOptions<TFetchReturn, UFormatBulkUploadPayload, any>['init'];
147-
public readonly fetch: CollectorFetchMethod<any, TFetchReturn>;
157+
public readonly fetch: CollectorFetchMethod<any, TFetchReturn, ExtraOptions>;
148158
public readonly isReady: CollectorOptions<TFetchReturn, UFormatBulkUploadPayload, any>['isReady'];
149159
private readonly _formatForBulkUpload?: CollectorFormatForBulkUpload<
150160
TFetchReturn,
@@ -169,7 +179,7 @@ export class Collector<TFetchReturn, UFormatBulkUploadPayload = TFetchReturn> {
169179
isReady,
170180
extendFetchContext = {},
171181
...options
172-
}: CollectorOptions<TFetchReturn, UFormatBulkUploadPayload, any>
182+
}: CollectorOptions<TFetchReturn, UFormatBulkUploadPayload, any, ExtraOptions>
173183
) {
174184
if (type === undefined) {
175185
throw new Error('Collector must be instantiated with a options.type string property');

src/plugins/usage_collection/server/collector/collector_set.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,37 +49,58 @@ export class CollectorSet {
4949
this.maximumWaitTimeForAllCollectorsInS = maximumWaitTimeForAllCollectorsInS || 60;
5050
}
5151

52+
/**
53+
* Instantiates a stats collector with the definition provided in the options
54+
* @param options Definition of the collector {@link CollectorOptions}
55+
*/
5256
public makeStatsCollector = <
5357
TFetchReturn,
5458
TFormatForBulkUpload,
55-
WithKibanaRequest extends boolean
59+
WithKibanaRequest extends boolean,
60+
ExtraOptions extends object = {}
5661
>(
57-
options: CollectorOptions<TFetchReturn, TFormatForBulkUpload, WithKibanaRequest>
62+
options: CollectorOptions<TFetchReturn, TFormatForBulkUpload, WithKibanaRequest, ExtraOptions>
5863
) => {
59-
return new Collector<TFetchReturn, TFormatForBulkUpload>(this.logger, options);
64+
return new Collector<TFetchReturn, TFormatForBulkUpload, ExtraOptions>(this.logger, options);
6065
};
66+
67+
/**
68+
* Instantiates an usage collector with the definition provided in the options
69+
* @param options Definition of the collector {@link CollectorOptions}
70+
*/
6171
public makeUsageCollector = <
6272
TFetchReturn,
6373
TFormatForBulkUpload = { usage: { [key: string]: TFetchReturn } },
6474
// TODO: Right now, users will need to explicitly claim `true` for TS to allow `kibanaRequest` usage.
6575
// If we improve `telemetry-check-tools` so plugins do not need to specify TFetchReturn,
6676
// we'll be able to remove the type defaults and TS will successfully infer the config value as provided in JS.
67-
WithKibanaRequest extends boolean = false
77+
WithKibanaRequest extends boolean = false,
78+
ExtraOptions extends object = {}
6879
>(
69-
options: UsageCollectorOptions<TFetchReturn, TFormatForBulkUpload, WithKibanaRequest>
80+
options: UsageCollectorOptions<
81+
TFetchReturn,
82+
TFormatForBulkUpload,
83+
WithKibanaRequest,
84+
ExtraOptions
85+
>
7086
) => {
71-
return new UsageCollector<TFetchReturn, TFormatForBulkUpload>(this.logger, options);
87+
return new UsageCollector<TFetchReturn, TFormatForBulkUpload, ExtraOptions>(
88+
this.logger,
89+
options
90+
);
7291
};
7392

74-
/*
75-
* @param collector {Collector} collector object
93+
/**
94+
* Registers a collector to be used when collecting all the usage and stats data
95+
* @param collector Collector to be added to the set (previously created via `makeUsageCollector` or `makeStatsCollector`)
7696
*/
7797
public registerCollector = <
7898
TFetchReturn,
7999
TFormatForBulkUpload,
80-
WithKibanaRequest extends boolean
100+
WithKibanaRequest extends boolean,
101+
ExtraOptions extends object
81102
>(
82-
collector: Collector<TFetchReturn, TFormatForBulkUpload>
103+
collector: Collector<TFetchReturn, TFormatForBulkUpload, ExtraOptions>
83104
) => {
84105
// check instanceof
85106
if (!(collector instanceof Collector)) {

src/plugins/usage_collection/server/collector/usage_collector.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,24 @@ import { Collector, CollectorOptions } from './collector';
2525
export type UsageCollectorOptions<
2626
TFetchReturn = unknown,
2727
UFormatBulkUploadPayload = { usage: { [key: string]: TFetchReturn } },
28-
WithKibanaRequest extends boolean = false
29-
> = CollectorOptions<TFetchReturn, UFormatBulkUploadPayload, WithKibanaRequest> &
28+
WithKibanaRequest extends boolean = false,
29+
ExtraOptions extends object = {}
30+
> = CollectorOptions<TFetchReturn, UFormatBulkUploadPayload, WithKibanaRequest, ExtraOptions> &
3031
Required<Pick<CollectorOptions<TFetchReturn, UFormatBulkUploadPayload, boolean>, 'schema'>>;
3132

3233
export class UsageCollector<
3334
TFetchReturn,
34-
UFormatBulkUploadPayload = { usage: { [key: string]: TFetchReturn } }
35-
> extends Collector<TFetchReturn, UFormatBulkUploadPayload> {
35+
UFormatBulkUploadPayload = { usage: { [key: string]: TFetchReturn } },
36+
ExtraOptions extends object = {}
37+
> extends Collector<TFetchReturn, UFormatBulkUploadPayload, ExtraOptions> {
3638
constructor(
3739
public readonly log: Logger,
38-
collectorOptions: UsageCollectorOptions<TFetchReturn, UFormatBulkUploadPayload, any>
40+
collectorOptions: UsageCollectorOptions<
41+
TFetchReturn,
42+
UFormatBulkUploadPayload,
43+
any,
44+
ExtraOptions
45+
>
3946
) {
4047
super(log, collectorOptions);
4148
}

x-pack/plugins/monitoring/server/kibana_monitoring/collectors/get_settings_collector.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,25 @@ interface EmailSettingData {
4444
xpack: { default_admin_email: string | null };
4545
}
4646

47-
function getEmailValueStructure(email: string | null): EmailSettingData {
48-
return {
49-
xpack: {
50-
default_admin_email: email,
51-
},
52-
};
53-
}
54-
55-
export interface KibanaSettingsCollector extends Collector<EmailSettingData | undefined> {
47+
export interface KibanaSettingsCollectorExtraOptions {
5648
getEmailValueStructure(email: string | null): EmailSettingData;
5749
}
5850

51+
export type KibanaSettingsCollector = Collector<
52+
EmailSettingData | undefined,
53+
unknown,
54+
KibanaSettingsCollectorExtraOptions
55+
>;
56+
5957
export function getSettingsCollector(
6058
usageCollection: UsageCollectionSetup,
6159
config: MonitoringConfig
6260
) {
63-
const collector = usageCollection.makeStatsCollector<
61+
return usageCollection.makeStatsCollector<
6462
EmailSettingData | undefined,
6563
unknown,
66-
false
64+
false,
65+
KibanaSettingsCollectorExtraOptions
6766
>({
6867
type: KIBANA_SETTINGS_TYPE,
6968
isReady: () => true,
@@ -78,7 +77,7 @@ export function getSettingsCollector(
7877

7978
// skip everything if defaultAdminEmail === undefined
8079
if (defaultAdminEmail || (defaultAdminEmail === null && shouldUseNull)) {
81-
kibanaSettingsData = getEmailValueStructure(defaultAdminEmail);
80+
kibanaSettingsData = this.getEmailValueStructure(defaultAdminEmail);
8281
this.log.debug(
8382
`[${defaultAdminEmail}] default admin email setting found, sending [${KIBANA_SETTINGS_TYPE}] monitoring document.`
8483
);
@@ -94,8 +93,12 @@ export function getSettingsCollector(
9493
// returns undefined if there was no result
9594
return kibanaSettingsData;
9695
},
96+
getEmailValueStructure(email: string | null) {
97+
return {
98+
xpack: {
99+
default_admin_email: email,
100+
},
101+
};
102+
},
97103
});
98-
// Attaching the method to the collector because we need to use it in `/api/settings`
99-
(collector as KibanaSettingsCollector).getEmailValueStructure = getEmailValueStructure;
100-
return collector;
101104
}

0 commit comments

Comments
 (0)