-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Alerting] retry internal OCC calls within alertsClient #77838
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
Changes from 1 commit
d26fa1f
7af6a9d
d6194e1
98e351d
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 |
|---|---|---|
|
|
@@ -45,6 +45,8 @@ import { parseIsoOrRelativeDate } from './lib/iso_or_relative_date'; | |
| import { alertInstanceSummaryFromEventLog } from './lib/alert_instance_summary_from_event_log'; | ||
| import { IEvent } from '../../event_log/server'; | ||
| import { parseDuration } from '../common/parse_duration'; | ||
| import { retryIfConflicts } from './lib/retry_if_conflicts'; | ||
| import { partiallyUpdateAlert } from './saved_objects'; | ||
|
|
||
| export interface RegistryAlertTypeWithAuth extends RegistryAlertType { | ||
| authorizedConsumers: string[]; | ||
|
|
@@ -421,6 +423,14 @@ export class AlertsClient { | |
| } | ||
|
|
||
| public async update({ id, data }: UpdateOptions): Promise<PartialAlert> { | ||
| return await retryIfConflicts( | ||
| this.logger, | ||
| `alertsClient.update('${id}')`, | ||
| async () => await this.updateWithOCC({ id, data }) | ||
| ); | ||
| } | ||
|
|
||
| private async updateWithOCC({ id, data }: UpdateOptions): Promise<PartialAlert> { | ||
|
Contributor
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. Seeing as AlertsClient is already a huge file, is there a way we can add the retry into this without adding an extra method for each operation? 🤔
Member
Author
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. Do we support decorators? I'm not sure inlining these is going to save all that space, is it? My thinking was that once we add versions to these methods, that basically just means to set retries to 0, so will likely be a straight-forward change to each of the sites. The reason I separated these out was to try to keep the actual methods a little cleaner - wrapping like this obviously doesn't scale real well, beyond the first time to you do it (now!). I can go either way on inlining, but I think the current shape is a little more readable than inlined.
Member
Author
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. After conversation with Gidi, we decided to think about some alternatives to this approach, but couldn't come up with anything that would require even more changes to the alerts client. |
||
| let alertSavedObject: SavedObject<RawAlert>; | ||
|
|
||
| try { | ||
|
|
@@ -529,7 +539,15 @@ export class AlertsClient { | |
| }; | ||
| } | ||
|
|
||
| public async updateApiKey({ id }: { id: string }) { | ||
| public async updateApiKey({ id }: { id: string }): Promise<void> { | ||
| return await retryIfConflicts( | ||
| this.logger, | ||
| `alertsClient.updateApiKey('${id}')`, | ||
| async () => await this.updateApiKeyWithOCC({ id }) | ||
| ); | ||
| } | ||
|
|
||
| private async updateApiKeyWithOCC({ id }: { id: string }) { | ||
| let apiKeyToInvalidate: string | null = null; | ||
| let attributes: RawAlert; | ||
| let version: string | undefined; | ||
|
|
@@ -597,7 +615,15 @@ export class AlertsClient { | |
| } | ||
| } | ||
|
|
||
| public async enable({ id }: { id: string }) { | ||
| public async enable({ id }: { id: string }): Promise<void> { | ||
| return await retryIfConflicts( | ||
| this.logger, | ||
| `alertsClient.enable('${id}')`, | ||
| async () => await this.enableWithOCC({ id }) | ||
| ); | ||
| } | ||
|
|
||
| private async enableWithOCC({ id }: { id: string }) { | ||
| let apiKeyToInvalidate: string | null = null; | ||
| let attributes: RawAlert; | ||
| let version: string | undefined; | ||
|
|
@@ -658,7 +684,15 @@ export class AlertsClient { | |
| } | ||
| } | ||
|
|
||
| public async disable({ id }: { id: string }) { | ||
| public async disable({ id }: { id: string }): Promise<void> { | ||
| return await retryIfConflicts( | ||
| this.logger, | ||
| `alertsClient.disable('${id}')`, | ||
| async () => await this.disableWithOCC({ id }) | ||
| ); | ||
| } | ||
|
|
||
| private async disableWithOCC({ id }: { id: string }) { | ||
| let apiKeyToInvalidate: string | null = null; | ||
| let attributes: RawAlert; | ||
| let version: string | undefined; | ||
|
|
@@ -711,8 +745,19 @@ export class AlertsClient { | |
| } | ||
| } | ||
|
|
||
| public async muteAll({ id }: { id: string }) { | ||
| const { attributes } = await this.unsecuredSavedObjectsClient.get<RawAlert>('alert', id); | ||
| public async muteAll({ id }: { id: string }): Promise<void> { | ||
| return await retryIfConflicts( | ||
| this.logger, | ||
| `alertsClient.muteAll('${id}')`, | ||
| async () => await this.muteAllWithOCC({ id }) | ||
| ); | ||
| } | ||
|
|
||
| private async muteAllWithOCC({ id }: { id: string }) { | ||
| const { attributes, version } = await this.unsecuredSavedObjectsClient.get<RawAlert>( | ||
| 'alert', | ||
| id | ||
| ); | ||
| await this.authorization.ensureAuthorized( | ||
| attributes.alertTypeId, | ||
| attributes.consumer, | ||
|
|
@@ -723,19 +768,34 @@ export class AlertsClient { | |
| await this.actionsAuthorization.ensureAuthorized('execute'); | ||
| } | ||
|
|
||
| await this.unsecuredSavedObjectsClient.update( | ||
| 'alert', | ||
| const updateAttributes = this.updateMeta({ | ||
| muteAll: true, | ||
| mutedInstanceIds: [], | ||
| updatedBy: await this.getUserName(), | ||
| }); | ||
| const updateOptions = { version }; | ||
|
|
||
| await partiallyUpdateAlert( | ||
| this.unsecuredSavedObjectsClient, | ||
| id, | ||
| this.updateMeta({ | ||
| muteAll: true, | ||
| mutedInstanceIds: [], | ||
| updatedBy: await this.getUserName(), | ||
| }) | ||
| updateAttributes, | ||
| updateOptions | ||
| ); | ||
| } | ||
|
|
||
| public async unmuteAll({ id }: { id: string }): Promise<void> { | ||
| return await retryIfConflicts( | ||
| this.logger, | ||
| `alertsClient.unmuteAll('${id}')`, | ||
| async () => await this.unmuteAllWithOCC({ id }) | ||
| ); | ||
| } | ||
|
|
||
| public async unmuteAll({ id }: { id: string }) { | ||
| const { attributes } = await this.unsecuredSavedObjectsClient.get<RawAlert>('alert', id); | ||
| private async unmuteAllWithOCC({ id }: { id: string }) { | ||
| const { attributes, version } = await this.unsecuredSavedObjectsClient.get<RawAlert>( | ||
| 'alert', | ||
| id | ||
| ); | ||
| await this.authorization.ensureAuthorized( | ||
| attributes.alertTypeId, | ||
| attributes.consumer, | ||
|
|
@@ -746,18 +806,30 @@ export class AlertsClient { | |
| await this.actionsAuthorization.ensureAuthorized('execute'); | ||
| } | ||
|
|
||
| await this.unsecuredSavedObjectsClient.update( | ||
| 'alert', | ||
| const updateAttributes = this.updateMeta({ | ||
| muteAll: false, | ||
| mutedInstanceIds: [], | ||
| updatedBy: await this.getUserName(), | ||
| }); | ||
| const updateOptions = { version }; | ||
|
|
||
| await partiallyUpdateAlert( | ||
| this.unsecuredSavedObjectsClient, | ||
| id, | ||
| this.updateMeta({ | ||
| muteAll: false, | ||
| mutedInstanceIds: [], | ||
| updatedBy: await this.getUserName(), | ||
| }) | ||
| updateAttributes, | ||
| updateOptions | ||
| ); | ||
| } | ||
|
|
||
| public async muteInstance({ alertId, alertInstanceId }: MuteOptions): Promise<void> { | ||
| return await retryIfConflicts( | ||
| this.logger, | ||
| `alertsClient.muteInstance('${alertId}')`, | ||
| async () => await this.muteInstanceWithOCC({ alertId, alertInstanceId }) | ||
| ); | ||
| } | ||
|
|
||
| public async muteInstance({ alertId, alertInstanceId }: MuteOptions) { | ||
| private async muteInstanceWithOCC({ alertId, alertInstanceId }: MuteOptions) { | ||
| const { attributes, version } = await this.unsecuredSavedObjectsClient.get<Alert>( | ||
| 'alert', | ||
| alertId | ||
|
|
@@ -788,7 +860,15 @@ export class AlertsClient { | |
| } | ||
| } | ||
|
|
||
| public async unmuteInstance({ | ||
| public async unmuteInstance({ alertId, alertInstanceId }: MuteOptions): Promise<void> { | ||
| return await retryIfConflicts( | ||
| this.logger, | ||
| `alertsClient.unmuteInstance('${alertId}')`, | ||
| async () => await this.unmuteInstanceWithOCC({ alertId, alertInstanceId }) | ||
| ); | ||
| } | ||
|
|
||
| private async unmuteInstanceWithOCC({ | ||
| alertId, | ||
| alertInstanceId, | ||
| }: { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.