Provide reader to alertmanager silence instead of file path (#39305)

pull/37423/head^2
Yuriy Tseretyan 4 years ago committed by GitHub
parent be2b08798b
commit e1aae0549e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 33
      pkg/services/ngalert/notifier/alertmanager.go

@ -8,6 +8,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/url" "net/url"
"os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"sync" "sync"
@ -164,12 +165,7 @@ func newAlertmanager(orgID int64, cfg *setting.Cfg, store store.AlertingStore, k
c := am.peer.AddState(fmt.Sprintf("notificationlog:%d", am.OrgID), am.notificationLog, m.Registerer) c := am.peer.AddState(fmt.Sprintf("notificationlog:%d", am.OrgID), am.notificationLog, m.Registerer)
am.notificationLog.SetBroadcast(c.Broadcast) am.notificationLog.SetBroadcast(c.Broadcast)
// Initialize silences am.silences, err = newSilences(silencesFilePath, m.Registerer)
am.silences, err = silence.New(silence.Options{
Metrics: m.Registerer,
SnapshotFile: silencesFilePath,
Retention: retentionNotificationsAndSilences,
})
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to initialize the silencing component of alerting: %w", err) return nil, fmt.Errorf("unable to initialize the silencing component of alerting: %w", err)
} }
@ -194,6 +190,31 @@ func newAlertmanager(orgID int64, cfg *setting.Cfg, store store.AlertingStore, k
return am, nil return am, nil
} }
// newSilences initializes returns *silence.Silences (from the Alertmanager) with silences taken from the file by path silencesFilePath and specific metrics registerer.
func newSilences(silencesFilePath string, registerer prometheus.Registerer) (*silence.Silences, error) {
//TODO yuriy: Replace with silencesFilePath when fix in https://github.com/prometheus/alertmanager/pull/2710 is merged.
silenceOpts := silence.Options{
Metrics: registerer,
Retention: retentionNotificationsAndSilences,
}
//The path is generated by the filestore. So presumably it should be safe
//nolint:gosec
silencesFileReader, err := os.Open(silencesFilePath)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
if silencesFileReader != nil {
silenceOpts.SnapshotReader = silencesFileReader
defer func(file *os.File) {
_ = file.Close()
}(silencesFileReader)
}
// Initialize silences
return silence.New(silenceOpts)
}
func (am *Alertmanager) Ready() bool { func (am *Alertmanager) Ready() bool {
// We consider AM as ready only when the config has been // We consider AM as ready only when the config has been
// applied at least once successfully. Until then, some objects // applied at least once successfully. Until then, some objects

Loading…
Cancel
Save