Skip to content

Commit

Permalink
Add metrics for notifications
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Li <[email protected]>
  • Loading branch information
joshuali925 committed Oct 7, 2021
1 parent a238baf commit 7825514
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ public enum Metrics {
REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR("on_demand_from_definition.create.system_error", new RollingCounter()),


// Notifications: POST _plugins/_reports/on_demand/{reportDefinitionId} and scheduled jobs
REPORT_NOTIFICATIONS_TOTAL("report_notifications.send.total", new BasicCounter()),
REPORT_NOTIFICATIONS_ERROR("report_notifications.send.error", new RollingCounter()),


REPORT_SECURITY_PERMISSION_ERROR("es_security_permission_error", new RollingCounter()),
REPORT_PERMISSION_USER_ERROR("permission_user_error", new RollingCounter());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

package org.opensearch.reportsscheduler.notifications

import org.opensearch.OpenSearchException
import org.opensearch.action.ActionListener
import org.opensearch.client.node.NodeClient
import org.opensearch.common.util.concurrent.ThreadContext
Expand All @@ -22,6 +23,7 @@ import org.opensearch.commons.notifications.model.ChannelMessage
import org.opensearch.commons.notifications.model.EventSource
import org.opensearch.commons.notifications.model.SeverityType
import org.opensearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX
import org.opensearch.reportsscheduler.metrics.Metrics
import org.opensearch.reportsscheduler.model.CreateReportDefinitionResponse
import org.opensearch.reportsscheduler.model.ReportDefinition
import org.opensearch.reportsscheduler.util.logger
Expand Down Expand Up @@ -81,22 +83,37 @@ internal object NotificationsActions {
): SendNotificationResponse? {
log.info("$LOG_PREFIX:NotificationsActions-send")
var sendNotificationResponse: SendNotificationResponse? = null
NotificationsPluginInterface.sendNotification(
client,
EventSource(delivery.title, referenceId, FEATURE_REPORTS, SeverityType.INFO),
ChannelMessage(delivery.textDescription, delivery.htmlDescription, null),
delivery.configIds,
object : ActionListener<SendNotificationResponse> {
override fun onResponse(response: SendNotificationResponse) {
sendNotificationResponse = response
log.info("$LOG_PREFIX:NotificationsActions-send:$sendNotificationResponse")
}
Metrics.REPORT_NOTIFICATIONS_TOTAL.counter.increment()
try {
NotificationsPluginInterface.sendNotification(
client,
EventSource(delivery.title, referenceId, FEATURE_REPORTS, SeverityType.INFO),
ChannelMessage(delivery.textDescription, delivery.htmlDescription, null),
delivery.configIds,
object : ActionListener<SendNotificationResponse> {
override fun onResponse(response: SendNotificationResponse) {
sendNotificationResponse = response
log.info("$LOG_PREFIX:NotificationsActions-send:$sendNotificationResponse")
}

override fun onFailure(exception: Exception) {
log.error("$LOG_PREFIX:NotificationsActions-send Error:$exception")
override fun onFailure(exception: Exception) {
log.error("$LOG_PREFIX:NotificationsActions-send Error:$exception")
throw exception
}
}
)
} catch (e: Exception) {
Metrics.REPORT_NOTIFICATIONS_ERROR.counter.increment()
val isMissingNotificationPlugin = e.message?.contains("failed to find action") ?: false
if (isMissingNotificationPlugin) {
throw OpenSearchException(
"Notification plugin is not installed. Please install the Notification plugin.",
e
)
} else {
throw e
}
)
}
return sendNotificationResponse
}

Expand Down

0 comments on commit 7825514

Please sign in to comment.