Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metrics for notifications #173

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -75,28 +77,44 @@ internal object NotificationsActions {
return sendNotificationResponse
}

@Suppress("TooGenericExceptionCaught")
private fun sendNotificationHelper(
delivery: ReportDefinition.Delivery,
referenceId: String
): 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