From 7825514f1da8b608effc5d8ab96bdf1fa89a0708 Mon Sep 17 00:00:00 2001 From: Joshua Li Date: Thu, 7 Oct 2021 16:26:30 -0700 Subject: [PATCH 1/2] Add metrics for notifications Signed-off-by: Joshua Li --- .../reportsscheduler/metrics/Metrics.java | 5 +++ .../notifications/NotificationsActions.kt | 43 +++++++++++++------ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/reports-scheduler/src/main/java/org/opensearch/reportsscheduler/metrics/Metrics.java b/reports-scheduler/src/main/java/org/opensearch/reportsscheduler/metrics/Metrics.java index 59b42a7d..903bbbcb 100644 --- a/reports-scheduler/src/main/java/org/opensearch/reportsscheduler/metrics/Metrics.java +++ b/reports-scheduler/src/main/java/org/opensearch/reportsscheduler/metrics/Metrics.java @@ -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()); diff --git a/reports-scheduler/src/main/kotlin/org/opensearch/reportsscheduler/notifications/NotificationsActions.kt b/reports-scheduler/src/main/kotlin/org/opensearch/reportsscheduler/notifications/NotificationsActions.kt index bb9bbe86..ea2e651f 100644 --- a/reports-scheduler/src/main/kotlin/org/opensearch/reportsscheduler/notifications/NotificationsActions.kt +++ b/reports-scheduler/src/main/kotlin/org/opensearch/reportsscheduler/notifications/NotificationsActions.kt @@ -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 @@ -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 @@ -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 { - 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 { + 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 } From cd997004e1bb7711f8a34988e5d2afd98af13e59 Mon Sep 17 00:00:00 2001 From: Joshua Li Date: Thu, 7 Oct 2021 16:41:13 -0700 Subject: [PATCH 2/2] Suppress catching generic exception Signed-off-by: Joshua Li --- .../reportsscheduler/notifications/NotificationsActions.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/reports-scheduler/src/main/kotlin/org/opensearch/reportsscheduler/notifications/NotificationsActions.kt b/reports-scheduler/src/main/kotlin/org/opensearch/reportsscheduler/notifications/NotificationsActions.kt index ea2e651f..e77a873b 100644 --- a/reports-scheduler/src/main/kotlin/org/opensearch/reportsscheduler/notifications/NotificationsActions.kt +++ b/reports-scheduler/src/main/kotlin/org/opensearch/reportsscheduler/notifications/NotificationsActions.kt @@ -77,6 +77,7 @@ internal object NotificationsActions { return sendNotificationResponse } + @Suppress("TooGenericExceptionCaught") private fun sendNotificationHelper( delivery: ReportDefinition.Delivery, referenceId: String