Skip to content

Commit bb6ade2

Browse files
opensearch-trigger-bot[bot]eirsep
authored andcommitted
add workflowId field in alert (opensearch-project#463) (opensearch-project#464)
Signed-off-by: Surya Sashank Nistala <[email protected]> (cherry picked from commit 110bf01) Co-authored-by: Surya Sashank Nistala <[email protected]> Signed-off-by: AWSHurneyt <[email protected]>
1 parent 61ea1f7 commit bb6ade2

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

Diff for: src/main/kotlin/org/opensearch/commons/alerting/model/Alert.kt

+26-12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ data class Alert(
2222
val version: Long = NO_VERSION,
2323
val schemaVersion: Int = NO_SCHEMA_VERSION,
2424
val monitorId: String,
25+
val workflowId: String,
2526
val monitorName: String,
2627
val monitorVersion: Long,
2728
val monitorUser: User?,
@@ -48,6 +49,7 @@ data class Alert(
4849
}
4950
}
5051

52+
// constructor for chained alerts.
5153
constructor(
5254
startTime: Instant,
5355
lastNotificationTime: Instant?,
@@ -63,7 +65,7 @@ data class Alert(
6365
lastNotificationTime = lastNotificationTime, errorMessage = errorMessage, errorHistory = emptyList(),
6466
severity = chainedAlertTrigger.severity, actionExecutionResults = emptyList(), schemaVersion = schemaVersion,
6567
aggregationResultBucket = null, findingIds = emptyList(), relatedDocIds = emptyList(),
66-
executionId = executionId
68+
executionId = executionId, workflowId = workflow.id
6769
)
6870

6971
constructor(
@@ -76,14 +78,15 @@ data class Alert(
7678
errorHistory: List<AlertError> = mutableListOf(),
7779
actionExecutionResults: List<ActionExecutionResult> = mutableListOf(),
7880
schemaVersion: Int = NO_SCHEMA_VERSION,
79-
executionId: String? = null
81+
executionId: String? = null,
82+
workflow: Workflow? = null,
8083
) : this(
8184
monitorId = monitor.id, monitorName = monitor.name, monitorVersion = monitor.version, monitorUser = monitor.user,
8285
triggerId = trigger.id, triggerName = trigger.name, state = state, startTime = startTime,
8386
lastNotificationTime = lastNotificationTime, errorMessage = errorMessage, errorHistory = errorHistory,
8487
severity = trigger.severity, actionExecutionResults = actionExecutionResults, schemaVersion = schemaVersion,
8588
aggregationResultBucket = null, findingIds = emptyList(), relatedDocIds = emptyList(),
86-
executionId = executionId
89+
executionId = executionId, workflowId = workflow?.id ?: ""
8790
)
8891

8992
constructor(
@@ -97,14 +100,15 @@ data class Alert(
97100
actionExecutionResults: List<ActionExecutionResult> = mutableListOf(),
98101
schemaVersion: Int = NO_SCHEMA_VERSION,
99102
findingIds: List<String> = emptyList(),
100-
executionId: String? = null
103+
executionId: String? = null,
104+
workflow: Workflow? = null,
101105
) : this(
102106
monitorId = monitor.id, monitorName = monitor.name, monitorVersion = monitor.version, monitorUser = monitor.user,
103107
triggerId = trigger.id, triggerName = trigger.name, state = state, startTime = startTime,
104108
lastNotificationTime = lastNotificationTime, errorMessage = errorMessage, errorHistory = errorHistory,
105109
severity = trigger.severity, actionExecutionResults = actionExecutionResults, schemaVersion = schemaVersion,
106110
aggregationResultBucket = null, findingIds = findingIds, relatedDocIds = emptyList(),
107-
executionId = executionId
111+
executionId = executionId, workflowId = workflow?.id ?: ""
108112
)
109113

110114
constructor(
@@ -119,14 +123,15 @@ data class Alert(
119123
schemaVersion: Int = NO_SCHEMA_VERSION,
120124
aggregationResultBucket: AggregationResultBucket,
121125
findingIds: List<String> = emptyList(),
122-
executionId: String? = null
126+
executionId: String? = null,
127+
workflow: Workflow? = null,
123128
) : this(
124129
monitorId = monitor.id, monitorName = monitor.name, monitorVersion = monitor.version, monitorUser = monitor.user,
125130
triggerId = trigger.id, triggerName = trigger.name, state = state, startTime = startTime,
126131
lastNotificationTime = lastNotificationTime, errorMessage = errorMessage, errorHistory = errorHistory,
127132
severity = trigger.severity, actionExecutionResults = actionExecutionResults, schemaVersion = schemaVersion,
128133
aggregationResultBucket = aggregationResultBucket, findingIds = findingIds, relatedDocIds = emptyList(),
129-
executionId = executionId
134+
executionId = executionId, workflowId = workflow?.id ?: ""
130135
)
131136

132137
constructor(
@@ -142,14 +147,15 @@ data class Alert(
142147
errorHistory: List<AlertError> = mutableListOf(),
143148
actionExecutionResults: List<ActionExecutionResult> = mutableListOf(),
144149
schemaVersion: Int = NO_SCHEMA_VERSION,
145-
executionId: String? = null
150+
executionId: String? = null,
151+
workflow: Workflow? = null,
146152
) : this(
147153
id = id, monitorId = monitor.id, monitorName = monitor.name, monitorVersion = monitor.version, monitorUser = monitor.user,
148154
triggerId = trigger.id, triggerName = trigger.name, state = state, startTime = startTime,
149155
lastNotificationTime = lastNotificationTime, errorMessage = errorMessage, errorHistory = errorHistory,
150156
severity = trigger.severity, actionExecutionResults = actionExecutionResults, schemaVersion = schemaVersion,
151157
aggregationResultBucket = null, findingIds = findingIds, relatedDocIds = relatedDocIds,
152-
executionId = executionId
158+
executionId = executionId, workflowId = workflow?.id ?: ""
153159
)
154160

155161
constructor(
@@ -161,13 +167,15 @@ data class Alert(
161167
state: State = State.ERROR,
162168
errorMessage: String,
163169
errorHistory: List<AlertError> = mutableListOf(),
164-
schemaVersion: Int = NO_SCHEMA_VERSION
170+
schemaVersion: Int = NO_SCHEMA_VERSION,
171+
workflow: Workflow? = null,
165172
) : this(
166173
id = id, monitorId = monitor.id, monitorName = monitor.name, monitorVersion = monitor.version, monitorUser = monitor.user,
167174
triggerId = trigger.id, triggerName = trigger.name, state = state, startTime = startTime,
168175
lastNotificationTime = lastNotificationTime, errorMessage = errorMessage, errorHistory = errorHistory,
169176
severity = trigger.severity, actionExecutionResults = listOf(), schemaVersion = schemaVersion,
170-
aggregationResultBucket = null, findingIds = listOf(), relatedDocIds = listOf()
177+
aggregationResultBucket = null, findingIds = listOf(), relatedDocIds = listOf(),
178+
workflowId = workflow?.id ?: ""
171179
)
172180

173181
enum class State {
@@ -185,6 +193,7 @@ data class Alert(
185193
version = sin.readLong(),
186194
schemaVersion = sin.readInt(),
187195
monitorId = sin.readString(),
196+
workflowId = sin.readString(),
188197
monitorName = sin.readString(),
189198
monitorVersion = sin.readLong(),
190199
monitorUser = if (sin.readBoolean()) {
@@ -215,6 +224,7 @@ data class Alert(
215224
out.writeLong(version)
216225
out.writeInt(schemaVersion)
217226
out.writeString(monitorId)
227+
out.writeString(workflowId)
218228
out.writeString(monitorName)
219229
out.writeLong(monitorVersion)
220230
out.writeBoolean(monitorUser != null)
@@ -247,6 +257,7 @@ data class Alert(
247257
const val SCHEMA_VERSION_FIELD = "schema_version"
248258
const val ALERT_VERSION_FIELD = "version"
249259
const val MONITOR_ID_FIELD = "monitor_id"
260+
const val WORKFLOW_ID_FIELD = "workflow_id"
250261
const val MONITOR_VERSION_FIELD = "monitor_version"
251262
const val MONITOR_NAME_FIELD = "monitor_name"
252263
const val MONITOR_USER_FIELD = "monitor_user"
@@ -274,6 +285,7 @@ data class Alert(
274285
@Throws(IOException::class)
275286
fun parse(xcp: XContentParser, id: String = NO_ID, version: Long = NO_VERSION): Alert {
276287
lateinit var monitorId: String
288+
var workflowId = ""
277289
var schemaVersion = NO_SCHEMA_VERSION
278290
lateinit var monitorName: String
279291
var monitorVersion: Long = Versions.NOT_FOUND
@@ -300,6 +312,7 @@ data class Alert(
300312

301313
when (fieldName) {
302314
MONITOR_ID_FIELD -> monitorId = xcp.text()
315+
WORKFLOW_ID_FIELD -> workflowId = xcp.text()
303316
SCHEMA_VERSION_FIELD -> schemaVersion = xcp.intValue()
304317
MONITOR_NAME_FIELD -> monitorName = xcp.text()
305318
MONITOR_VERSION_FIELD -> monitorVersion = xcp.longValue()
@@ -360,7 +373,7 @@ data class Alert(
360373
lastNotificationTime = lastNotificationTime, acknowledgedTime = acknowledgedTime,
361374
errorMessage = errorMessage, errorHistory = errorHistory, severity = severity,
362375
actionExecutionResults = actionExecutionResults, aggregationResultBucket = aggAlertBucket, findingIds = findingIds,
363-
relatedDocIds = relatedDocIds, executionId = executionId
376+
relatedDocIds = relatedDocIds, executionId = executionId, workflowId = workflowId
364377
)
365378
}
366379

@@ -383,6 +396,7 @@ data class Alert(
383396
.field(ALERT_ID_FIELD, id)
384397
.field(ALERT_VERSION_FIELD, version)
385398
.field(MONITOR_ID_FIELD, monitorId)
399+
.field(WORKFLOW_ID_FIELD, workflowId)
386400
.field(SCHEMA_VERSION_FIELD, schemaVersion)
387401
.field(MONITOR_VERSION_FIELD, monitorVersion)
388402
.field(MONITOR_NAME_FIELD, monitorName)

Diff for: src/test/kotlin/org/opensearch/commons/alerting/AlertTests.kt

+1
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,6 @@ class AlertTests {
7777
val alert = randomChainedAlert(workflow = workflow, trigger = trigger)
7878
assertEquals(alert.monitorId, "")
7979
assertEquals(alert.id, "")
80+
assertEquals(workflow.id, alert.workflowId)
8081
}
8182
}

Diff for: src/test/kotlin/org/opensearch/commons/alerting/action/AcknowledgeAlertResponseTests.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ class AcknowledgeAlertResponseTests {
1818

1919
val acknowledged = mutableListOf(
2020
Alert(
21-
"1234", 0L, 1, "monitor-1234", "test-monitor", 0L, randomUser(),
21+
"1234", 0L, 1, "monitor-1234", "", "test-monitor", 0L, randomUser(),
2222
"trigger-14", "test-trigger", ArrayList(), ArrayList(), Alert.State.ACKNOWLEDGED,
2323
Instant.now(), Instant.now(), Instant.now(), Instant.now(), null, ArrayList(),
2424
"sev-2", ArrayList(), null
2525
)
2626
)
2727
val failed = mutableListOf(
2828
Alert(
29-
"1234", 0L, 1, "monitor-1234", "test-monitor", 0L, randomUser(),
29+
"1234", 0L, 1, "monitor-1234", "", "test-monitor", 0L, randomUser(),
3030
"trigger-14", "test-trigger", ArrayList(), ArrayList(), Alert.State.ERROR, Instant.now(), Instant.now(),
3131
Instant.now(), Instant.now(), null, mutableListOf(AlertError(Instant.now(), "Error msg")),
3232
"sev-2", mutableListOf(ActionExecutionResult("7890", null, 0)), null

Diff for: src/test/kotlin/org/opensearch/commons/alerting/action/GetAlertsResponseTests.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class GetAlertsResponseTests {
3636
0L,
3737
0,
3838
"monitorId",
39+
"workflowId",
3940
"monitorName",
4041
0L,
4142
randomUser(),
@@ -64,6 +65,7 @@ class GetAlertsResponseTests {
6465
assertEquals(1, newReq.alerts.size)
6566
assertEquals(alert, newReq.alerts[0])
6667
assertEquals(1, newReq.totalAlerts)
68+
assertEquals(newReq.alerts[0].workflowId, "workflowId")
6769
}
6870

6971
@Test
@@ -75,6 +77,7 @@ class GetAlertsResponseTests {
7577
0L,
7678
0,
7779
"monitorId",
80+
"workflowId",
7881
"monitorName",
7982
0L,
8083
null,
@@ -96,7 +99,7 @@ class GetAlertsResponseTests {
9699
val req = GetAlertsResponse(listOf(alert), 1)
97100
var actualXContentString = req.toXContent(builder(), ToXContent.EMPTY_PARAMS).string()
98101
val expectedXContentString = "{\"alerts\":[{\"id\":\"id\",\"version\":0,\"monitor_id\":\"monitorId\"," +
99-
"\"schema_version\":0,\"monitor_version\":0,\"monitor_name\":\"monitorName\"," +
102+
"\"workflow_id\":\"workflowId\",\"schema_version\":0,\"monitor_version\":0,\"monitor_name\":\"monitorName\"," +
100103
"\"execution_id\":null,\"trigger_id\":\"triggerId\",\"trigger_name\":\"triggerName\"," +
101104
"\"finding_ids\":[],\"related_doc_ids\":[],\"state\":\"ACKNOWLEDGED\",\"error_message\":null,\"alert_history\":[]," +
102105
"\"severity\":\"severity\",\"action_execution_results\":[],\"start_time\":" + now.toEpochMilli() +

0 commit comments

Comments
 (0)