From a969f41ca185cbda80a7ededbbd8ac8d1be3550c Mon Sep 17 00:00:00 2001 From: Subhobrata Dey Date: Fri, 6 Oct 2023 22:31:40 +0000 Subject: [PATCH] fix monitor runner --- .../alerting/DocumentLevelMonitorRunner.kt | 2 ++ .../alerting/util/DocLevelMonitorQueries.kt | 2 +- .../alerting/DocumentMonitorRunnerIT.kt | 28 ++++++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/DocumentLevelMonitorRunner.kt b/alerting/src/main/kotlin/org/opensearch/alerting/DocumentLevelMonitorRunner.kt index 35630aa51..42a5affb8 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/DocumentLevelMonitorRunner.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/DocumentLevelMonitorRunner.kt @@ -35,6 +35,7 @@ import org.opensearch.cluster.service.ClusterService import org.opensearch.common.xcontent.XContentFactory import org.opensearch.common.xcontent.XContentType import org.opensearch.commons.alerting.AlertingPluginInterface +import org.opensearch.commons.alerting.action.AlertingActions import org.opensearch.commons.alerting.action.PublishFindingsRequest import org.opensearch.commons.alerting.action.SubscribeFindingsResponse import org.opensearch.commons.alerting.model.ActionExecutionResult @@ -490,6 +491,7 @@ object DocumentLevelMonitorRunner : MonitorRunner() { val publishFindingsRequest = PublishFindingsRequest(monitor.id, finding) AlertingPluginInterface.publishFinding( monitorCtx.client!! as NodeClient, + AlertingActions.SUBSCRIBE_FINDINGS_ACTION_TYPE, publishFindingsRequest, object : ActionListener { override fun onResponse(response: SubscribeFindingsResponse) {} diff --git a/alerting/src/main/kotlin/org/opensearch/alerting/util/DocLevelMonitorQueries.kt b/alerting/src/main/kotlin/org/opensearch/alerting/util/DocLevelMonitorQueries.kt index 6dc7c12ba..99143d62d 100644 --- a/alerting/src/main/kotlin/org/opensearch/alerting/util/DocLevelMonitorQueries.kt +++ b/alerting/src/main/kotlin/org/opensearch/alerting/util/DocLevelMonitorQueries.kt @@ -365,7 +365,7 @@ class DocLevelMonitorQueries(private val client: Client, private val clusterServ .id(it.id + "_$monitorId") .source( mapOf( - "query" to mapOf("query_string" to mapOf("query" to query)), + "query" to mapOf("query_string" to mapOf("query" to query, "fields" to listOf("*"))), "monitor_id" to monitorId, "index" to sourceIndex ) diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/DocumentMonitorRunnerIT.kt b/alerting/src/test/kotlin/org/opensearch/alerting/DocumentMonitorRunnerIT.kt index c8ab62933..5c05caafe 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/DocumentMonitorRunnerIT.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/DocumentMonitorRunnerIT.kt @@ -41,7 +41,7 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() { val index = createTestIndex() - val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3") + val docQuery = DocLevelQuery(query = "\"us-west-2\"", name = "3") val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery)) val action = randomAction(template = randomTemplateScript("Hello {{ctx.monitor.name}}"), destinationId = createDestination().id) @@ -73,6 +73,32 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() { val alerts = searchAlerts(monitor) assertEquals("Alert saved for test monitor", 0, alerts.size) + + val testDoc1 = """{ + "message" : "us-west-2", + "test_strict_date_time" : "$testTime", + "test_field" : "This is an error from IAD region" + }""" + indexDoc(index, "2", testDoc1) + val response1 = executeMonitor(monitor, params = DRYRUN_MONITOR) + + val output1 = entityAsMap(response1) + assertEquals(monitor.name, output1["monitor_name"]) + + assertEquals(1, output1.objectMap("trigger_results").values.size) + + val testDoc2 = """{ + "message1" : "us-west-2", + "test_strict_date_time" : "$testTime", + "test_field" : "This is an error from IAD region" + }""" + indexDoc(index, "4", testDoc2) + val response2 = executeMonitor(monitor, params = DRYRUN_MONITOR) + + val output2 = entityAsMap(response2) + assertEquals(monitor.name, output2["monitor_name"]) + + assertEquals(1, output2.objectMap("trigger_results").values.size) } fun `test execute monitor returns search result with dryrun`() {