@@ -44,6 +44,7 @@ import java.time.Instant
4444import java.time.ZoneOffset.UTC
4545import java.time.format.DateTimeFormatter
4646import java.time.temporal.ChronoUnit
47+ import org.opensearch.alerting.alertsv2.AlertV2Indices
4748
4849object PPLMonitorRunner : MonitorV2Runner {
4950 private val logger = LogManager .getLogger(javaClass)
@@ -86,11 +87,10 @@ object PPLMonitorRunner : MonitorV2Runner {
8687 // use threadpool time for cross node consistency
8788 val timeOfCurrentExecution = Instant .ofEpochMilli(MonitorRunnerService .monitorCtx.threadPool!! .absoluteTimeInMillis())
8889
89- // TODO: should alerting v1 and v2 alerts index be separate?
90+ // TODO: put alertV2s in their own index
9091 try {
91- // TODO: write generated V2 alerts to existing alerts v1 index for now, revisit this decision
92- monitorCtx.alertIndices!! .createOrUpdateAlertIndex()
93- monitorCtx.alertIndices!! .createOrUpdateInitialAlertHistoryIndex()
92+ monitorCtx.alertV2Indices!! .createOrUpdateAlertV2Index()
93+ monitorCtx.alertV2Indices!! .createOrUpdateInitialAlertV2HistoryIndex()
9494 } catch (e: Exception ) {
9595 val id = if (pplMonitor.id.trim().isEmpty()) " _na_" else pplMonitor.id
9696 logger.error(" Error loading alerts for monitorV2: $id " , e)
@@ -121,21 +121,6 @@ object PPLMonitorRunner : MonitorV2Runner {
121121 }
122122 logger.info(" suppression check passed, executing trigger ${pplTrigger.name} from monitor ${pplMonitor.name} " )
123123
124- // internal fun isActionActionable(action: Action, alert: Alert?): Boolean {
125- // if (alert != null && alert.state == Alert.State.AUDIT)
126- // return false
127- // if (alert == null || action.throttle == null) {
128- // return true
129- // }
130- // if (action.throttleEnabled) {
131- // val result = alert.actionExecutionResults.firstOrNull { r -> r.actionId == action.id }
132- // val lastExecutionTime: Instant? = result?.lastExecutionTime
133- // val throttledTimeBound = currentTime().minus(action.throttle!!.value.toLong(), action.throttle!!.unit)
134- // return (lastExecutionTime == null || lastExecutionTime.isBefore(throttledTimeBound))
135- // }
136- // return true
137- // }
138-
139124 // if trigger uses custom condition, append the custom condition to query, otherwise simply proceed
140125 val queryToExecute = if (pplTrigger.conditionType == ConditionType .NUMBER_OF_RESULTS ) { // number of results trigger
141126 timeFilteredQuery
@@ -147,15 +132,15 @@ object PPLMonitorRunner : MonitorV2Runner {
147132 val queryResponseJson = executePplQuery(queryToExecute, nodeClient)
148133 logger.info(" query execution results for trigger ${pplTrigger.name} : $queryResponseJson " )
149134
150- // retrieve only the relevant query response rows.
135+ // retrieve deep copies of only the relevant query response rows.
151136 // for num_results triggers, that's the entire response
152137 // for custom triggers, that's only rows that evaluated to true
153138 val relevantQueryResultRows = if (pplTrigger.conditionType == ConditionType .NUMBER_OF_RESULTS ) {
154139 // number of results trigger
155140 getQueryResponseWithoutSize(queryResponseJson)
156141 } else {
157142 // custom condition trigger
158- evaluateCustomConditionTrigger (queryResponseJson, pplTrigger)
143+ collectCustomConditionResults (queryResponseJson, pplTrigger)
159144 }
160145
161146 // retrieve the number of results
@@ -194,10 +179,6 @@ object PPLMonitorRunner : MonitorV2Runner {
194179 timeOfCurrentExecution
195180 )
196181
197- // collect the generated alerts to be written to alerts index
198- // if the trigger is on result_set mode
199- // generatedAlerts.addAll(thisTriggersGeneratedAlerts)
200-
201182 // update the trigger's last execution time for future suppression checks
202183 pplTrigger.lastTriggeredTime = timeOfCurrentExecution
203184
@@ -354,7 +335,7 @@ object PPLMonitorRunner : MonitorV2Runner {
354335 return queryResponseDeepCopy
355336 }
356337
357- private fun evaluateCustomConditionTrigger (customConditionQueryResponse : JSONObject , pplTrigger : PPLTrigger ): JSONObject {
338+ private fun collectCustomConditionResults (customConditionQueryResponse : JSONObject , pplTrigger : PPLTrigger ): JSONObject {
358339 // a PPL query with custom condition returning 0 results should imply a valid but not useful query.
359340 // do not trigger alert, but warn that query likely is not functioning as user intended
360341 if (customConditionQueryResponse.getLong(" total" ) == 0L ) {
@@ -445,7 +426,10 @@ object PPLMonitorRunner : MonitorV2Runner {
445426 alertV2s.add(alertV2)
446427 }
447428
448- return alertV2s.toList() // return as immutable list
429+ // TODO: this is a magic number right now, make it a setting
430+ val alertsLimit = 10
431+
432+ return alertV2s.take(alertsLimit).toList() // return as immutable list
449433 }
450434
451435 private fun generateErrorAlert (
@@ -488,7 +472,7 @@ object PPLMonitorRunner : MonitorV2Runner {
488472
489473 var requestsToRetry = alerts.flatMap { alert ->
490474 listOf<DocWriteRequest <* >>(
491- IndexRequest (AlertIndices . ALERT_INDEX )
475+ IndexRequest (AlertV2Indices . ALERT_V2_INDEX )
492476 .routing(pplMonitor.id) // set routing ID to PPL Monitor ID
493477 .source(alert.toXContent(XContentFactory .jsonBuilder(), ToXContent .EMPTY_PARAMS ))
494478 .id(if (alert.id != Alert .NO_ID ) alert.id else null )
0 commit comments