Alerting: A better and cleaner way to know if Alertmanager is initialised (#36659)

Signed-off-by: Ganesh Vernekar <ganeshvern@gmail.com>
This commit is contained in:
Ganesh Vernekar 2021-07-12 18:53:01 +05:30 committed by GitHub
parent 37c3e6f9b9
commit 8efe1856e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 11 deletions

View File

@ -104,8 +104,6 @@ type Alertmanager struct {
reloadConfigMtx sync.RWMutex
config []byte
initialised bool
}
func New(cfg *setting.Cfg, store store.AlertingStore, m *metrics.Metrics) (*Alertmanager, error) {
@ -158,6 +156,16 @@ func New(cfg *setting.Cfg, store store.AlertingStore, m *metrics.Metrics) (*Aler
return am, nil
}
func (am *Alertmanager) Ready() bool {
// We consider AM as ready only when the config has been
// applied at least once successfully. Until then, some objects
// can still be nil.
am.reloadConfigMtx.RLock()
defer am.reloadConfigMtx.RUnlock()
return len(am.config) > 0
}
func (am *Alertmanager) Run(ctx context.Context) error {
// Make sure dispatcher starts. We can tolerate future reload failures.
if err := am.SyncAndApplyConfigFromDatabase(); err != nil {
@ -272,14 +280,6 @@ func (am *Alertmanager) SyncAndApplyConfigFromDatabase() error {
// applyConfig applies a new configuration by re-initializing all components using the configuration provided.
// It is not safe to call concurrently.
func (am *Alertmanager) applyConfig(cfg *apimodels.PostableUserConfig, rawConfig []byte) (err error) {
defer func() {
if err == nil {
// We consider AM as initialised only when the config has been
// applied at least once successfully. Until then, some objects
// can still be nil.
am.initialised = true
}
}()
// First, let's make sure this config is not already loaded
var configChanged bool
if rawConfig == nil {

View File

@ -28,7 +28,7 @@ func (am *Alertmanager) GetAlerts(active, silenced, inhibited bool, filter []str
res = apimodels.GettableAlerts{}
)
if !am.initialised {
if !am.Ready() {
return res, ErrGetAlertsUnavailable
}