Skip to content
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

[Backport 2.x] fix alert constructor with noop trigger to use executi… #994

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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