Skip to content

Commit 0740d9b

Browse files
authored
Ack alerts - allow moving alerts to history index with custom datasources (opensearch-project#626)
* in case of custom indices, allow moving alerts to history index Signed-off-by: Petar Dzepina <[email protected]> * empty commit Signed-off-by: Petar Dzepina <[email protected]> * added IT for custom datasources alert ack Signed-off-by: Petar Dzepina <[email protected]> Signed-off-by: Petar Dzepina <[email protected]>
1 parent 49fbb4d commit 0740d9b

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

alerting/src/main/kotlin/org/opensearch/alerting/transport/TransportAcknowledgeAlertAction.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import org.opensearch.action.update.UpdateRequest
2424
import org.opensearch.alerting.action.GetMonitorAction
2525
import org.opensearch.alerting.action.GetMonitorRequest
2626
import org.opensearch.alerting.action.GetMonitorResponse
27-
import org.opensearch.alerting.alerts.AlertIndices
2827
import org.opensearch.alerting.opensearchapi.suspendUntil
2928
import org.opensearch.alerting.settings.AlertingSettings
3029
import org.opensearch.alerting.util.AlertingException
@@ -161,7 +160,6 @@ class TransportAcknowledgeAlertAction @Inject constructor(
161160

162161
if (alert.state == Alert.State.ACTIVE) {
163162
if (
164-
monitor.dataSources.alertsIndex != AlertIndices.ALERT_INDEX ||
165163
alert.findingIds.isEmpty() ||
166164
!isAlertHistoryEnabled
167165
) {

alerting/src/test/kotlin/org/opensearch/alerting/MonitorDataSourcesIT.kt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,76 @@ class MonitorDataSourcesIT : AlertingSingleNodeTestCase() {
516516
assertEquals("Alerts from custom history index", 1, alerts.size)
517517
}
518518

519+
fun `test search custom alerts history index after alert ack`() {
520+
val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3")
521+
val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery))
522+
val trigger1 = randomDocumentLevelTrigger(condition = ALWAYS_RUN)
523+
val trigger2 = randomDocumentLevelTrigger(condition = ALWAYS_RUN)
524+
val customAlertsIndex = "custom_alerts_index"
525+
val customAlertsHistoryIndex = "custom_alerts_history_index"
526+
val customAlertsHistoryIndexPattern = "<custom_alerts_history_index-{now/d}-1>"
527+
var monitor = randomDocumentLevelMonitor(
528+
inputs = listOf(docLevelInput),
529+
triggers = listOf(trigger1, trigger2),
530+
dataSources = DataSources(
531+
alertsIndex = customAlertsIndex,
532+
alertsHistoryIndex = customAlertsHistoryIndex,
533+
alertsHistoryIndexPattern = customAlertsHistoryIndexPattern
534+
)
535+
)
536+
val monitorResponse = createMonitor(monitor)
537+
val testTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now().truncatedTo(MILLIS))
538+
val testDoc = """{
539+
"message" : "This is an error from IAD region",
540+
"test_strict_date_time" : "$testTime",
541+
"test_field" : "us-west-2"
542+
}"""
543+
assertFalse(monitorResponse?.id.isNullOrEmpty())
544+
monitor = monitorResponse!!.monitor
545+
indexDoc(index, "1", testDoc)
546+
val monitorId = monitorResponse.id
547+
val executeMonitorResponse = executeMonitor(monitor, monitorId, false)
548+
var alertsBefore = searchAlerts(monitorId, customAlertsIndex)
549+
Assert.assertEquals(2, alertsBefore.size)
550+
Assert.assertEquals(executeMonitorResponse!!.monitorRunResult.monitorName, monitor.name)
551+
Assert.assertEquals(executeMonitorResponse.monitorRunResult.triggerResults.size, 2)
552+
553+
var alerts = listOf<Alert>()
554+
OpenSearchTestCase.waitUntil({
555+
alerts = searchAlerts(monitorId, customAlertsIndex)
556+
if (alerts.size == 1) {
557+
return@waitUntil true
558+
}
559+
return@waitUntil false
560+
}, 30, TimeUnit.SECONDS)
561+
assertEquals("Alerts from custom index", 2, alerts.size)
562+
563+
val ackReq = AcknowledgeAlertRequest(monitorId, alerts.map { it.id }.toMutableList(), WriteRequest.RefreshPolicy.IMMEDIATE)
564+
client().execute(AlertingActions.ACKNOWLEDGE_ALERTS_ACTION_TYPE, ackReq).get()
565+
566+
// verify alerts moved from alert index to alert history index
567+
alerts = listOf<Alert>()
568+
OpenSearchTestCase.waitUntil({
569+
alerts = searchAlerts(monitorId, customAlertsHistoryIndex)
570+
if (alerts.size == 1) {
571+
return@waitUntil true
572+
}
573+
return@waitUntil false
574+
}, 30, TimeUnit.SECONDS)
575+
assertEquals("Alerts from custom history index", 2, alerts.size)
576+
577+
// verify alerts deleted from alert index
578+
alerts = listOf<Alert>()
579+
OpenSearchTestCase.waitUntil({
580+
alerts = searchAlerts(monitorId, customAlertsIndex)
581+
if (alerts.size == 1) {
582+
return@waitUntil true
583+
}
584+
return@waitUntil false
585+
}, 30, TimeUnit.SECONDS)
586+
assertEquals("Alerts from custom history index", 0, alerts.size)
587+
}
588+
519589
fun `test get alerts by list of monitors containing both existent and non-existent ids`() {
520590
val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3")
521591
val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery))

0 commit comments

Comments
 (0)