Skip to content

Commit

Permalink
fix monitor runner
Browse files Browse the repository at this point in the history
  • Loading branch information
sbcd90 committed Oct 6, 2023
1 parent c6a28a8 commit a969f41
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<SubscribeFindingsResponse> {
override fun onResponse(response: SubscribeFindingsResponse) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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`() {
Expand Down

0 comments on commit a969f41

Please sign in to comment.