From b6c00b2b9d972ce0ee4c34384da892ceb9dc1dee Mon Sep 17 00:00:00 2001 From: Muneeb Aijaz <43588696+MuneebAijaz@users.noreply.github.com> Date: Wed, 28 Aug 2024 20:03:45 +0500 Subject: [PATCH] Alert sensitivity grafana cloud (#604) * Alert sensitivity grafana cloud * Add alert sens to test * Add description * Add description * Add description --- api/v1alpha1/endpointmonitor_types.go | 6 ++++++ ...endpointmonitor.stakater.com_endpointmonitors.yaml | 11 +++++++++++ pkg/monitors/grafana/grafana-monitor.go | 7 ++++++- pkg/monitors/grafana/grafana-monitor_test.go | 7 ++++--- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/api/v1alpha1/endpointmonitor_types.go b/api/v1alpha1/endpointmonitor_types.go index e1a65810..c8a2f70b 100644 --- a/api/v1alpha1/endpointmonitor_types.go +++ b/api/v1alpha1/endpointmonitor_types.go @@ -394,6 +394,12 @@ type GrafanaConfig struct { // or services. These agents periodically send requests to predefined URLs and record the responses, // checking for expected outcomes and measuring performance. Probes []string `json:"probes,omitempty"` + + // The alertSensitivity value defaults to none if there are no alerts or can be set to low, medium, + // or high to correspond to the check alert levels. + // +kubebuilder:validation:Enum=none;low;medium;high + // +kubebuilder:default=none + AlertSensitivity string `json:"alertSensitivity,omitempty"` } // URLSource represents the set of resources to fetch the URL from diff --git a/charts/ingressmonitorcontroller/crds/endpointmonitor.stakater.com_endpointmonitors.yaml b/charts/ingressmonitorcontroller/crds/endpointmonitor.stakater.com_endpointmonitors.yaml index 05333c48..0f984d44 100644 --- a/charts/ingressmonitorcontroller/crds/endpointmonitor.stakater.com_endpointmonitors.yaml +++ b/charts/ingressmonitorcontroller/crds/endpointmonitor.stakater.com_endpointmonitors.yaml @@ -63,6 +63,17 @@ spec: grafanaConfig: description: Configuration for Grafana Cloud Monitor Provider properties: + alertSensitivity: + default: none + description: The alertSensitivity value defaults to none if there + are no alerts or can be set to low, medium, or high to correspond + to the check alert levels. + enum: + - none + - low + - medium + - high + type: string frequency: description: The frequency value specifies how often the check runs in milliseconds diff --git a/pkg/monitors/grafana/grafana-monitor.go b/pkg/monitors/grafana/grafana-monitor.go index a11f8e94..019af8fb 100644 --- a/pkg/monitors/grafana/grafana-monitor.go +++ b/pkg/monitors/grafana/grafana-monitor.go @@ -111,9 +111,13 @@ func (service *GrafanaMonitorService) CreateSyntheticCheck(monitor models.Monito var probeToSet []synthetic_monitoring.Probe var configProbeNames []string var frequency int64 = service.frequency + var alertSensitivity string providerConfig, _ := monitor.Config.(*endpointmonitorv1alpha1.GrafanaConfig) if providerConfig != nil { // load configs from EndpointMonitor CR + if providerConfig.AlertSensitivity != "" { + alertSensitivity = providerConfig.AlertSensitivity + } if providerConfig.Frequency > 0 { frequency = providerConfig.Frequency } @@ -156,6 +160,7 @@ func (service *GrafanaMonitorService) CreateSyntheticCheck(monitor models.Monito }, }, BasicMetricsOnly: true, + AlertSensitivity: alertSensitivity, }, nil } @@ -197,7 +202,7 @@ func (service *GrafanaMonitorService) Update(monitor models.Monitor) { // Using the synthetic monitoring client to update the old check createdCheck, err := service.smClient.UpdateCheck(service.ctx, *newCheck) if err != nil { - log.Error(err, "Failed to update monitor") + log.Error(err, "Failed to update monitor", "monitorID", checkID) return } diff --git a/pkg/monitors/grafana/grafana-monitor_test.go b/pkg/monitors/grafana/grafana-monitor_test.go index 1587c86e..4400370c 100644 --- a/pkg/monitors/grafana/grafana-monitor_test.go +++ b/pkg/monitors/grafana/grafana-monitor_test.go @@ -29,8 +29,9 @@ func TestAddMonitorWithCorrectValues(t *testing.T) { service.Setup(*provider) m := models.Monitor{Name: "google-test", URL: "https://google.com", Config: &endpointmonitorv1alpha1.GrafanaConfig{ - Frequency: 20000, - Probes: []string{"Singapore"}, + Frequency: 20000, + Probes: []string{"Singapore"}, + AlertSensitivity: "low", }} preExistingMonitor, _ := service.GetByName(m.Name) @@ -59,7 +60,7 @@ func TestAddMonitorWithCorrectValues(t *testing.T) { monitorConfig, _ := monitor.Config.(*endpointmonitorv1alpha1.GrafanaConfig) providerConfig, _ := m.Config.(*endpointmonitorv1alpha1.GrafanaConfig) - if monitor.Name != m.Name || monitor.URL != m.URL || monitorConfig.Frequency != providerConfig.Frequency || reflect.DeepEqual(monitorConfig.Probes, providerConfig.Probes) { + if monitor.Name != m.Name || monitor.URL != m.URL || monitorConfig.Frequency != providerConfig.Frequency || reflect.DeepEqual(monitorConfig.Probes, providerConfig.Probes) || monitorConfig.AlertSensitivity != providerConfig.AlertSensitivity { t.Error("URL, name, frequency and probes should be the same", monitor, m) } service.Remove(*monitor)