forked from opensearch-project/alerting
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored alert generation for cluster metrics monitors to include t…
…riggered clusters in alerts. Signed-off-by: AWSHurneyt <[email protected]>
- Loading branch information
1 parent
affb7aa
commit 09abfbf
Showing
5 changed files
with
196 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
alerting/src/main/kotlin/org/opensearch/alerting/model/ClusterMetricsTriggerRunResult.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.alerting.model | ||
|
||
import org.opensearch.common.io.stream.StreamInput | ||
import org.opensearch.common.io.stream.StreamOutput | ||
import org.opensearch.common.io.stream.Writeable | ||
import org.opensearch.commons.alerting.alerts.AlertError | ||
import org.opensearch.core.xcontent.ToXContent | ||
import org.opensearch.core.xcontent.ToXContentObject | ||
import org.opensearch.core.xcontent.XContentBuilder | ||
import org.opensearch.script.ScriptException | ||
import java.io.IOException | ||
import java.time.Instant | ||
|
||
data class ClusterMetricsTriggerRunResult( | ||
override var triggerName: String, | ||
override var triggered: Boolean, | ||
override var error: Exception?, | ||
override var actionResults: MutableMap<String, ActionRunResult> = mutableMapOf(), | ||
var clusterTriggerResults: List<ClusterTriggerResult> = listOf() | ||
) : QueryLevelTriggerRunResult( | ||
triggerName = triggerName, | ||
error = error, | ||
triggered = triggered, | ||
actionResults = actionResults | ||
) { | ||
|
||
@Throws(IOException::class) | ||
@Suppress("UNCHECKED_CAST") | ||
constructor(sin: StreamInput) : this( | ||
triggerName = sin.readString(), | ||
error = sin.readException(), | ||
triggered = sin.readBoolean(), | ||
actionResults = sin.readMap() as MutableMap<String, ActionRunResult>, | ||
clusterTriggerResults = sin.readList((ClusterTriggerResult.Companion)::readFrom) | ||
) | ||
|
||
override fun alertError(): AlertError? { | ||
if (error != null) { | ||
return AlertError(Instant.now(), "Failed evaluating trigger:\n${error!!.userErrorMessage()}") | ||
} | ||
for (actionResult in actionResults.values) { | ||
if (actionResult.error != null) { | ||
return AlertError(Instant.now(), "Failed running action:\n${actionResult.error.userErrorMessage()}") | ||
} | ||
} | ||
return null | ||
} | ||
|
||
override fun internalXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { | ||
if (error is ScriptException) error = Exception((error as ScriptException).toJsonString(), error) | ||
return builder | ||
.field(TRIGGERED_FIELD, triggered) | ||
.field(ACTION_RESULTS_FIELD, actionResults as Map<String, ActionRunResult>) | ||
.field(CLUSTER_RESULTS_FIELD, clusterTriggerResults) | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun writeTo(out: StreamOutput) { | ||
super.writeTo(out) | ||
out.writeBoolean(triggered) | ||
out.writeMap(actionResults as Map<String, ActionRunResult>) | ||
clusterTriggerResults.forEach { it.writeTo(out) } | ||
} | ||
|
||
companion object { | ||
const val TRIGGERED_FIELD = "triggered" | ||
const val ACTION_RESULTS_FIELD = "action_results" | ||
const val CLUSTER_RESULTS_FIELD = "cluster_results" | ||
} | ||
|
||
data class ClusterTriggerResult( | ||
val cluster: String, | ||
val triggered: Boolean, | ||
) : ToXContentObject, Writeable { | ||
|
||
@Throws(IOException::class) | ||
constructor(sin: StreamInput) : this( | ||
cluster = sin.readString(), | ||
triggered = sin.readBoolean() | ||
) | ||
|
||
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { | ||
return builder.startObject(cluster) | ||
.field(TRIGGERED_FIELD, triggered) | ||
.endObject() | ||
} | ||
|
||
override fun writeTo(out: StreamOutput) { | ||
out.writeString(cluster) | ||
out.writeBoolean(triggered) | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
@Throws(IOException::class) | ||
fun readFrom(sin: StreamInput): ClusterTriggerResult { | ||
return ClusterTriggerResult(sin) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters