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.
- Loading branch information
Showing
10 changed files
with
223 additions
and
17 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
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
17 changes: 17 additions & 0 deletions
17
alerting/src/main/kotlin/org/opensearch/alerting/event/listener/AlertingEventListener.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,17 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.alerting.event.listener | ||
|
||
import org.opensearch.client.node.NodeClient | ||
import org.opensearch.commons.alerting.model.Finding | ||
import org.opensearch.commons.authuser.User | ||
|
||
interface AlertingEventListener { | ||
|
||
suspend fun onAdCallbackCalled(client: NodeClient, monitorId: String, user: User?) | ||
|
||
fun onFindingCreated(client: NodeClient, monitorId: String, finding: Finding) | ||
} |
65 changes: 65 additions & 0 deletions
65
...ing/src/main/kotlin/org/opensearch/alerting/event/listener/AlertingEventListenerModule.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,65 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.alerting.event.listener | ||
|
||
import org.apache.logging.log4j.LogManager | ||
import org.opensearch.common.settings.Settings | ||
import org.opensearch.threadpool.ThreadPool | ||
import java.lang.IllegalStateException | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
private val log = LogManager.getLogger(AlertingEventListenerModule::class.java) | ||
|
||
class AlertingEventListenerModule( | ||
val alertingEventListeners: MutableMap<String, MutableSet<String>> = mutableMapOf(), | ||
var frozen: AtomicBoolean = AtomicBoolean(false), | ||
var eventListener: AlertingEventListener? = null, | ||
val settings: Settings, | ||
val threadPool: ThreadPool | ||
) { | ||
|
||
public fun addEventListener(listener: String?, topic: String) { | ||
ensureNotFrozen() | ||
if (listener == null) { | ||
throw IllegalArgumentException("listener must not be null") | ||
} | ||
if (alertingEventListeners.contains(topic)) { | ||
if (alertingEventListeners[topic]!!.contains(listener)) { | ||
throw IllegalArgumentException("listener already added for topic") | ||
} else { | ||
alertingEventListeners[topic]!!.add(listener) | ||
} | ||
} else { | ||
alertingEventListeners[topic] = mutableSetOf(listener) | ||
} | ||
} | ||
|
||
public fun eventListener(): AlertingEventListener { | ||
if (!isFrozen()) { | ||
ensureNotFrozen() | ||
eventListener = freeze() | ||
} | ||
return eventListener!! | ||
} | ||
|
||
private fun freeze(): CompositeAlertingEventListener { | ||
if (this.frozen.compareAndSet(false, true)) { | ||
return CompositeAlertingEventListener(alertingEventListeners, settings, threadPool) | ||
} else { | ||
throw IllegalStateException("already frozen") | ||
} | ||
} | ||
|
||
private fun ensureNotFrozen() { | ||
if (this.frozen.get()) { | ||
throw IllegalArgumentException("Can't modify AlertingEventListenerModule once Alerting callbacks are frozen") | ||
} | ||
} | ||
|
||
private fun isFrozen(): Boolean { | ||
return frozen.get() | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
.../src/main/kotlin/org/opensearch/alerting/event/listener/CompositeAlertingEventListener.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,71 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.alerting.event.listener | ||
|
||
import org.apache.logging.log4j.LogManager | ||
import org.opensearch.action.ActionType | ||
import org.opensearch.alerting.opensearchapi.InjectorContextElement | ||
import org.opensearch.alerting.opensearchapi.withClosableContext | ||
import org.opensearch.client.node.NodeClient | ||
import org.opensearch.common.settings.Settings | ||
import org.opensearch.commons.alerting.AlertingPluginInterface | ||
import org.opensearch.commons.alerting.action.PublishAdRequest | ||
import org.opensearch.commons.alerting.action.PublishFindingsRequest | ||
import org.opensearch.commons.alerting.action.SubscribeAdResponse | ||
import org.opensearch.commons.alerting.action.SubscribeFindingsResponse | ||
import org.opensearch.commons.alerting.model.Finding | ||
import org.opensearch.commons.authuser.User | ||
import org.opensearch.core.action.ActionListener | ||
import org.opensearch.threadpool.ThreadPool | ||
|
||
private val log = LogManager.getLogger(CompositeAlertingEventListener::class.java) | ||
|
||
class CompositeAlertingEventListener( | ||
val topics: MutableMap<String, MutableSet<String>>, | ||
val settings: Settings, | ||
val threadPool: ThreadPool | ||
) : AlertingEventListener { | ||
|
||
override suspend fun onAdCallbackCalled(client: NodeClient, monitorId: String, user: User?) { | ||
val listeners = topics["onAdCallbackCalled"] | ||
listeners?.forEach { listener -> | ||
val SUBSCRIBE_AD_ACTION_TYPE = ActionType(listener, ::SubscribeAdResponse) | ||
val request = PublishAdRequest(monitorId) | ||
|
||
withClosableContext(InjectorContextElement(monitorId, settings, threadPool.threadContext, user!!.roles, user)) { | ||
AlertingPluginInterface.publishAdRequest( | ||
client, | ||
SUBSCRIBE_AD_ACTION_TYPE, | ||
request, | ||
object : ActionListener<SubscribeAdResponse> { | ||
override fun onResponse(response: SubscribeAdResponse) {} | ||
|
||
override fun onFailure(e: Exception) {} | ||
} | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun onFindingCreated(client: NodeClient, monitorId: String, finding: Finding) { | ||
val listeners = topics["onFindingCreated"] | ||
listeners?.forEach { listener -> | ||
val SUBSCRIBE_FINDINGS_ACTION_TYPE = ActionType(listener, ::SubscribeFindingsResponse) | ||
val request = PublishFindingsRequest(monitorId, finding) | ||
|
||
AlertingPluginInterface.publishFinding( | ||
client, | ||
SUBSCRIBE_FINDINGS_ACTION_TYPE, | ||
request, | ||
object : ActionListener<SubscribeFindingsResponse> { | ||
override fun onResponse(response: SubscribeFindingsResponse) {} | ||
|
||
override fun onFailure(e: Exception) {} | ||
} | ||
) | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...ting/src/main/kotlin/org/opensearch/alerting/transport/TransportAddEventListenerAction.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,42 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.alerting.transport | ||
|
||
import org.apache.logging.log4j.LogManager | ||
import org.opensearch.action.ActionRequest | ||
import org.opensearch.action.support.ActionFilters | ||
import org.opensearch.action.support.HandledTransportAction | ||
import org.opensearch.alerting.event.listener.AlertingEventListenerModule | ||
import org.opensearch.client.Client | ||
import org.opensearch.common.inject.Inject | ||
import org.opensearch.commons.alerting.action.AlertingActions | ||
import org.opensearch.commons.alerting.action.EventListenerRequest | ||
import org.opensearch.commons.alerting.action.EventListenerResponse | ||
import org.opensearch.commons.utils.recreateObject | ||
import org.opensearch.core.action.ActionListener | ||
import org.opensearch.tasks.Task | ||
import org.opensearch.transport.TransportService | ||
|
||
private val log = LogManager.getLogger(TransportAddEventListenerAction::class.java) | ||
|
||
class TransportAddEventListenerAction @Inject constructor( | ||
transportService: TransportService, | ||
val client: Client, | ||
actionFilters: ActionFilters, | ||
val eventListenerModule: AlertingEventListenerModule | ||
) : HandledTransportAction<ActionRequest, EventListenerResponse>( | ||
AlertingActions.EVENT_LISTENER_ACTION_NAME, transportService, actionFilters, | ||
::EventListenerRequest | ||
) { | ||
|
||
override fun doExecute(task: Task, request: ActionRequest, listener: ActionListener<EventListenerResponse>) { | ||
log.info("hit here- TransportAddEventListenerAction") | ||
val transformedRequest = request as? EventListenerRequest | ||
?: recreateObject(request) { EventListenerRequest(it) } | ||
eventListenerModule.addEventListener(transformedRequest.listener, transformedRequest.topic) | ||
listener.onResponse(EventListenerResponse()) | ||
} | ||
} |
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