@@ -46,6 +46,7 @@ import org.opensearch.script.ScriptType
46
46
import org.opensearch.script.TemplateScript
47
47
import org.opensearch.search.aggregations.AggregatorFactories
48
48
import org.opensearch.search.aggregations.bucket.composite.CompositeAggregationBuilder
49
+ import org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder
49
50
import org.opensearch.search.builder.SearchSourceBuilder
50
51
import java.time.Instant
51
52
import java.util.UUID
@@ -71,6 +72,9 @@ object BucketLevelMonitorRunner : MonitorRunner() {
71
72
val currentAlerts = try {
72
73
monitorCtx.alertIndices!! .createOrUpdateAlertIndex(monitor.dataSources)
73
74
monitorCtx.alertIndices!! .createOrUpdateInitialAlertHistoryIndex(monitor.dataSources)
75
+ if (monitor.dataSources.findingsEnabled == true ) {
76
+ monitorCtx.alertIndices!! .createOrUpdateInitialFindingHistoryIndex(monitor.dataSources)
77
+ }
74
78
monitorCtx.alertService!! .loadCurrentAlertsForBucketLevelMonitor(monitor)
75
79
} catch (e: Exception ) {
76
80
// We can't save ERROR alerts to the index here as we don't know if there are existing ACTIVE alerts
@@ -142,15 +146,19 @@ object BucketLevelMonitorRunner : MonitorRunner() {
142
146
*/
143
147
if (triggerResults[trigger.id]?.error != null ) continue
144
148
val findings =
145
- if (monitor.triggers.size == 1 && monitor.dataSources.findingsEnabled == true ) createFindings(
146
- triggerResult,
147
- monitor,
148
- monitorCtx,
149
- periodStart,
150
- periodEnd,
151
- ! dryrun && monitor.id != Monitor .NO_ID
152
- )
153
- else emptyList()
149
+ if (monitor.triggers.size == 1 && monitor.dataSources.findingsEnabled == true ) {
150
+ logger.debug(" Creating bucket level findings" )
151
+ createFindings(
152
+ triggerResult,
153
+ monitor,
154
+ monitorCtx,
155
+ periodStart,
156
+ periodEnd,
157
+ ! dryrun && monitor.id != Monitor .NO_ID
158
+ )
159
+ } else {
160
+ emptyList()
161
+ }
154
162
// TODO: Should triggerResult's aggregationResultBucket be a list? If not, getCategorizedAlertsForBucketLevelMonitor can
155
163
// be refactored to use a map instead
156
164
val categorizedAlerts = monitorCtx.alertService!! .getCategorizedAlertsForBucketLevelMonitor(
@@ -334,15 +342,30 @@ object BucketLevelMonitorRunner : MonitorRunner() {
334
342
val bucketValues: Set <String > = triggerResult.aggregationResultBuckets.keys
335
343
val query = input.query
336
344
var fieldName = " "
337
- var grouByFields = 0 // if number of fields used to group by > 1 we won't calculate findings
345
+
338
346
for (aggFactory in (query.aggregations() as AggregatorFactories .Builder ).aggregatorFactories) {
339
- val sources = (aggFactory as CompositeAggregationBuilder ).sources()
340
- for (source in sources) {
341
- if (grouByFields > 0 ) {
347
+ when (aggFactory) {
348
+ is CompositeAggregationBuilder -> {
349
+ var grouByFields = 0 // if number of fields used to group by > 1 we won't calculate findings
350
+ val sources = aggFactory.sources()
351
+ for (source in sources) {
352
+ if (grouByFields > 0 ) {
353
+ logger.error(" grouByFields > 0. not generating findings for bucket level monitor ${monitor.id} " )
354
+ return listOf ()
355
+ }
356
+ grouByFields++
357
+ fieldName = source.field()
358
+ }
359
+ }
360
+ is TermsAggregationBuilder -> {
361
+ fieldName = aggFactory.field()
362
+ }
363
+ else -> {
364
+ logger.error(
365
+ " Bucket level monitor findings supported only for composite and term aggs. Found [{${aggFactory.type} }]"
366
+ )
342
367
return listOf ()
343
368
}
344
- grouByFields++
345
- fieldName = source.field()
346
369
}
347
370
}
348
371
if (fieldName != " " ) {
@@ -370,6 +393,8 @@ object BucketLevelMonitorRunner : MonitorRunner() {
370
393
}
371
394
val searchResponse: SearchResponse = monitorCtx.client!! .suspendUntil { monitorCtx.client!! .search(sr, it) }
372
395
return createFindingPerIndex(searchResponse, monitor, monitorCtx, shouldCreateFinding)
396
+ } else {
397
+ logger.error(" Couldn't resolve groupBy field. Not generating bucket level monitor findings for monitor %${monitor.id} " )
373
398
}
374
399
}
375
400
}
@@ -403,8 +428,9 @@ object BucketLevelMonitorRunner : MonitorRunner() {
403
428
)
404
429
405
430
val findingStr = finding.toXContent(XContentBuilder .builder(XContentType .JSON .xContent()), ToXContent .EMPTY_PARAMS ).string()
406
- logger.debug(" Findings: $findingStr " )
431
+ logger.debug(" Bucket level monitor ${monitor.id} Findings: $findingStr " )
407
432
if (shouldCreateFinding) {
433
+ logger.debug(" Saving bucket level monitor findings for monitor ${monitor.id} " )
408
434
val indexRequest = IndexRequest (monitor.dataSources.findingsIndex)
409
435
.source(findingStr, XContentType .JSON )
410
436
.id(finding.id)
0 commit comments