-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Alerting] Encourage type safe usage of Alerting #86623
Changes from all commits
c4863a4
bfcab2a
561ebb1
5ddcf7a
93f10cb
f4c543a
aa95e0e
dd833fc
1071113
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ export interface ConstructorOptions { | |
|
||
export interface RegistryAlertType | ||
extends Pick< | ||
NormalizedAlertType, | ||
UntypedNormalizedAlertType, | ||
| 'name' | ||
| 'actionGroups' | ||
| 'recoveryActionGroup' | ||
|
@@ -66,16 +66,23 @@ const alertIdSchema = schema.string({ | |
}); | ||
|
||
export type NormalizedAlertType< | ||
Params extends AlertTypeParams = AlertTypeParams, | ||
State extends AlertTypeState = AlertTypeState, | ||
InstanceState extends AlertInstanceState = AlertInstanceState, | ||
InstanceContext extends AlertInstanceContext = AlertInstanceContext | ||
Params extends AlertTypeParams, | ||
State extends AlertTypeState, | ||
InstanceState extends AlertInstanceState, | ||
InstanceContext extends AlertInstanceContext | ||
> = Omit<AlertType<Params, State, InstanceState, InstanceContext>, 'recoveryActionGroup'> & | ||
Pick<Required<AlertType<Params, State, InstanceState, InstanceContext>>, 'recoveryActionGroup'>; | ||
|
||
export type UntypedNormalizedAlertType = NormalizedAlertType< | ||
AlertTypeParams, | ||
AlertTypeState, | ||
AlertInstanceState, | ||
AlertInstanceContext | ||
>; | ||
|
||
Comment on lines
+76
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By |
||
export class AlertTypeRegistry { | ||
private readonly taskManager: TaskManagerSetupContract; | ||
private readonly alertTypes: Map<string, NormalizedAlertType> = new Map(); | ||
private readonly alertTypes: Map<string, UntypedNormalizedAlertType> = new Map(); | ||
private readonly taskRunnerFactory: TaskRunnerFactory; | ||
private readonly licenseState: ILicenseState; | ||
private readonly licensing: LicensingPluginSetup; | ||
|
@@ -96,10 +103,10 @@ export class AlertTypeRegistry { | |
} | ||
|
||
public register< | ||
Params extends AlertTypeParams = AlertTypeParams, | ||
State extends AlertTypeState = AlertTypeState, | ||
InstanceState extends AlertInstanceState = AlertInstanceState, | ||
InstanceContext extends AlertInstanceContext = AlertInstanceContext | ||
Params extends AlertTypeParams, | ||
State extends AlertTypeState, | ||
InstanceState extends AlertInstanceState, | ||
InstanceContext extends AlertInstanceContext | ||
Comment on lines
+106
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed defaults so we force the developers to be explicit about the types they expect. |
||
>(alertType: AlertType<Params, State, InstanceState, InstanceContext>) { | ||
if (this.has(alertType.id)) { | ||
throw new Error( | ||
|
@@ -113,14 +120,22 @@ export class AlertTypeRegistry { | |
} | ||
alertType.actionVariables = normalizedActionVariables(alertType.actionVariables); | ||
|
||
const normalizedAlertType = augmentActionGroupsWithReserved(alertType as AlertType); | ||
const normalizedAlertType = augmentActionGroupsWithReserved< | ||
Params, | ||
State, | ||
InstanceState, | ||
InstanceContext | ||
>(alertType); | ||
|
||
this.alertTypes.set(alertIdSchema.validate(alertType.id), normalizedAlertType); | ||
this.alertTypes.set( | ||
alertIdSchema.validate(alertType.id), | ||
normalizedAlertType as UntypedNormalizedAlertType | ||
); | ||
this.taskManager.registerTaskDefinitions({ | ||
[`alerting:${alertType.id}`]: { | ||
title: alertType.name, | ||
createTaskRunner: (context: RunContext) => | ||
this.taskRunnerFactory.create(normalizedAlertType, context), | ||
this.taskRunnerFactory.create(normalizedAlertType as UntypedNormalizedAlertType, context), | ||
}, | ||
}); | ||
// No need to notify usage on basic alert types | ||
|
@@ -170,7 +185,7 @@ export class AlertTypeRegistry { | |
producer, | ||
minimumLicenseRequired, | ||
}, | ||
]: [string, NormalizedAlertType]) => ({ | ||
]: [string, UntypedNormalizedAlertType]) => ({ | ||
id, | ||
name, | ||
actionGroups, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't have mixed types in the
Map
, so we have to string these when we set them, and rely on the user to provide the right type when they get them.