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

apis for get workflow alerts and acknowledge chained alerts #472

Merged
merged 3 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -11,6 +11,7 @@ import org.opensearch.common.io.stream.NamedWriteableRegistry
import org.opensearch.common.io.stream.Writeable
import org.opensearch.commons.alerting.action.AcknowledgeAlertRequest
import org.opensearch.commons.alerting.action.AcknowledgeAlertResponse
import org.opensearch.commons.alerting.action.AcknowledgeChainedAlertRequest
import org.opensearch.commons.alerting.action.AlertingActions
import org.opensearch.commons.alerting.action.DeleteMonitorRequest
import org.opensearch.commons.alerting.action.DeleteMonitorResponse
Expand All @@ -20,6 +21,8 @@ import org.opensearch.commons.alerting.action.GetAlertsRequest
import org.opensearch.commons.alerting.action.GetAlertsResponse
import org.opensearch.commons.alerting.action.GetFindingsRequest
import org.opensearch.commons.alerting.action.GetFindingsResponse
import org.opensearch.commons.alerting.action.GetWorkflowAlertsRequest
import org.opensearch.commons.alerting.action.GetWorkflowAlertsResponse
import org.opensearch.commons.alerting.action.GetWorkflowRequest
import org.opensearch.commons.alerting.action.GetWorkflowResponse
import org.opensearch.commons.alerting.action.IndexMonitorRequest
Expand Down Expand Up @@ -147,6 +150,30 @@ object AlertingPluginInterface {
)
}

/**
* Get Workflow Alerts interface.
* @param client Node client for making transport action
* @param request The request object
* @param listener The listener for getting response
*/
fun getWorkflowAlerts(
client: NodeClient,
request: GetWorkflowAlertsRequest,
listener: ActionListener<GetWorkflowAlertsResponse>
) {
client.execute(
AlertingActions.GET_WORKFLOW_ALERTS_ACTION_TYPE,
request,
wrapActionListener(listener) { response ->
recreateObject(response) {
GetWorkflowAlertsResponse(
it
)
}
}
)
}

/**
* Get Workflow interface.
* @param client Node client for making transport action
Expand Down Expand Up @@ -237,6 +264,30 @@ object AlertingPluginInterface {
)
}

/**
* Acknowledge Chained Alerts interface.
* @param client Node client for making transport action
* @param request The request object
* @param listener The listener for getting response
*/
fun acknowledgeChainedAlerts(
client: NodeClient,
request: AcknowledgeChainedAlertRequest,
listener: ActionListener<AcknowledgeAlertResponse>
) {
client.execute(
AlertingActions.ACKNOWLEDGE_CHAINED_ALERTS_ACTION_TYPE,
request,
wrapActionListener(listener) { response ->
recreateObject(response) {
AcknowledgeAlertResponse(
it
)
}
}
)
}

@Suppress("UNCHECKED_CAST")
private fun <Response : BaseResponse> wrapActionListener(
listener: ActionListener<Response>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,53 @@ object AlertingActions {
const val INDEX_MONITOR_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/write"
const val INDEX_WORKFLOW_ACTION_NAME = "cluster:admin/opensearch/alerting/workflow/write"
const val GET_ALERTS_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/get"
const val GET_WORKFLOW_ALERTS_ACTION_NAME = "cluster:admin/opensearch/alerting/workflow_alerts/get"
const val GET_WORKFLOW_ACTION_NAME = "cluster:admin/opensearch/alerting/workflow/get"
const val DELETE_MONITOR_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/delete"
const val DELETE_WORKFLOW_ACTION_NAME = "cluster:admin/opensearch/alerting/workflow/delete"
const val GET_FINDINGS_ACTION_NAME = "cluster:admin/opensearch/alerting/findings/get"
const val ACKNOWLEDGE_ALERTS_ACTION_NAME = "cluster:admin/opendistro/alerting/alerts/ack"
const val ACKNOWLEDGE_CHAINED_ALERTS_ACTION_NAME = "cluster:admin/opendistro/alerting/chained_alerts/ack"
const val SUBSCRIBE_FINDINGS_ACTION_NAME = "cluster:admin/opensearch/alerting/findings/subscribe"

@JvmField
val INDEX_MONITOR_ACTION_TYPE =
ActionType(INDEX_MONITOR_ACTION_NAME, ::IndexMonitorResponse)

@JvmField
val INDEX_WORKFLOW_ACTION_TYPE =
ActionType(INDEX_WORKFLOW_ACTION_NAME, ::IndexWorkflowResponse)
@JvmField
val GET_ALERTS_ACTION_TYPE =
ActionType(GET_ALERTS_ACTION_NAME, ::GetAlertsResponse)

@JvmField
val GET_WORKFLOW_ALERTS_ACTION_TYPE =
ActionType(GET_WORKFLOW_ALERTS_ACTION_NAME, ::GetWorkflowAlertsResponse)

@JvmField
val GET_WORKFLOW_ACTION_TYPE =
ActionType(GET_WORKFLOW_ACTION_NAME, ::GetWorkflowResponse)

@JvmField
val DELETE_MONITOR_ACTION_TYPE =
ActionType(DELETE_MONITOR_ACTION_NAME, ::DeleteMonitorResponse)

@JvmField
val DELETE_WORKFLOW_ACTION_TYPE =
ActionType(DELETE_WORKFLOW_ACTION_NAME, ::DeleteWorkflowResponse)
@JvmField
val GET_FINDINGS_ACTION_TYPE =
ActionType(GET_FINDINGS_ACTION_NAME, ::GetFindingsResponse)

@JvmField
val ACKNOWLEDGE_ALERTS_ACTION_TYPE =
ActionType(ACKNOWLEDGE_ALERTS_ACTION_NAME, ::AcknowledgeAlertResponse)

@JvmField
val SUBSCRIBE_FINDINGS_ACTION_TYPE =
ActionType(SUBSCRIBE_FINDINGS_ACTION_NAME, ::SubscribeFindingsResponse)

val ACKNOWLEDGE_CHAINED_ALERTS_ACTION_TYPE =
ActionType(ACKNOWLEDGE_CHAINED_ALERTS_ACTION_NAME, ::AcknowledgeAlertResponse)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing @JvmField annotation?

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class GetAlertsRequest : ActionRequest {
val monitorId: String?
val alertIndex: String?
val monitorIds: List<String>?
val workflowIds: List<String>?
val alertIds: List<String>?

constructor(
Expand All @@ -23,6 +24,7 @@ class GetAlertsRequest : ActionRequest {
monitorId: String?,
alertIndex: String?,
monitorIds: List<String>? = null,
workflowIds: List<String>? = null,
alertIds: List<String>? = null
) : super() {
this.table = table
Expand All @@ -31,6 +33,7 @@ class GetAlertsRequest : ActionRequest {
this.monitorId = monitorId
this.alertIndex = alertIndex
this.monitorIds = monitorIds
this.workflowIds = workflowIds
this.alertIds = alertIds
}

Expand All @@ -42,6 +45,7 @@ class GetAlertsRequest : ActionRequest {
monitorId = sin.readOptionalString(),
alertIndex = sin.readOptionalString(),
monitorIds = sin.readOptionalStringList(),
workflowIds = sin.readOptionalStringList(),
alertIds = sin.readOptionalStringList()
)

Expand All @@ -57,6 +61,7 @@ class GetAlertsRequest : ActionRequest {
out.writeOptionalString(monitorId)
out.writeOptionalString(alertIndex)
out.writeOptionalStringCollection(monitorIds)
out.writeOptionalStringCollection(workflowIds)
out.writeOptionalStringCollection(alertIds)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.opensearch.commons.alerting.action

import org.opensearch.action.ActionRequest
import org.opensearch.action.ActionRequestValidationException
import org.opensearch.common.io.stream.StreamInput
import org.opensearch.common.io.stream.StreamOutput
import org.opensearch.commons.alerting.model.Table
import java.io.IOException

class GetWorkflowAlertsRequest : ActionRequest {
val table: Table
val severityLevel: String
val alertState: String
val alertIndex: String?
val monitorIds: List<String>?
val workflowIds: List<String>?
val alertIds: List<String>?
val getAssociatedAlerts: Boolean

constructor(
table: Table,
severityLevel: String,
alertState: String,
alertIndex: String?,
monitorIds: List<String>? = null,
workflowIds: List<String>? = null,
alertIds: List<String>? = null,
getAssociatedAlerts: Boolean
) : super() {
this.table = table
this.severityLevel = severityLevel
this.alertState = alertState
this.alertIndex = alertIndex
this.monitorIds = monitorIds
this.workflowIds = workflowIds
this.alertIds = alertIds
this.getAssociatedAlerts = getAssociatedAlerts
}

@Throws(IOException::class)
constructor(sin: StreamInput) : this(
table = Table.readFrom(sin),
severityLevel = sin.readString(),
alertState = sin.readString(),
alertIndex = sin.readOptionalString(),
monitorIds = sin.readOptionalStringList(),
workflowIds = sin.readOptionalStringList(),
alertIds = sin.readOptionalStringList(),
getAssociatedAlerts = sin.readBoolean()
)

override fun validate(): ActionRequestValidationException? {
return null
}

@Throws(IOException::class)
override fun writeTo(out: StreamOutput) {
table.writeTo(out)
out.writeString(severityLevel)
out.writeString(alertState)
out.writeOptionalString(alertIndex)
out.writeOptionalStringCollection(monitorIds)
out.writeOptionalStringCollection(workflowIds)
out.writeOptionalStringCollection(alertIds)
out.writeBoolean(getAssociatedAlerts)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.opensearch.commons.alerting.action

import org.opensearch.common.io.stream.StreamInput
import org.opensearch.common.io.stream.StreamOutput
import org.opensearch.commons.alerting.model.Alert
import org.opensearch.commons.notifications.action.BaseResponse
import org.opensearch.core.xcontent.ToXContent
import org.opensearch.core.xcontent.XContentBuilder
import java.io.IOException
import java.util.Collections

class GetWorkflowAlertsResponse : BaseResponse {
val alerts: List<Alert>
val associatedAlerts: List<Alert>
// totalAlerts is not the same as the size of alerts because there can be 30 alerts from the request, but
// the request only asked for 5 alerts, so totalAlerts will be 30, but alerts will only contain 5 alerts
val totalAlerts: Int?

constructor(
alerts: List<Alert>,
associatedAlerts: List<Alert>,
totalAlerts: Int?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is totalAlerts nullable?

Copy link
Member Author

@eirsep eirsep Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a parallel api to get alerts. Keeping it consistent with the response structure of GetAlertsResponse which has totalAlerts as nullable

https://github.com/opensearch-project/common-utils/blob/main/src/main/kotlin/org/opensearch/commons/alerting/action/GetAlertsResponse.kt#L17

) : super() {
this.alerts = alerts
this.associatedAlerts = associatedAlerts
this.totalAlerts = totalAlerts
}

@Throws(IOException::class)
constructor(sin: StreamInput) : this(
alerts = Collections.unmodifiableList(sin.readList(::Alert)),
associatedAlerts = Collections.unmodifiableList(sin.readList(::Alert)),
totalAlerts = sin.readOptionalInt()
)

@Throws(IOException::class)
override fun writeTo(out: StreamOutput) {
out.writeCollection(alerts)
out.writeCollection(associatedAlerts)
out.writeOptionalInt(totalAlerts)
}

@Throws(IOException::class)
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder {
builder.startObject()
.field("alerts", alerts)
.field("associatedAlerts", associatedAlerts)
.field("totalAlerts", totalAlerts)
return builder.endObject()
}
}
Loading