-
Notifications
You must be signed in to change notification settings - Fork 104
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
Integrate with Notifications plugin for Alerting backend #401
Merged
qreshi
merged 10 commits into
opensearch-project:main
from
qreshi:monitor-exec-notif-integration
Apr 17, 2022
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6d8ef81
Add utils for retrieving notifications to both legacy Destinations an…
qreshi e2fe7a6
Refactor runAction in MonitorRunner to be able to send notifications …
qreshi 75674c2
Fix error handling when Notification config is not found and support …
qreshi 2adaebe
Fix issue with fallback setting for Destination email keystore settings
qreshi 0ccd42d
Add publishing email Destinations via Notifications passthrough API
qreshi 6064a4c
Remove unused code in NotificationApiUtils
qreshi 6803523
Use subject as the title for Email Channels
qreshi d830185
Combine runAction() methods
qreshi e76d4fd
Pass accountName to LegacyEmailMessage
qreshi a997d40
Split retrieval of Destination configs in DestinationMigrationUtilSer…
qreshi 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 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 |
---|---|---|
|
@@ -35,6 +35,7 @@ import org.opensearch.alerting.model.action.ActionExecutionScope | |
import org.opensearch.alerting.model.action.AlertCategory | ||
import org.opensearch.alerting.model.action.PerAlertActionScope | ||
import org.opensearch.alerting.model.action.PerExecutionActionScope | ||
import org.opensearch.alerting.model.destination.Destination | ||
import org.opensearch.alerting.model.destination.DestinationContextFactory | ||
import org.opensearch.alerting.opensearchapi.InjectorContextElement | ||
import org.opensearch.alerting.opensearchapi.retry | ||
|
@@ -54,18 +55,27 @@ import org.opensearch.alerting.settings.DestinationSettings.Companion.ALLOW_LIST | |
import org.opensearch.alerting.settings.DestinationSettings.Companion.HOST_DENY_LIST | ||
import org.opensearch.alerting.settings.DestinationSettings.Companion.loadDestinationSettings | ||
import org.opensearch.alerting.settings.LegacyOpenDistroDestinationSettings.Companion.HOST_DENY_LIST_NONE | ||
import org.opensearch.alerting.util.destinationmigration.NotificationActionConfigs | ||
import org.opensearch.alerting.util.destinationmigration.NotificationApiUtils.Companion.getNotificationConfigInfo | ||
import org.opensearch.alerting.util.destinationmigration.createMessageContent | ||
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.getActionExecutionPolicy | ||
import org.opensearch.alerting.util.getBucketKeysHash | ||
import org.opensearch.alerting.util.getCombinedTriggerRunResult | ||
import org.opensearch.alerting.util.isADMonitor | ||
import org.opensearch.alerting.util.isAllowed | ||
import org.opensearch.alerting.util.isBucketLevelMonitor | ||
import org.opensearch.alerting.util.isTestAction | ||
import org.opensearch.client.Client | ||
import org.opensearch.client.node.NodeClient | ||
import org.opensearch.cluster.service.ClusterService | ||
import org.opensearch.common.Strings | ||
import org.opensearch.common.component.AbstractLifecycleComponent | ||
import org.opensearch.common.settings.Settings | ||
import org.opensearch.common.xcontent.NamedXContentRegistry | ||
import org.opensearch.commons.notifications.model.NotificationConfigInfo | ||
import org.opensearch.script.Script | ||
import org.opensearch.script.ScriptService | ||
import org.opensearch.script.TemplateScript | ||
|
@@ -637,31 +647,22 @@ object MonitorRunner : JobRunner, CoroutineScope, AbstractLifecycleComponent() { | |
} | ||
} | ||
|
||
private suspend fun runAction(action: Action, ctx: QueryLevelTriggerExecutionContext, dryrun: Boolean): ActionRunResult { | ||
private suspend fun runAction(action: Action, ctx: TriggerExecutionContext, dryrun: Boolean): ActionRunResult { | ||
return try { | ||
if (!isActionActionable(action, ctx.alert)) { | ||
if (ctx is QueryLevelTriggerExecutionContext && !isActionActionable(action, ctx.alert)) { | ||
return ActionRunResult(action.id, action.name, mapOf(), true, null, null) | ||
} | ||
|
||
val actionOutput = mutableMapOf<String, String>() | ||
actionOutput[SUBJECT] = if (action.subjectTemplate != null) compileTemplate(action.subjectTemplate, ctx) else "" | ||
actionOutput[MESSAGE] = compileTemplate(action.messageTemplate, ctx) | ||
if (Strings.isNullOrEmpty(actionOutput[MESSAGE])) { | ||
throw IllegalStateException("Message content missing in the Destination with id: ${action.destinationId}") | ||
} | ||
if (!dryrun) { | ||
// TODO: Inject user here so only Destination/Notifications that the user has permissions to are retrieved | ||
withContext(Dispatchers.IO) { | ||
val destination = AlertingConfigAccessor.getDestinationInfo(client, xContentRegistry, action.destinationId) | ||
if (!destination.isAllowed(allowList)) { | ||
throw IllegalStateException("Monitor contains a Destination type that is not allowed: ${destination.type}") | ||
} | ||
|
||
val destinationCtx = destinationContextFactory.getDestinationContext(destination) | ||
actionOutput[MESSAGE_ID] = destination.publish( | ||
actionOutput[SUBJECT], | ||
actionOutput[MESSAGE]!!, | ||
destinationCtx, | ||
hostDenyList | ||
) | ||
actionOutput[MESSAGE_ID] = getConfigAndSendNotification(action, actionOutput[SUBJECT], actionOutput[MESSAGE]!!) | ||
} | ||
} | ||
ActionRunResult(action.id, action.name, actionOutput, false, currentTime(), null) | ||
|
@@ -670,41 +671,67 @@ object MonitorRunner : JobRunner, CoroutineScope, AbstractLifecycleComponent() { | |
} | ||
} | ||
|
||
// TODO: This is largely a duplicate of runAction above for BucketLevelTriggerExecutionContext for now. | ||
// After suppression logic implementation, if this remains mostly the same, it can be refactored. | ||
private suspend fun runAction(action: Action, ctx: BucketLevelTriggerExecutionContext, dryrun: Boolean): ActionRunResult { | ||
return try { | ||
val actionOutput = mutableMapOf<String, String>() | ||
actionOutput[SUBJECT] = if (action.subjectTemplate != null) compileTemplate(action.subjectTemplate, ctx) else "" | ||
actionOutput[MESSAGE] = compileTemplate(action.messageTemplate, ctx) | ||
if (Strings.isNullOrEmpty(actionOutput[MESSAGE])) { | ||
throw IllegalStateException("Message content missing in the Destination with id: ${action.destinationId}") | ||
} | ||
if (!dryrun) { | ||
withContext(Dispatchers.IO) { | ||
val destination = AlertingConfigAccessor.getDestinationInfo(client, xContentRegistry, action.destinationId) | ||
if (!destination.isAllowed(allowList)) { | ||
throw IllegalStateException("Monitor contains a Destination type that is not allowed: ${destination.type}") | ||
} | ||
private suspend fun getConfigAndSendNotification(action: Action, subject: String?, message: String): String { | ||
val config = getConfigForNotificationAction(action) | ||
|
||
val destinationCtx = destinationContextFactory.getDestinationContext(destination) | ||
actionOutput[MESSAGE_ID] = destination.publish( | ||
actionOutput[SUBJECT], | ||
actionOutput[MESSAGE]!!, | ||
destinationCtx, | ||
hostDenyList | ||
) | ||
} | ||
} | ||
ActionRunResult(action.id, action.name, actionOutput, false, currentTime(), null) | ||
} catch (e: Exception) { | ||
ActionRunResult(action.id, action.name, mapOf(), false, currentTime(), e) | ||
if (config.destination == null && config.channel == null) { | ||
throw IllegalStateException("Unable to find a Notification Channel or Destination config with id [${action.id}]") | ||
} | ||
|
||
// 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(allowList) == false) { | ||
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. can we rename |
||
throw IllegalStateException( | ||
"Monitor contains a Destination type that is not allowed: ${config.destination.type}" | ||
) | ||
} | ||
|
||
var actionResponseContent = "" | ||
actionResponseContent = config.channel | ||
?.sendNotification( | ||
client, | ||
config.channel.getTitle(subject), | ||
config.channel.createMessageContent(subject, message) | ||
) ?: actionResponseContent | ||
|
||
actionResponseContent = config.destination | ||
?.buildLegacyBaseMessage(subject, message, destinationContextFactory.getDestinationContext(config.destination)) | ||
?.publishLegacyNotification(client) | ||
?: actionResponseContent | ||
|
||
return actionResponseContent | ||
} | ||
|
||
private fun compileTemplate(template: Script, ctx: TriggerExecutionContext): String { | ||
return scriptService.compile(template, TemplateScript.CONTEXT) | ||
.newInstance(template.params + mapOf("ctx" to ctx.asTemplateArg())) | ||
.execute() | ||
} | ||
|
||
/** | ||
* 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): NotificationActionConfigs { | ||
var destination: Destination? = null | ||
val channel: NotificationConfigInfo? = getNotificationConfigInfo(client as NodeClient, action.destinationId) | ||
|
||
// If the channel was not found, try to retrieve the Destination | ||
if (channel == null) { | ||
destination = try { | ||
AlertingConfigAccessor.getDestinationInfo(client, xContentRegistry, action.destinationId) | ||
} catch (e: IllegalStateException) { | ||
// Catching the exception thrown when the Destination was not found so the NotificationActionConfigs object can be returned | ||
null | ||
} | ||
} | ||
|
||
return NotificationActionConfigs(destination, channel) | ||
} | ||
} |
This file contains 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 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 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
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.
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.
can we define this constant somewhere else and use it both at the time assert and here.
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.
+1