mirror of
https://github.com/prometheus/alertmanager
synced 2024-12-28 00:52:13 +00:00
Separate alert and silence DB.
This commit separates out the silences and alert/notify databases. Additional alert FP index was added and a locking of the silences DB access added.
This commit is contained in:
parent
2eb685c45b
commit
d0e35c109e
16
main.go
16
main.go
@ -62,23 +62,29 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
db, err := sql.Open("sqlite3", filepath.Join(*dataDir, "am.db"))
|
||||
alertDB, err := sql.Open("sqlite3", filepath.Join(*dataDir, "alerts.db"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
defer alertDB.Close()
|
||||
|
||||
silenceDB, err := sql.Open("sqlite3", filepath.Join(*dataDir, "silence.db"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer alertDB.Close()
|
||||
|
||||
marker := types.NewMarker()
|
||||
|
||||
alerts, err := sqlite.NewAlerts(db)
|
||||
alerts, err := sqlite.NewAlerts(alertDB)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
notifies, err := sqlite.NewNotifies(db)
|
||||
notifies, err := sqlite.NewNotifies(alertDB)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
silences, err := sqlite.NewSilences(db, marker)
|
||||
silences, err := sqlite.NewSilences(silenceDB, marker)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
@ -25,12 +25,13 @@ CREATE TABLE IF NOT EXISTS alerts (
|
||||
updated_at timestamp,
|
||||
timeout integer
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS fingerprint ON alerts (fingerprint);
|
||||
CREATE INDEX IF NOT EXISTS alerts_start ON alerts (starts_at);
|
||||
CREATE INDEX IF NOT EXISTS alerts_end ON alerts (ends_at);
|
||||
CREATE INDEX IF NOT EXISTS alerts_updated ON alerts (updated_at);
|
||||
`
|
||||
|
||||
var dbmtx sync.Mutex
|
||||
var alertMtx, silenceMtx sync.Mutex
|
||||
|
||||
type Alerts struct {
|
||||
db *sql.DB
|
||||
@ -42,8 +43,8 @@ type Alerts struct {
|
||||
}
|
||||
|
||||
func NewAlerts(db *sql.DB) (*Alerts, error) {
|
||||
dbmtx.Lock()
|
||||
defer dbmtx.Unlock()
|
||||
alertMtx.Lock()
|
||||
defer alertMtx.Unlock()
|
||||
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
@ -125,8 +126,8 @@ func (a *Alerts) GetPending() provider.AlertIterator {
|
||||
}
|
||||
|
||||
func (a *Alerts) getPending() ([]*types.Alert, error) {
|
||||
dbmtx.Lock()
|
||||
defer dbmtx.Unlock()
|
||||
alertMtx.Lock()
|
||||
defer alertMtx.Unlock()
|
||||
|
||||
// Get the last instance for each alert.
|
||||
rows, err := a.db.Query(`
|
||||
@ -181,8 +182,8 @@ func (a *Alerts) Get(model.Fingerprint) (*types.Alert, error) {
|
||||
|
||||
// Put implements the Alerts interface.
|
||||
func (a *Alerts) Put(alerts ...*types.Alert) error {
|
||||
dbmtx.Lock()
|
||||
defer dbmtx.Unlock()
|
||||
alertMtx.Lock()
|
||||
defer alertMtx.Unlock()
|
||||
|
||||
tx, err := a.db.Begin()
|
||||
if err != nil {
|
||||
@ -345,8 +346,8 @@ type Notifies struct {
|
||||
}
|
||||
|
||||
func NewNotifies(db *sql.DB) (*Notifies, error) {
|
||||
dbmtx.Lock()
|
||||
defer dbmtx.Unlock()
|
||||
alertMtx.Lock()
|
||||
defer alertMtx.Unlock()
|
||||
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
@ -363,8 +364,8 @@ func NewNotifies(db *sql.DB) (*Notifies, error) {
|
||||
|
||||
// Get implements the Notifies interface.
|
||||
func (n *Notifies) Get(dest string, fps ...model.Fingerprint) ([]*types.NotifyInfo, error) {
|
||||
dbmtx.Lock()
|
||||
defer dbmtx.Unlock()
|
||||
alertMtx.Lock()
|
||||
defer alertMtx.Unlock()
|
||||
|
||||
var result []*types.NotifyInfo
|
||||
|
||||
@ -402,8 +403,8 @@ func (n *Notifies) Get(dest string, fps ...model.Fingerprint) ([]*types.NotifyIn
|
||||
|
||||
// Set implements the Notifies interface.
|
||||
func (n *Notifies) Set(ns ...*types.NotifyInfo) error {
|
||||
dbmtx.Lock()
|
||||
defer dbmtx.Unlock()
|
||||
alertMtx.Lock()
|
||||
defer alertMtx.Unlock()
|
||||
|
||||
tx, err := n.db.Begin()
|
||||
if err != nil {
|
||||
@ -472,8 +473,8 @@ type Silences struct {
|
||||
|
||||
// NewSilences returns a new Silences based on the provided SQL DB.
|
||||
func NewSilences(db *sql.DB, mk types.Marker) (*Silences, error) {
|
||||
dbmtx.Lock()
|
||||
defer dbmtx.Unlock()
|
||||
silenceMtx.Lock()
|
||||
defer silenceMtx.Unlock()
|
||||
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
@ -510,8 +511,8 @@ func (s *Silences) Mutes(lset model.LabelSet) bool {
|
||||
|
||||
// All implements the Silences interface.
|
||||
func (s *Silences) All() ([]*types.Silence, error) {
|
||||
dbmtx.Lock()
|
||||
defer dbmtx.Unlock()
|
||||
silenceMtx.Lock()
|
||||
defer silenceMtx.Unlock()
|
||||
|
||||
rows, err := s.db.Query(`
|
||||
SELECT id, matchers, starts_at, ends_at, created_at, created_by, comment
|
||||
@ -558,8 +559,8 @@ func (s *Silences) All() ([]*types.Silence, error) {
|
||||
|
||||
// Set impelements the Silences interface.
|
||||
func (s *Silences) Set(sil *types.Silence) (uint64, error) {
|
||||
dbmtx.Lock()
|
||||
defer dbmtx.Unlock()
|
||||
silenceMtx.Lock()
|
||||
defer silenceMtx.Unlock()
|
||||
|
||||
mb, err := json.Marshal(sil.Silence.Matchers)
|
||||
if err != nil {
|
||||
@ -600,6 +601,9 @@ func (s *Silences) Set(sil *types.Silence) (uint64, error) {
|
||||
|
||||
// Del implements the Silences interface.
|
||||
func (s *Silences) Del(sid uint64) error {
|
||||
silenceMtx.Lock()
|
||||
defer silenceMtx.Unlock()
|
||||
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
@ -616,8 +620,8 @@ func (s *Silences) Del(sid uint64) error {
|
||||
|
||||
// Get implements the Silences interface.
|
||||
func (s *Silences) Get(sid uint64) (*types.Silence, error) {
|
||||
dbmtx.Lock()
|
||||
defer dbmtx.Unlock()
|
||||
silenceMtx.Lock()
|
||||
defer silenceMtx.Unlock()
|
||||
|
||||
row := s.db.QueryRow(`
|
||||
SELECT id, matchers, starts_at, ends_at, created_at, created_by, comment
|
||||
|
Loading…
Reference in New Issue
Block a user