Repository navigation
PPL Alerting: Execute Monitor and Monitor Stats #1960
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
f44ee68
PPL Alerting initial commit
toepkerd-zz f78ea2e
removing unused get alerts params and adding serde tests for alertsv2…
toepkerd-zz b892bf5
adding get alerts rbac IT
toepkerd-zz 77361e9
misc changes
toepkerd-zz 610d892
removing actioned upon TODO
toepkerd-zz 154bb85
adding execute monitor request/response serde tests that are currentl…
toepkerd-zz 11a5932
adding request response serde tests and passing in explicit readers a…
toepkerd-zz 7f2ab26
finishing out request/response serde tests
toepkerd-zz ba91386
minor changes in tests
toepkerd-zz 14532ef
adding debug logs for playground
toepkerd-zz 443662f
adding expires null check in alert expiration
toepkerd-zz 228b1b7
removing null check as expires duration should never be null
toepkerd-zz d7b3986
cleanup
toepkerd-zz d149cb0
updating timestamp field check to include any date type that includes…
toepkerd-zz 3c21de5
addressing PR comments
toepkerd-zz df1fefc
misc logs changes
toepkerd-zz 3a58615
changing default max query results to 1000 and replacing get alerts f…
toepkerd-zz 9ad2a2c
putting query index parsing logic in try catch
toepkerd-zz 375bd80
adding backtick trimming when reading index names in ppl query
toepkerd-zz c35a113
adding time measurements and logs
toepkerd-zz 16a0224
refactoring PPL query execution call to use transport service with ti…
toepkerd-zz c280e83
adding monitor run timeout test
toepkerd-zz c8e8cd9
removing return in timeout catch block to allow for monitors to get u…
toepkerd-zz c23a873
addressing pr comments
toepkerd-zz b7586a5
addressing pr comments
toepkerd-zz 474acbb
update monitor now ignores seqNo and primaryTerm, and eliminating mon…
toepkerd-zz ffb271b
adding experimental tags
toepkerd-zz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
alerting/src/main/kotlin/org/opensearch/alerting/MonitorV2Runner.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.alerting | ||
|
|
||
| import org.opensearch.OpenSearchSecurityException | ||
| import org.opensearch.alerting.action.GetDestinationsAction | ||
| import org.opensearch.alerting.action.GetDestinationsRequest | ||
| import org.opensearch.alerting.action.GetDestinationsResponse | ||
| import org.opensearch.alerting.model.destination.Destination | ||
| import org.opensearch.alerting.modelv2.MonitorV2 | ||
| import org.opensearch.alerting.modelv2.MonitorV2RunResult | ||
| import org.opensearch.alerting.opensearchapi.suspendUntil | ||
| import org.opensearch.alerting.util.destinationmigration.NotificationActionConfigs | ||
| import org.opensearch.alerting.util.destinationmigration.NotificationApiUtils.Companion.getNotificationConfigInfo | ||
| import org.opensearch.alerting.util.destinationmigration.getTitle | ||
| import org.opensearch.alerting.util.destinationmigration.publishLegacyNotification | ||
| import org.opensearch.alerting.util.destinationmigration.sendNotification | ||
| import org.opensearch.alerting.util.isAllowed | ||
| import org.opensearch.alerting.util.isTestAction | ||
| import org.opensearch.commons.alerting.model.Table | ||
| import org.opensearch.commons.alerting.model.action.Action | ||
| import org.opensearch.commons.notifications.model.NotificationConfigInfo | ||
| import org.opensearch.transport.TransportService | ||
| import org.opensearch.transport.client.node.NodeClient | ||
| import java.time.Instant | ||
|
|
||
| abstract class MonitorV2Runner { | ||
| abstract suspend fun runMonitorV2( | ||
| monitorV2: MonitorV2, | ||
| monitorCtx: MonitorRunnerExecutionContext, // MonitorV2 reads from same context as Monitor does | ||
| periodEnd: Instant, | ||
| dryRun: Boolean, | ||
| manual: Boolean, | ||
| executionId: String, | ||
| transportService: TransportService | ||
| ): MonitorV2RunResult<*> | ||
|
|
||
| protected suspend fun getConfigAndSendNotification( | ||
|
AWSHurneyt marked this conversation as resolved.
Resolved
|
||
| action: Action, | ||
| monitorCtx: MonitorRunnerExecutionContext, | ||
| subject: String?, | ||
| message: String | ||
| ): String { | ||
| val config = getConfigForNotificationAction(action, monitorCtx) | ||
| if (config.destination == null && config.channel == null) { | ||
| throw IllegalStateException("Unable to find a Notification Channel or Destination config with id [${action.destinationId}]") | ||
| } | ||
|
|
||
| // Adding a check on TEST_ACTION Destination type here to avoid supporting it as a LegacyBaseMessage type | ||
| // just for Alerting integration tests | ||
| if (config.destination?.isTestAction() == true) { | ||
| return "test action" | ||
| } | ||
|
|
||
| if (config.destination?.isAllowed(monitorCtx.allowList) == false) { | ||
| throw IllegalStateException( | ||
| "Monitor contains a Destination type that is not allowed: ${config.destination.type}" | ||
| ) | ||
| } | ||
|
|
||
| var actionResponseContent = "" | ||
| actionResponseContent = config.channel | ||
| ?.sendNotification( | ||
| monitorCtx.client!!, | ||
| config.channel.getTitle(subject), | ||
| message | ||
| ) ?: actionResponseContent | ||
|
|
||
| actionResponseContent = config.destination | ||
| ?.buildLegacyBaseMessage(subject, message, monitorCtx.destinationContextFactory!!.getDestinationContext(config.destination)) | ||
| ?.publishLegacyNotification(monitorCtx.client!!) | ||
| ?: actionResponseContent | ||
|
|
||
| return actionResponseContent | ||
| } | ||
|
|
||
| /** | ||
| * The "destination" ID referenced in a Monitor Action could either be a Notification config or a Destination config | ||
| * depending on whether the background migration process has already migrated it from a Destination to a Notification config. | ||
| * | ||
| * To cover both of these cases, the Notification config will take precedence and if it is not found, the Destination will be retrieved. | ||
| */ | ||
| private suspend fun getConfigForNotificationAction( | ||
| action: Action, | ||
| monitorCtx: MonitorRunnerExecutionContext | ||
| ): NotificationActionConfigs { | ||
| var destination: Destination? = null | ||
| var notificationPermissionException: Exception? = null | ||
|
|
||
| var channel: NotificationConfigInfo? = null | ||
| try { | ||
| channel = getNotificationConfigInfo(monitorCtx.client as NodeClient, action.destinationId) | ||
| } catch (e: OpenSearchSecurityException) { | ||
| notificationPermissionException = e | ||
| } | ||
|
|
||
| // If the channel was not found, try to retrieve the Destination | ||
| if (channel == null) { | ||
| destination = try { | ||
| val table = Table( | ||
| "asc", | ||
| "destination.name.keyword", | ||
| null, | ||
| 1, | ||
| 0, | ||
| null | ||
| ) | ||
| val getDestinationsRequest = GetDestinationsRequest( | ||
| action.destinationId, | ||
| 0L, | ||
| null, | ||
| table, | ||
| "ALL" | ||
| ) | ||
|
|
||
| val getDestinationsResponse: GetDestinationsResponse = monitorCtx.client!!.suspendUntil { | ||
| monitorCtx.client!!.execute(GetDestinationsAction.INSTANCE, getDestinationsRequest, it) | ||
| } | ||
| getDestinationsResponse.destinations.firstOrNull() | ||
| } catch (e: IllegalStateException) { | ||
| // Catching the exception thrown when the Destination was not found so the NotificationActionConfigs object can be returned | ||
| null | ||
| } catch (e: OpenSearchSecurityException) { | ||
| if (notificationPermissionException != null) | ||
| throw notificationPermissionException | ||
| else | ||
| throw e | ||
| } | ||
|
|
||
| if (destination == null && notificationPermissionException != null) | ||
| throw notificationPermissionException | ||
| } | ||
|
|
||
| return NotificationActionConfigs(destination, channel) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.