Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Ack alerts - allow moving alerts to history index with custom datasources #627

Merged
merged 1 commit into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import org.opensearch.action.update.UpdateRequest
import org.opensearch.alerting.action.GetMonitorAction
import org.opensearch.alerting.action.GetMonitorRequest
import org.opensearch.alerting.action.GetMonitorResponse
import org.opensearch.alerting.alerts.AlertIndices
import org.opensearch.alerting.opensearchapi.suspendUntil
import org.opensearch.alerting.settings.AlertingSettings
import org.opensearch.alerting.util.AlertingException
Expand Down Expand Up @@ -161,7 +160,6 @@ class TransportAcknowledgeAlertAction @Inject constructor(

if (alert.state == Alert.State.ACTIVE) {
if (
monitor.dataSources.alertsIndex != AlertIndices.ALERT_INDEX ||
alert.findingIds.isEmpty() ||
!isAlertHistoryEnabled
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,76 @@ class MonitorDataSourcesIT : AlertingSingleNodeTestCase() {
assertEquals("Alerts from custom history index", 1, alerts.size)
}

fun `test search custom alerts history index after alert ack`() {
val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3")
val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery))
val trigger1 = randomDocumentLevelTrigger(condition = ALWAYS_RUN)
val trigger2 = randomDocumentLevelTrigger(condition = ALWAYS_RUN)
val customAlertsIndex = "custom_alerts_index"
val customAlertsHistoryIndex = "custom_alerts_history_index"
val customAlertsHistoryIndexPattern = "<custom_alerts_history_index-{now/d}-1>"
var monitor = randomDocumentLevelMonitor(
inputs = listOf(docLevelInput),
triggers = listOf(trigger1, trigger2),
dataSources = DataSources(
alertsIndex = customAlertsIndex,
alertsHistoryIndex = customAlertsHistoryIndex,
alertsHistoryIndexPattern = customAlertsHistoryIndexPattern
)
)
val monitorResponse = createMonitor(monitor)
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"
}"""
assertFalse(monitorResponse?.id.isNullOrEmpty())
monitor = monitorResponse!!.monitor
indexDoc(index, "1", testDoc)
val monitorId = monitorResponse.id
val executeMonitorResponse = executeMonitor(monitor, monitorId, false)
var alertsBefore = searchAlerts(monitorId, customAlertsIndex)
Assert.assertEquals(2, alertsBefore.size)
Assert.assertEquals(executeMonitorResponse!!.monitorRunResult.monitorName, monitor.name)
Assert.assertEquals(executeMonitorResponse.monitorRunResult.triggerResults.size, 2)

var alerts = listOf<Alert>()
OpenSearchTestCase.waitUntil({
alerts = searchAlerts(monitorId, customAlertsIndex)
if (alerts.size == 1) {
return@waitUntil true
}
return@waitUntil false
}, 30, TimeUnit.SECONDS)
assertEquals("Alerts from custom index", 2, alerts.size)

val ackReq = AcknowledgeAlertRequest(monitorId, alerts.map { it.id }.toMutableList(), WriteRequest.RefreshPolicy.IMMEDIATE)
client().execute(AlertingActions.ACKNOWLEDGE_ALERTS_ACTION_TYPE, ackReq).get()

// verify alerts moved from alert index to alert history index
alerts = listOf<Alert>()
OpenSearchTestCase.waitUntil({
alerts = searchAlerts(monitorId, customAlertsHistoryIndex)
if (alerts.size == 1) {
return@waitUntil true
}
return@waitUntil false
}, 30, TimeUnit.SECONDS)
assertEquals("Alerts from custom history index", 2, alerts.size)

// verify alerts deleted from alert index
alerts = listOf<Alert>()
OpenSearchTestCase.waitUntil({
alerts = searchAlerts(monitorId, customAlertsIndex)
if (alerts.size == 1) {
return@waitUntil true
}
return@waitUntil false
}, 30, TimeUnit.SECONDS)
assertEquals("Alerts from custom history index", 0, alerts.size)
}

fun `test get alerts by list of monitors containing both existent and non-existent ids`() {
val docQuery = DocLevelQuery(query = "test_field:\"us-west-2\"", name = "3")
val docLevelInput = DocLevelMonitorInput("description", listOf(index), listOf(docQuery))
Expand Down