Skip to content

Commit

Permalink
[Backport 2.x] fix alert constructor with noop trigger to use executi…
Browse files Browse the repository at this point in the history
…on id and workflow id (#994)

Signed-off-by: Subhobrata Dey <[email protected]>
Co-authored-by: Surya Sashank Nistala <[email protected]>
(cherry picked from commit 8567028)
  • Loading branch information
sbcd90 authored and github-actions[bot] committed Jul 11, 2023
1 parent 459cd99 commit ab7bb94
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
19 changes: 15 additions & 4 deletions alerting/src/main/kotlin/org/opensearch/alerting/AlertService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.opensearch.alerting.script.QueryLevelTriggerExecutionContext
import org.opensearch.alerting.util.IndexUtils
import org.opensearch.alerting.util.MAX_SEARCH_SIZE
import org.opensearch.alerting.util.getBucketKeysHash
import org.opensearch.alerting.workflow.WorkflowRunContext
import org.opensearch.client.Client
import org.opensearch.common.bytes.BytesReference
import org.opensearch.common.unit.TimeValue
Expand Down Expand Up @@ -213,13 +214,17 @@ class AlertService(
fun composeMonitorErrorAlert(
id: String,
monitor: Monitor,
alertError: AlertError
alertError: AlertError,
executionId: String?,
workflowRunContext: WorkflowRunContext?
): Alert {
val currentTime = Instant.now()
return Alert(
id = id, monitor = monitor, trigger = NoOpTrigger(), startTime = currentTime,
lastNotificationTime = currentTime, state = Alert.State.ERROR, errorMessage = alertError?.message,
schemaVersion = IndexUtils.alertIndexSchemaVersion, executionId = ""
schemaVersion = IndexUtils.alertIndexSchemaVersion,
workflowId = workflowRunContext?.workflowId ?: "",
executionId = executionId ?: ""
)
}

Expand Down Expand Up @@ -319,7 +324,12 @@ class AlertService(
} ?: listOf()
}

suspend fun upsertMonitorErrorAlert(monitor: Monitor, errorMessage: String) {
suspend fun upsertMonitorErrorAlert(
monitor: Monitor,
errorMessage: String,
executionId: String?,
workflowRunContext: WorkflowRunContext?,
) {
val newErrorAlertId = "$ERROR_ALERT_ID_PREFIX-${monitor.id}-${UUID.randomUUID()}"

val searchRequest = SearchRequest(monitor.dataSources.alertsIndex)
Expand All @@ -334,7 +344,8 @@ class AlertService(
)
val searchResponse: SearchResponse = client.suspendUntil { search(searchRequest, it) }

var alert = composeMonitorErrorAlert(newErrorAlertId, monitor, AlertError(Instant.now(), errorMessage))
var alert =
composeMonitorErrorAlert(newErrorAlertId, monitor, AlertError(Instant.now(), errorMessage), executionId, workflowRunContext)

if (searchResponse.hits.totalHits.value > 0L) {
if (searchResponse.hits.totalHits.value > 1L) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,12 @@ object DocumentLevelMonitorRunner : MonitorRunner() {
// If any error happened during trigger execution, upsert monitor error alert
val errorMessage = constructErrorMessageFromTriggerResults(triggerResults = triggerResults)
if (errorMessage.isNotEmpty()) {
monitorCtx.alertService!!.upsertMonitorErrorAlert(monitor = monitor, errorMessage = errorMessage)
monitorCtx.alertService!!.upsertMonitorErrorAlert(
monitor = monitor,
errorMessage = errorMessage,
executionId = workflowRunContext?.executionId,
workflowRunContext
)
} else {
onSuccessfulMonitorRun(monitorCtx, monitor)
}
Expand All @@ -263,7 +268,7 @@ object DocumentLevelMonitorRunner : MonitorRunner() {
return monitorResult.copy(triggerResults = triggerResults)
} catch (e: Exception) {
val errorMessage = ExceptionsHelper.detailedMessage(e)
monitorCtx.alertService!!.upsertMonitorErrorAlert(monitor, errorMessage)
monitorCtx.alertService!!.upsertMonitorErrorAlert(monitor, errorMessage, workflowRunContext?.executionId, workflowRunContext)
logger.error("Failed running Document-level-monitor ${monitor.name}", e)
val alertingException = AlertingException(
errorMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
"monitor_id": {
"type": "keyword"
},
"workflow_id": {
"type": "keyword"
},
"monitor_version": {
"type": "long"
},
Expand All @@ -28,9 +25,6 @@
"severity": {
"type": "keyword"
},
"execution_id": {
"type": "keyword"
},
"monitor_name": {
"type": "text",
"fields": {
Expand Down Expand Up @@ -77,6 +71,15 @@
}
}
},
"execution_id": {
"type": "keyword"
},
"workflow_id": {
"type": "keyword"
},
"workflow_name": {
"type": "keyword"
},
"trigger_id": {
"type": "keyword"
},
Expand All @@ -97,6 +100,14 @@
}
}
},
"associated_alert_ids": {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
"related_doc_ids": {
"type" : "text",
"fields" : {
Expand Down

0 comments on commit ab7bb94

Please sign in to comment.