diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/DocumentMonitorRunnerIT.kt b/alerting/src/test/kotlin/org/opensearch/alerting/DocumentMonitorRunnerIT.kt index e2e45d8a6..c21310f58 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/DocumentMonitorRunnerIT.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/DocumentMonitorRunnerIT.kt @@ -47,7 +47,7 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() { val index = createTestIndex() - val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3", fields = listOf()) + val docQuery = DocLevelQuery(query = "test_field:\"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) @@ -108,7 +108,7 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() { val index = createTestIndex() val docQuery = - DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3", fields = listOf(), queryFieldNames = listOf("test_field")) + DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3", queryFieldNames = listOf("test_field")) val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery)) val action = randomAction(template = randomTemplateScript("Hello {{ctx.monitor.name}}"), destinationId = createDestination().id) @@ -154,7 +154,7 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() { val index = createTestIndex() val docQuery = - DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3", fields = listOf()) + DocLevelQuery(query = "test_field:\"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) @@ -199,7 +199,6 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() { val docQuery = DocLevelQuery( query = "test_field:\"us-west-2\"", name = "3", - fields = listOf(), queryFieldNames = listOf("wrong_field") ) val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery)) @@ -253,153 +252,6 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() { val docQuery = DocLevelQuery( query = "test_field:\"us-west-2\"", name = "3", - fields = listOf(), - queryFieldNames = listOf("wrong_field") - ) - val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery)) - - val action = randomAction(template = randomTemplateScript("Hello {{ctx.monitor.name}}"), destinationId = createDestination().id) - val monitor = randomDocumentLevelMonitor( - inputs = listOf(docLevelInput), - triggers = listOf(randomDocumentLevelTrigger(condition = ALWAYS_RUN, actions = listOf(action))) - ) - - indexDoc(index, "1", testDoc) - - val response = executeMonitor(monitor, params = DRYRUN_MONITOR) - - val output = entityAsMap(response) - assertEquals(monitor.name, output["monitor_name"]) - - assertEquals(1, output.objectMap("trigger_results").values.size) - - for (triggerResult in output.objectMap("trigger_results").values) { - assertEquals(1, triggerResult.objectMap("action_results").values.size) - for (alertActionResult in triggerResult.objectMap("action_results").values) { - for (actionResult in alertActionResult.values) { - @Suppress("UNCHECKED_CAST") val actionOutput = (actionResult as Map>)["output"] - as Map - assertEquals("Hello ${monitor.name}", actionOutput["subject"]) - assertEquals("Hello ${monitor.name}", actionOutput["message"]) - } - } - } - - val alerts = searchAlerts(monitor) - assertEquals("Alert saved for test monitor", 0, alerts.size) - } - - fun `test dryrun execute monitor with queryFieldNames set up with correct field`() { - - val testTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now().truncatedTo(MILLIS)) - val testDoc = """{ - "message" : "This is an error from IAD region", - "test_strict_date_time" : "$testTime", - "test_field" : "us-west-2" - }""" - - val index = createTestIndex() - - val docQuery = - DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3", fields = listOf(), queryFieldNames = listOf("test_field")) - val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery)) - - val action = randomAction(template = randomTemplateScript("Hello {{ctx.monitor.name}}"), destinationId = createDestination().id) - val monitor = randomDocumentLevelMonitor( - inputs = listOf(docLevelInput), - triggers = listOf(randomDocumentLevelTrigger(condition = ALWAYS_RUN, actions = listOf(action))) - ) - - indexDoc(index, "1", testDoc) - - val response = executeMonitor(monitor, params = DRYRUN_MONITOR) - - val output = entityAsMap(response) - assertEquals(monitor.name, output["monitor_name"]) - - assertEquals(1, output.objectMap("trigger_results").values.size) - - for (triggerResult in output.objectMap("trigger_results").values) { - assertEquals(1, triggerResult.objectMap("action_results").values.size) - for (alertActionResult in triggerResult.objectMap("action_results").values) { - for (actionResult in alertActionResult.values) { - @Suppress("UNCHECKED_CAST") val actionOutput = (actionResult as Map>)["output"] - as Map - assertEquals("Hello ${monitor.name}", actionOutput["subject"]) - assertEquals("Hello ${monitor.name}", actionOutput["message"]) - } - } - } - - val alerts = searchAlerts(monitor) - assertEquals("Alert saved for test monitor", 0, alerts.size) - } - - fun `test dryrun execute monitor with queryFieldNames set up with wrong field`() { - - val testTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now().truncatedTo(MILLIS)) - val testDoc = """{ - "message" : "This is an error from IAD region", - "test_strict_date_time" : "$testTime", - "test_field" : "us-west-2" - }""" - - val index = createTestIndex() - // using wrong field name - val docQuery = DocLevelQuery( - query = "test_field:\"us-west-2\"", - name = "3", - fields = listOf(), - queryFieldNames = listOf("wrong_field") - ) - val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery)) - - val action = randomAction(template = randomTemplateScript("Hello {{ctx.monitor.name}}"), destinationId = createDestination().id) - val monitor = randomDocumentLevelMonitor( - inputs = listOf(docLevelInput), - triggers = listOf(randomDocumentLevelTrigger(condition = ALWAYS_RUN, actions = listOf(action))) - ) - - indexDoc(index, "1", testDoc) - - val response = executeMonitor(monitor, params = DRYRUN_MONITOR) - - val output = entityAsMap(response) - assertEquals(monitor.name, output["monitor_name"]) - - assertEquals(1, output.objectMap("trigger_results").values.size) - - for (triggerResult in output.objectMap("trigger_results").values) { - assertEquals(0, triggerResult.objectMap("action_results").values.size) - for (alertActionResult in triggerResult.objectMap("action_results").values) { - for (actionResult in alertActionResult.values) { - @Suppress("UNCHECKED_CAST") val actionOutput = (actionResult as Map>)["output"] - as Map - assertEquals("Hello ${monitor.name}", actionOutput["subject"]) - assertEquals("Hello ${monitor.name}", actionOutput["message"]) - } - } - } - - val alerts = searchAlerts(monitor) - assertEquals("Alert saved for test monitor", 0, alerts.size) - } - - fun `test fetch_query_field_names setting is disabled by configuring queryFieldNames set up with wrong field still works`() { - adminClient().updateSettings(AlertingSettings.DOC_LEVEL_MONITOR_FETCH_ONLY_QUERY_FIELDS_ENABLED.key, "false") - val testTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now().truncatedTo(MILLIS)) - val testDoc = """{ - "message" : "This is an error from IAD region", - "test_strict_date_time" : "$testTime", - "test_field" : "us-west-2" - }""" - - val index = createTestIndex() - // using wrong field name - val docQuery = DocLevelQuery( - query = "test_field:\"us-west-2\"", - name = "3", - fields = listOf(), queryFieldNames = listOf("wrong_field") ) val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery)) @@ -491,7 +343,7 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() { "test_field" : "us-west-2" }""" - val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3", fields = listOf()) + val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3") val docLevelInput = DocLevelMonitorInput("description", listOf(testIndex), listOf(docQuery)) val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN) @@ -537,7 +389,7 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() { "test_field" : "us-east-1" }""" - val docQuery2 = DocLevelQuery(query = "test_field:\"us-east-1\"", name = "3", fields = listOf()) + val docQuery2 = DocLevelQuery(query = "test_field:\"us-east-1\"", name = "3") val docLevelInput2 = DocLevelMonitorInput("description", listOf(testIndex2), listOf(docQuery2)) val trigger2 = randomDocumentLevelTrigger(condition = ALWAYS_RUN) @@ -630,7 +482,7 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() { "test_field" : "us-west-2" }""" - val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3", fields = listOf()) + val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3") val docLevelInput = DocLevelMonitorInput("description", listOf(testIndex), listOf(docQuery)) val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN) @@ -668,7 +520,7 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() { "test_field" : "us-west-2" }""" - val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3", fields = listOf()) + val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3") val docLevelInput = DocLevelMonitorInput("description", listOf(testIndex), listOf(docQuery)) val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN) @@ -960,7 +812,7 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() { "test_field" : "us-west-2" }""" - val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = testQueryName, fields = listOf()) + val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = testQueryName) val docLevelInput = DocLevelMonitorInput("description", listOf("$testIndexPrefix*"), listOf(docQuery)) val trigger = randomDocumentLevelTrigger(condition = Script("query[name=$testQueryName]")) @@ -1839,7 +1691,7 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() { false ) - val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3", fields = listOf()) + val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3") val docLevelInput = DocLevelMonitorInput("description", listOf(dataStreamName), listOf(docQuery)) val action = randomAction(template = randomTemplateScript("Hello {{ctx.monitor.name}}"), destinationId = createDestination().id) @@ -1891,7 +1743,7 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() { false ) - val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3", fields = listOf()) + val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3") val docLevelInput = DocLevelMonitorInput("description", listOf(dataStreamName), listOf(docQuery)) val action = randomAction(template = randomTemplateScript("Hello {{ctx.monitor.name}}"), destinationId = createDestination().id) @@ -1953,7 +1805,7 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() { """.trimIndent() ) - val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3", fields = listOf()) + val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3") val docLevelInput = DocLevelMonitorInput("description", listOf("$aliasName"), listOf(docQuery)) val action = randomAction(template = randomTemplateScript("Hello {{ctx.monitor.name}}"), destinationId = createDestination().id) @@ -2028,7 +1880,7 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() { rolloverDatastream(dataStreamName2) indexDoc(dataStreamName2, "0", testDoc) - val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3", fields = listOf()) + val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3") val docLevelInput = DocLevelMonitorInput("description", listOf("test-datastream*"), listOf(docQuery)) val action = randomAction(template = randomTemplateScript("Hello {{ctx.monitor.name}}"), destinationId = createDestination().id) @@ -2102,7 +1954,7 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() { rolloverDatastream(dataStreamName) indexDoc(dataStreamName, "0", testDoc) - val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3", fields = listOf()) + val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3") val docLevelInput = DocLevelMonitorInput("description", listOf(dataStreamName), listOf(docQuery)) val action = randomAction(template = randomTemplateScript("Hello {{ctx.monitor.name}}"), destinationId = createDestination().id) diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/MonitorDataSourcesIT.kt b/alerting/src/test/kotlin/org/opensearch/alerting/MonitorDataSourcesIT.kt index 132bb3933..810836bf9 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/MonitorDataSourcesIT.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/MonitorDataSourcesIT.kt @@ -370,45 +370,38 @@ class MonitorDataSourcesIT : AlertingSingleNodeTestCase() { val docQuery1 = DocLevelQuery( query = "source.ip.v6.v1:12345", name = "3", - fields = listOf() ) val docQuery2 = DocLevelQuery( query = "source.ip.v6.v2:16645", name = "4", - fields = listOf(), queryFieldNames = listOf("alias.some.fff", "source.ip.v6.v1") ) val docQuery3 = DocLevelQuery( query = "source.ip.v4.v0:120", name = "5", - fields = listOf(), queryFieldNames = listOf("alias.some.fff", "source.ip.v6.v1") ) val docQuery4 = DocLevelQuery( query = "alias.some.fff:\"us-west-2\"", name = "6", - fields = listOf(), queryFieldNames = listOf("alias.some.fff", "source.ip.v6.v1") ) val docQuery5 = DocLevelQuery( query = "message:\"This is an error from IAD region\"", name = "7", queryFieldNames = listOf("alias.some.fff", "source.ip.v6.v1"), - fields = listOf() ) val docQuery6 = DocLevelQuery( query = "type.subtype:\"some subtype\"", name = "8", - fields = listOf(), queryFieldNames = listOf("alias.some.fff", "source.ip.v6.v1") ) val docQuery7 = DocLevelQuery( query = "supertype.type:\"some type\"", name = "9", - fields = listOf(), queryFieldNames = listOf("alias.some.fff", "source.ip.v6.v1") ) val docLevelInput = DocLevelMonitorInput( @@ -469,46 +462,39 @@ class MonitorDataSourcesIT : AlertingSingleNodeTestCase() { val docQuery1 = DocLevelQuery( query = "source.ip.v6.v1:12345", name = "3", - fields = listOf(), queryFieldNames = listOf("alias.some.fff", "source.ip.v6.v1") ) val docQuery2 = DocLevelQuery( query = "source.ip.v6.v2:16645", name = "4", - fields = listOf(), queryFieldNames = listOf("source.ip.v6.v2") ) val docQuery3 = DocLevelQuery( query = "source.ip.v4.v0:120", name = "5", - fields = listOf(), queryFieldNames = listOf("source.ip.v6.v4") ) val docQuery4 = DocLevelQuery( query = "alias.some.fff:\"us-west-2\"", name = "6", - fields = listOf(), queryFieldNames = listOf("alias.some.fff") ) val docQuery5 = DocLevelQuery( query = "message:\"This is an error from IAD region\"", name = "7", queryFieldNames = listOf("alias.some.fff", "source.ip.v6.v1"), - fields = listOf() ) val docQuery6 = DocLevelQuery( query = "type.subtype:\"some subtype\"", name = "8", - fields = listOf(), queryFieldNames = listOf("alias.some.fff", "source.ip.v6.v1") ) val docQuery7 = DocLevelQuery( query = "supertype.type:\"some type\"", name = "9", - fields = listOf(), queryFieldNames = listOf("alias.some.fff", "source.ip.v6.v1") ) val docLevelInput = DocLevelMonitorInput( diff --git a/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/WorkflowRestApiIT.kt b/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/WorkflowRestApiIT.kt index a865c344d..22791b5aa 100644 --- a/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/WorkflowRestApiIT.kt +++ b/alerting/src/test/kotlin/org/opensearch/alerting/resthandler/WorkflowRestApiIT.kt @@ -1197,7 +1197,7 @@ class WorkflowRestApiIT : AlertingRestTestCase() { "test_field" : "us-west-2" }""" - val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3", fields = listOf()) + val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3") val docLevelInput = DocLevelMonitorInput("description", listOf(testIndex), listOf(docQuery)) val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN) diff --git a/core/src/main/kotlin/org/opensearch/alerting/core/lock/LockModel.kt b/core/src/main/kotlin/org/opensearch/alerting/core/lock/LockModel.kt index 706bd0987..91d144013 100644 --- a/core/src/main/kotlin/org/opensearch/alerting/core/lock/LockModel.kt +++ b/core/src/main/kotlin/org/opensearch/alerting/core/lock/LockModel.kt @@ -5,11 +5,11 @@ package org.opensearch.alerting.core.lock +import org.opensearch.common.xcontent.XContentParserUtils import org.opensearch.core.xcontent.ToXContent import org.opensearch.core.xcontent.ToXContentObject import org.opensearch.core.xcontent.XContentBuilder import org.opensearch.core.xcontent.XContentParser -import org.opensearch.core.xcontent.XContentParserUtils import org.opensearch.index.seqno.SequenceNumbers import java.io.IOException import java.time.Instant diff --git a/core/src/main/kotlin/org/opensearch/alerting/core/lock/LockService.kt b/core/src/main/kotlin/org/opensearch/alerting/core/lock/LockService.kt index 35618e156..03268eb83 100644 --- a/core/src/main/kotlin/org/opensearch/alerting/core/lock/LockService.kt +++ b/core/src/main/kotlin/org/opensearch/alerting/core/lock/LockService.kt @@ -2,6 +2,7 @@ package org.opensearch.alerting.core.lock import org.apache.logging.log4j.LogManager import org.opensearch.ResourceAlreadyExistsException +import org.opensearch.action.ActionListener import org.opensearch.action.DocWriteResponse import org.opensearch.action.admin.indices.create.CreateIndexRequest import org.opensearch.action.admin.indices.create.CreateIndexResponse @@ -20,7 +21,6 @@ import org.opensearch.common.xcontent.LoggingDeprecationHandler import org.opensearch.common.xcontent.XContentFactory import org.opensearch.common.xcontent.XContentType import org.opensearch.commons.alerting.model.ScheduledJob -import org.opensearch.core.action.ActionListener import org.opensearch.core.xcontent.NamedXContentRegistry import org.opensearch.core.xcontent.ToXContent import org.opensearch.index.IndexNotFoundException