From 5c4fee71d2e1ffbb0c95b03a5f17bab9e28f1e1a Mon Sep 17 00:00:00 2001 From: Aashish Sharma Date: Mon, 4 Jan 2021 10:39:16 +0530 Subject: [PATCH] mgr/dashboard: alert badge includes suppressed alerts On a cluster with alerting enabled, when alerts are triggered, even if they are silenced, the vertical navigation item (Cluster > Monitoring) displays the total number of alerts, including the ones suppressed.This PR intends to fix this issue. Fixes: https://tracker.ceph.com/issues/48591 Signed-off-by: Aashish Sharma (cherry picked from commit b4e32461f1f8430f223965c245974b2a2dbab3aa) --- .../navigation/navigation.component.html | 4 ++-- .../services/prometheus-alert.service.spec.ts | 22 +++++++++++++++++++ .../services/prometheus-alert.service.ts | 6 +++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/navigation/navigation.component.html b/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/navigation/navigation.component.html index 891f674785dd..5a71b110bfb9 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/navigation/navigation.component.html +++ b/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/navigation/navigation.component.html @@ -158,8 +158,8 @@ *ngIf="permissions.prometheus.read"> Monitoring - {{ prometheusAlertService.alerts.length }} + {{ prometheusAlertService.activeAlerts }} diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.spec.ts index 8bf4c41a8222..cbac0014c688 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.spec.ts @@ -187,4 +187,26 @@ describe('PrometheusAlertService', () => { expect(notificationService.show).toHaveBeenCalledTimes(2); }); }); + + describe('alert badge', () => { + beforeEach(() => { + service = TestBed.get(PrometheusAlertService); + + prometheusService = TestBed.get(PrometheusService); + spyOn(prometheusService, 'ifAlertmanagerConfigured').and.callFake((fn) => fn()); + spyOn(prometheusService, 'getAlerts').and.callFake(() => of(alerts)); + + alerts = [ + prometheus.createAlert('alert0', 'active'), + prometheus.createAlert('alert1', 'suppressed'), + prometheus.createAlert('alert2', 'suppressed') + ]; + service.refresh(); + }); + + it('should count active alerts', () => { + service.refresh(); + expect(service.activeAlerts).toBe(1); + }); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.ts index dc1731b92665..365ad007ccb0 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-alert.service.ts @@ -17,6 +17,7 @@ export class PrometheusAlertService { private canAlertsBeNotified = false; alerts: AlertmanagerAlert[] = []; rules: PrometheusRule[] = []; + activeAlerts: number; constructor( private alertFormatter: PrometheusAlertFormatter, @@ -60,6 +61,11 @@ export class PrometheusAlertService { if (this.canAlertsBeNotified) { this.notifyOnAlertChanges(alerts, this.alerts); } + this.activeAlerts = _.reduce( + this.alerts, + (result, alert) => (alert.status.state === 'active' ? ++result : result), + 0 + ); this.alerts = alerts; this.canAlertsBeNotified = true; } -- 2.47.3