Skip to content

Commit 819599f

Browse files
PPL Alerting: Get and Search Monitors (#1966)
* PPL Alerting: Get and Search Monitors Signed-off-by: Dennis Toepker <[email protected]> * adding requested ID Signed-off-by: Dennis Toepker <[email protected]> --------- Signed-off-by: Dennis Toepker <[email protected]> Co-authored-by: Dennis Toepker <[email protected]>
1 parent c911cfa commit 819599f

File tree

13 files changed

+1592
-0
lines changed

13 files changed

+1592
-0
lines changed

alerting/src/main/kotlin/org/opensearch/alerting/AlertingPlugin.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import org.opensearch.alerting.action.GetEmailGroupAction
1414
import org.opensearch.alerting.action.GetRemoteIndexesAction
1515
import org.opensearch.alerting.action.SearchEmailAccountAction
1616
import org.opensearch.alerting.action.SearchEmailGroupAction
17+
import org.opensearch.alerting.actionv2.GetMonitorV2Action
1718
import org.opensearch.alerting.actionv2.IndexMonitorV2Action
19+
import org.opensearch.alerting.actionv2.SearchMonitorV2Action
1820
import org.opensearch.alerting.alerts.AlertIndices
1921
import org.opensearch.alerting.alerts.AlertIndices.Companion.ALL_ALERT_INDEX_PATTERN
2022
import org.opensearch.alerting.comments.CommentsIndices
@@ -53,7 +55,9 @@ import org.opensearch.alerting.resthandler.RestSearchAlertingCommentAction
5355
import org.opensearch.alerting.resthandler.RestSearchEmailAccountAction
5456
import org.opensearch.alerting.resthandler.RestSearchEmailGroupAction
5557
import org.opensearch.alerting.resthandler.RestSearchMonitorAction
58+
import org.opensearch.alerting.resthandlerv2.RestGetMonitorV2Action
5659
import org.opensearch.alerting.resthandlerv2.RestIndexMonitorV2Action
60+
import org.opensearch.alerting.resthandlerv2.RestSearchMonitorV2Action
5761
import org.opensearch.alerting.script.TriggerScript
5862
import org.opensearch.alerting.service.DeleteMonitorService
5963
import org.opensearch.alerting.settings.AlertingSettings
@@ -86,7 +90,9 @@ import org.opensearch.alerting.transport.TransportSearchAlertingCommentAction
8690
import org.opensearch.alerting.transport.TransportSearchEmailAccountAction
8791
import org.opensearch.alerting.transport.TransportSearchEmailGroupAction
8892
import org.opensearch.alerting.transport.TransportSearchMonitorAction
93+
import org.opensearch.alerting.transportv2.TransportGetMonitorV2Action
8994
import org.opensearch.alerting.transportv2.TransportIndexMonitorV2Action
95+
import org.opensearch.alerting.transportv2.TransportSearchMonitorV2Action
9096
import org.opensearch.alerting.util.DocLevelMonitorQueries
9197
import org.opensearch.alerting.util.destinationmigration.DestinationMigrationCoordinator
9298
import org.opensearch.cluster.metadata.IndexNameExpressionResolver
@@ -227,6 +233,8 @@ internal class AlertingPlugin : PainlessExtension, ActionPlugin, ScriptPlugin, R
227233

228234
// Alerting V2
229235
RestIndexMonitorV2Action(),
236+
RestGetMonitorV2Action(),
237+
RestSearchMonitorV2Action(settings, clusterService),
230238
)
231239
}
232240

@@ -263,6 +271,8 @@ internal class AlertingPlugin : PainlessExtension, ActionPlugin, ScriptPlugin, R
263271

264272
// Alerting V2
265273
ActionPlugin.ActionHandler(IndexMonitorV2Action.INSTANCE, TransportIndexMonitorV2Action::class.java),
274+
ActionPlugin.ActionHandler(GetMonitorV2Action.INSTANCE, TransportGetMonitorV2Action::class.java),
275+
ActionPlugin.ActionHandler(SearchMonitorV2Action.INSTANCE, TransportSearchMonitorV2Action::class.java),
266276
)
267277
}
268278

alerting/src/main/kotlin/org/opensearch/alerting/PPLUtils.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
package org.opensearch.alerting
27

38
import org.json.JSONArray
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.alerting.actionv2
7+
8+
import org.opensearch.action.ActionType
9+
10+
class GetMonitorV2Action private constructor() : ActionType<GetMonitorV2Response>(NAME, ::GetMonitorV2Response) {
11+
companion object {
12+
val INSTANCE = GetMonitorV2Action()
13+
const val NAME = "cluster:admin/opensearch/alerting/v2/monitor/get"
14+
}
15+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.alerting.actionv2
7+
8+
import org.opensearch.action.ActionRequest
9+
import org.opensearch.action.ActionRequestValidationException
10+
import org.opensearch.core.common.io.stream.StreamInput
11+
import org.opensearch.core.common.io.stream.StreamOutput
12+
import org.opensearch.search.fetch.subphase.FetchSourceContext
13+
import java.io.IOException
14+
15+
class GetMonitorV2Request : ActionRequest {
16+
val monitorV2Id: String
17+
val version: Long
18+
val srcContext: FetchSourceContext?
19+
20+
constructor(
21+
monitorV2Id: String,
22+
version: Long,
23+
srcContext: FetchSourceContext?
24+
) : super() {
25+
this.monitorV2Id = monitorV2Id
26+
this.version = version
27+
this.srcContext = srcContext
28+
}
29+
30+
@Throws(IOException::class)
31+
constructor(sin: StreamInput) : this(
32+
sin.readString(), // monitorV2Id
33+
sin.readLong(), // version
34+
if (sin.readBoolean()) {
35+
FetchSourceContext(sin) // srcContext
36+
} else {
37+
null
38+
}
39+
)
40+
41+
override fun validate(): ActionRequestValidationException? {
42+
return null
43+
}
44+
45+
@Throws(IOException::class)
46+
override fun writeTo(out: StreamOutput) {
47+
out.writeString(monitorV2Id)
48+
out.writeLong(version)
49+
out.writeBoolean(srcContext != null)
50+
srcContext?.writeTo(out)
51+
}
52+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.alerting.actionv2
7+
8+
import org.opensearch.alerting.modelv2.MonitorV2
9+
import org.opensearch.commons.alerting.util.IndexUtils.Companion._ID
10+
import org.opensearch.commons.alerting.util.IndexUtils.Companion._PRIMARY_TERM
11+
import org.opensearch.commons.alerting.util.IndexUtils.Companion._SEQ_NO
12+
import org.opensearch.commons.alerting.util.IndexUtils.Companion._VERSION
13+
import org.opensearch.commons.notifications.action.BaseResponse
14+
import org.opensearch.core.common.io.stream.StreamInput
15+
import org.opensearch.core.common.io.stream.StreamOutput
16+
import org.opensearch.core.xcontent.ToXContent
17+
import org.opensearch.core.xcontent.XContentBuilder
18+
import java.io.IOException
19+
20+
class GetMonitorV2Response : BaseResponse {
21+
var id: String
22+
var version: Long
23+
var seqNo: Long
24+
var primaryTerm: Long
25+
var monitorV2: MonitorV2?
26+
27+
constructor(
28+
id: String,
29+
version: Long,
30+
seqNo: Long,
31+
primaryTerm: Long,
32+
monitorV2: MonitorV2?
33+
) : super() {
34+
this.id = id
35+
this.version = version
36+
this.seqNo = seqNo
37+
this.primaryTerm = primaryTerm
38+
this.monitorV2 = monitorV2
39+
}
40+
41+
@Throws(IOException::class)
42+
constructor(sin: StreamInput) : this(
43+
id = sin.readString(), // id
44+
version = sin.readLong(), // version
45+
seqNo = sin.readLong(), // seqNo
46+
primaryTerm = sin.readLong(), // primaryTerm
47+
monitorV2 = if (sin.readBoolean()) {
48+
MonitorV2.readFrom(sin) // monitorV2
49+
} else {
50+
null
51+
}
52+
)
53+
54+
@Throws(IOException::class)
55+
override fun writeTo(out: StreamOutput) {
56+
out.writeString(id)
57+
out.writeLong(version)
58+
out.writeLong(seqNo)
59+
out.writeLong(primaryTerm)
60+
if (monitorV2 != null) {
61+
out.writeBoolean(true)
62+
monitorV2?.writeTo(out)
63+
} else {
64+
out.writeBoolean(false)
65+
}
66+
}
67+
68+
@Throws(IOException::class)
69+
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder {
70+
builder.startObject()
71+
.field(_ID, id)
72+
.field(_VERSION, version)
73+
.field(_SEQ_NO, seqNo)
74+
.field(_PRIMARY_TERM, primaryTerm)
75+
if (monitorV2 != null) {
76+
builder.field("monitorV2", monitorV2)
77+
}
78+
return builder.endObject()
79+
}
80+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.alerting.actionv2
7+
8+
import org.opensearch.action.ActionType
9+
import org.opensearch.action.search.SearchResponse
10+
11+
class SearchMonitorV2Action private constructor() : ActionType<SearchResponse>(NAME, ::SearchResponse) {
12+
companion object {
13+
val INSTANCE = SearchMonitorV2Action()
14+
const val NAME = "cluster:admin/opensearch/alerting/v2/monitor/search"
15+
}
16+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.alerting.actionv2
7+
8+
import org.opensearch.action.ActionRequest
9+
import org.opensearch.action.ActionRequestValidationException
10+
import org.opensearch.action.search.SearchRequest
11+
import org.opensearch.core.common.io.stream.StreamInput
12+
import org.opensearch.core.common.io.stream.StreamOutput
13+
import java.io.IOException
14+
15+
class SearchMonitorV2Request : ActionRequest {
16+
val searchRequest: SearchRequest
17+
18+
constructor(
19+
searchRequest: SearchRequest
20+
) : super() {
21+
this.searchRequest = searchRequest
22+
}
23+
24+
@Throws(IOException::class)
25+
constructor(sin: StreamInput) : this(
26+
searchRequest = SearchRequest(sin)
27+
)
28+
29+
override fun validate(): ActionRequestValidationException? {
30+
return null
31+
}
32+
33+
@Throws(IOException::class)
34+
override fun writeTo(out: StreamOutput) {
35+
searchRequest.writeTo(out)
36+
}
37+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.alerting.resthandlerv2
7+
8+
import org.apache.logging.log4j.LogManager
9+
import org.opensearch.alerting.AlertingPlugin
10+
import org.opensearch.alerting.actionv2.GetMonitorV2Action
11+
import org.opensearch.alerting.actionv2.GetMonitorV2Request
12+
import org.opensearch.alerting.modelv2.MonitorV2.Companion.UUID_LENGTH
13+
import org.opensearch.alerting.util.context
14+
import org.opensearch.rest.BaseRestHandler
15+
import org.opensearch.rest.RestHandler.Route
16+
import org.opensearch.rest.RestRequest
17+
import org.opensearch.rest.RestRequest.Method.GET
18+
import org.opensearch.rest.RestRequest.Method.HEAD
19+
import org.opensearch.rest.action.RestActions
20+
import org.opensearch.rest.action.RestToXContentListener
21+
import org.opensearch.search.fetch.subphase.FetchSourceContext
22+
import org.opensearch.transport.client.node.NodeClient
23+
24+
private val log = LogManager.getLogger(RestGetMonitorV2Action::class.java)
25+
26+
class RestGetMonitorV2Action : BaseRestHandler() {
27+
28+
override fun getName(): String {
29+
return "get_monitor_v2_action"
30+
}
31+
32+
override fun routes(): List<Route> {
33+
return listOf(
34+
Route(
35+
GET,
36+
"${AlertingPlugin.MONITOR_V2_BASE_URI}/{monitorV2Id}"
37+
),
38+
Route(
39+
HEAD,
40+
"${AlertingPlugin.MONITOR_V2_BASE_URI}/{monitorV2Id}"
41+
)
42+
)
43+
}
44+
45+
override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer {
46+
log.debug("${request.method()} ${AlertingPlugin.MONITOR_V2_BASE_URI}/{monitorV2Id}")
47+
48+
val monitorV2Id = request.param("monitorV2Id")
49+
if (monitorV2Id == null || monitorV2Id.isEmpty()) {
50+
throw IllegalArgumentException("No MonitorV2 ID provided")
51+
}
52+
53+
if (monitorV2Id.length != UUID_LENGTH) {
54+
throw IllegalArgumentException("MonitorV2 ID provided does not have correct length")
55+
}
56+
57+
var srcContext = context(request)
58+
if (request.method() == HEAD) {
59+
srcContext = FetchSourceContext.DO_NOT_FETCH_SOURCE
60+
}
61+
62+
val getMonitorV2Request = GetMonitorV2Request(monitorV2Id, RestActions.parseVersion(request), srcContext)
63+
return RestChannelConsumer {
64+
channel ->
65+
client.execute(GetMonitorV2Action.INSTANCE, getMonitorV2Request, RestToXContentListener(channel))
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)