diff --git a/observability-lib/grafana/dashboard.go b/observability-lib/grafana/dashboard.go index a9b31fa59..ece0fc963 100644 --- a/observability-lib/grafana/dashboard.go +++ b/observability-lib/grafana/dashboard.go @@ -43,7 +43,7 @@ type DeployOptions struct { func alertRuleExist(alerts []alerting.Rule, alert alerting.Rule) bool { for _, a := range alerts { - if reflect.DeepEqual(a, alert) { + if reflect.DeepEqual(a.Title, alert.Title) { return true } } @@ -212,6 +212,19 @@ func DeleteDashboard(options *DeleteOptions) error { return errGetDashboard } + alertsRule, errGetAlertRules := grafanaClient.GetAlertRulesByDashboardUID(*db.UID) + if errGetAlertRules != nil { + return errGetAlertRules + } + + // delete existing alert rules for the dashboard if alerts are disabled + for _, rule := range alertsRule { + _, _, errDeleteAlertRule := grafanaClient.DeleteAlertRule(*rule.Uid) + if errDeleteAlertRule != nil { + return errDeleteAlertRule + } + } + _, errDelete := grafanaClient.DeleteDashboardByUID(*db.UID) if errDelete != nil { return errDelete diff --git a/pkg/services/service.go b/pkg/services/service.go index d0294681a..85726e924 100644 --- a/pkg/services/service.go +++ b/pkg/services/service.go @@ -47,9 +47,13 @@ type Engine struct { wg sync.WaitGroup - emitHealthErr func(error) - conds map[string]error - condsMu sync.RWMutex + serviceMethods interface { + emitHealthErr(error) + ifStarted(func() error) error + ifNotStopped(func() error) error + } + conds map[string]error + condsMu sync.RWMutex } // Go runs fn in a tracked goroutine that will block closing the service. @@ -101,7 +105,13 @@ func (e *Engine) Tracer() trace.Tracer { } // EmitHealthErr records an error to be reported via the next call to Healthy(). -func (e *Engine) EmitHealthErr(err error) { e.emitHealthErr(err) } +func (e *Engine) EmitHealthErr(err error) { e.serviceMethods.emitHealthErr(err) } + +// IfStarted calls fn only if the service is started. +func (e *Engine) IfStarted(fn func() error) error { return e.serviceMethods.ifStarted(fn) } + +// IfNotStopped calls fn only if the service is not stopped. +func (e *Engine) IfNotStopped(fn func() error) error { return e.serviceMethods.ifNotStopped(fn) } // SetHealthCond records a condition key and an error, which causes an unhealthy report, until ClearHealthCond(condition) is called. // condition keys are for internal use only, and do not show up in the health report. @@ -189,7 +199,7 @@ func (c Config) new(lggr logger.SugaredLogger) *service { conds: make(map[string]error), }, } - s.eng.emitHealthErr = s.StateMachine.SvcErrBuffer.Append + s.eng.serviceMethods = s // give Engine access to some service methods if c.NewSubServices != nil { s.subs = c.NewSubServices(lggr) } @@ -280,3 +290,19 @@ func (s *service) Close() error { return }) } + +func (s *service) emitHealthErr(err error) { s.StateMachine.SvcErrBuffer.Append(err) } + +func (s *service) ifStarted(fn func() error) (err error) { + if !s.IfStarted(func() { err = fn() }) { + return fmt.Errorf("service is %s, not started", s.State()) + } + return +} + +func (s *service) ifNotStopped(fn func() error) (err error) { + if !s.IfNotStopped(func() { err = fn() }) { + return errors.New("service is stopped") + } + return +}