From 107340c6c6bd4fa0de83f633522cfffc17bf75e9 Mon Sep 17 00:00:00 2001 From: Surya Sashank Nistala Date: Thu, 6 Jul 2023 21:00:31 -0700 Subject: [PATCH 1/3] fix alert constructor with noop trigger to use execution id and workflow id Signed-off-by: Surya Sashank Nistala --- .../org/opensearch/alerting/AlertService.kt | 19 +++++++++++++++---- .../alerting/DocumentLevelMonitorRunner.kt | 9 +++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/AlertService.kt b/alerting/src/main/kotlin/org/opensearch/alerting/AlertService.kt index f2d07a102..3b00a1962 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/AlertService.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/AlertService.kt @@ -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 @@ -207,13 +208,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 + schemaVersion = IndexUtils.alertIndexSchemaVersion, + workflowId = workflowRunContext?.workflowId ?: "", + executionId = executionId ?: "" ) } @@ -311,7 +316,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) @@ -326,7 +336,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) { diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/DocumentLevelMonitorRunner.kt b/alerting/src/main/kotlin/org/opensearch/alerting/DocumentLevelMonitorRunner.kt index e0d9b2ef8..1824f3075 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/DocumentLevelMonitorRunner.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/DocumentLevelMonitorRunner.kt @@ -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) } @@ -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, From 5d012a9a36f17a00bb0da0cd5edc796543393735 Mon Sep 17 00:00:00 2001 From: Surya Sashank Nistala Date: Fri, 7 Jul 2023 02:05:39 -0700 Subject: [PATCH 2/3] update alerting mappings for workflow_name field Signed-off-by: Surya Sashank Nistala --- .../alerting/alerts/alert_mapping.json | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/alerting/src/main/resources/org/opensearch/alerting/alerts/alert_mapping.json b/alerting/src/main/resources/org/opensearch/alerting/alerts/alert_mapping.json index acabec7e0..53fb5b0a2 100644 --- a/alerting/src/main/resources/org/opensearch/alerting/alerts/alert_mapping.json +++ b/alerting/src/main/resources/org/opensearch/alerting/alerts/alert_mapping.json @@ -13,9 +13,6 @@ "monitor_id": { "type": "keyword" }, - "workflow_id": { - "type": "keyword" - }, "monitor_version": { "type": "long" }, @@ -28,9 +25,6 @@ "severity": { "type": "keyword" }, - "execution_id": { - "type": "keyword" - }, "monitor_name": { "type": "text", "fields": { @@ -77,6 +71,15 @@ } } }, + "execution_id": { + "type": "keyword" + }, + "workflow_id": { + "type": "keyword" + }, + "workflow_name": { + "type": "keyword" + }, "trigger_id": { "type": "keyword" }, @@ -97,6 +100,14 @@ } } }, + "associated_alert_ids": { + "type" : "text", + "fields" : { + "keyword" : { + "type" : "keyword" + } + } + }, "related_doc_ids": { "type" : "text", "fields" : { From 98a55e9ddd6478de4806a4db6fc38215a59c09cb Mon Sep 17 00:00:00 2001 From: Subhobrata Dey Date: Fri, 7 Jul 2023 23:44:38 +0000 Subject: [PATCH 3/3] remove httpcomponents force version Signed-off-by: Subhobrata Dey --- alerting/build.gradle | 3 --- 1 file changed, 3 deletions(-) diff --git a/alerting/build.gradle b/alerting/build.gradle index 63c1ba09a..581b866b3 100644 --- a/alerting/build.gradle +++ b/alerting/build.gradle @@ -77,7 +77,6 @@ configurations.all { force "com.fasterxml.jackson.core:jackson-core:${versions.jackson}" force "com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:${versions.jackson}" force "commons-logging:commons-logging:${versions.commonslogging}" - force "org.apache.httpcomponents.core5:httpcore5:5.1.4" // force the version until OpenSearch upgrade to an invulnerable one, https://www.whitesourcesoftware.com/vulnerability-database/WS-2019-0379 force "commons-codec:commons-codec:1.13" @@ -113,8 +112,6 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-common:${kotlin_version}" implementation "org.jetbrains:annotations:13.0" - implementation "org.apache.httpcomponents.core5:httpcore5:5.1.4" - api project(":alerting-core") implementation "com.github.seancfoley:ipaddress:5.3.3"