mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Fix lint errors
Signed-off-by: Mustafain Ali Khan <mustalik@amazon.com>
This commit is contained in:
parent
1ae0a35e33
commit
c53f2ee849
|
@ -131,7 +131,6 @@ func NewGroup(o GroupOptions) *Group {
|
||||||
}
|
}
|
||||||
|
|
||||||
alertStoreFunc := o.AlertStoreFunc
|
alertStoreFunc := o.AlertStoreFunc
|
||||||
//var alertStore *AlertStore
|
|
||||||
if alertStoreFunc == nil {
|
if alertStoreFunc == nil {
|
||||||
alertStoreFunc = DefaultAlertStoreFunc
|
alertStoreFunc = DefaultAlertStoreFunc
|
||||||
}
|
}
|
||||||
|
@ -553,7 +552,7 @@ func (g *Group) Eval(ctx context.Context, ts time.Time) {
|
||||||
// Restore alerts when feature is enabled and it is the first evaluation for the group
|
// Restore alerts when feature is enabled and it is the first evaluation for the group
|
||||||
if ar, ok := rule.(*AlertingRule); ok {
|
if ar, ok := rule.(*AlertingRule); ok {
|
||||||
restoredAlerts, _ := g.alertStore.GetAlerts(ar.GetFingerprint(GroupKey(g.File(), g.Name())))
|
restoredAlerts, _ := g.alertStore.GetAlerts(ar.GetFingerprint(GroupKey(g.File(), g.Name())))
|
||||||
if restoredAlerts != nil && len(restoredAlerts) > 0 {
|
if len(restoredAlerts) > 0 {
|
||||||
ar.SetActiveAlerts(restoredAlerts)
|
ar.SetActiveAlerts(restoredAlerts)
|
||||||
logger.Info("Restored alerts from store", "rule", ar.name, "alerts", len(restoredAlerts))
|
logger.Info("Restored alerts from store", "rule", ar.name, "alerts", len(restoredAlerts))
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ import (
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/common/promslog"
|
"github.com/prometheus/common/promslog"
|
||||||
|
"golang.org/x/sync/semaphore"
|
||||||
|
|
||||||
"github.com/prometheus/prometheus/model/labels"
|
"github.com/prometheus/prometheus/model/labels"
|
||||||
"github.com/prometheus/prometheus/model/rulefmt"
|
"github.com/prometheus/prometheus/model/rulefmt"
|
||||||
|
@ -34,7 +35,6 @@ import (
|
||||||
"github.com/prometheus/prometheus/promql/parser"
|
"github.com/prometheus/prometheus/promql/parser"
|
||||||
"github.com/prometheus/prometheus/storage"
|
"github.com/prometheus/prometheus/storage"
|
||||||
"github.com/prometheus/prometheus/util/strutil"
|
"github.com/prometheus/prometheus/util/strutil"
|
||||||
"golang.org/x/sync/semaphore"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// QueryFunc processes PromQL queries.
|
// QueryFunc processes PromQL queries.
|
||||||
|
@ -88,7 +88,7 @@ func DefaultEvalIterationFunc(ctx context.Context, g *Group, evalTimestamp time.
|
||||||
g.setLastEvalTimestamp(evalTimestamp)
|
g.setLastEvalTimestamp(evalTimestamp)
|
||||||
|
|
||||||
if g.alertStore != nil {
|
if g.alertStore != nil {
|
||||||
//feature enabled
|
// feature enabled.
|
||||||
go func() {
|
go func() {
|
||||||
g.alertStoreFunc(g)
|
g.alertStoreFunc(g)
|
||||||
}()
|
}()
|
||||||
|
|
|
@ -2310,7 +2310,7 @@ func TestKeepFiringForStateRestore(t *testing.T) {
|
||||||
0,
|
0,
|
||||||
keepFiringForDuration,
|
keepFiringForDuration,
|
||||||
labels.FromStrings("severity", "critical"),
|
labels.FromStrings("severity", "critical"),
|
||||||
labels.FromStrings("annotation1", "rule1"), labels.EmptyLabels(), "", true, nil,
|
labels.FromStrings("annotation_test", "rule1"), labels.EmptyLabels(), "", true, nil,
|
||||||
)
|
)
|
||||||
keepFiringForDuration2 := 60 * time.Minute
|
keepFiringForDuration2 := 60 * time.Minute
|
||||||
rule2 := NewAlertingRule(
|
rule2 := NewAlertingRule(
|
||||||
|
@ -2319,7 +2319,7 @@ func TestKeepFiringForStateRestore(t *testing.T) {
|
||||||
0,
|
0,
|
||||||
keepFiringForDuration2,
|
keepFiringForDuration2,
|
||||||
labels.FromStrings("severity", "critical"),
|
labels.FromStrings("severity", "critical"),
|
||||||
labels.FromStrings("annotation2", "rule2"), labels.EmptyLabels(), "", true, nil,
|
labels.FromStrings("annotation_test", "rule2"), labels.EmptyLabels(), "", true, nil,
|
||||||
)
|
)
|
||||||
|
|
||||||
group := NewGroup(GroupOptions{
|
group := NewGroup(GroupOptions{
|
||||||
|
@ -2387,7 +2387,7 @@ func TestKeepFiringForStateRestore(t *testing.T) {
|
||||||
0,
|
0,
|
||||||
keepFiringForDuration,
|
keepFiringForDuration,
|
||||||
labels.FromStrings("severity", "critical"),
|
labels.FromStrings("severity", "critical"),
|
||||||
labels.FromStrings("annotation1", "rule1"), labels.EmptyLabels(), "", false, nil,
|
labels.FromStrings("annotation_test", "rule1"), labels.EmptyLabels(), "", false, nil,
|
||||||
)
|
)
|
||||||
newRule2 := NewAlertingRule(
|
newRule2 := NewAlertingRule(
|
||||||
"HTTPRequestRateLow",
|
"HTTPRequestRateLow",
|
||||||
|
@ -2395,9 +2395,9 @@ func TestKeepFiringForStateRestore(t *testing.T) {
|
||||||
0,
|
0,
|
||||||
keepFiringForDuration2,
|
keepFiringForDuration2,
|
||||||
labels.FromStrings("severity", "critical"),
|
labels.FromStrings("severity", "critical"),
|
||||||
labels.FromStrings("annotation2", "rule2"), labels.EmptyLabels(), "", true, nil,
|
labels.FromStrings("annotation_test", "rule2"), labels.EmptyLabels(), "", true, nil,
|
||||||
)
|
)
|
||||||
// Restart alert store
|
// Restart alert store.
|
||||||
newAlertStore := NewFileStore(promslog.NewNopLogger(), testStoreFile)
|
newAlertStore := NewFileStore(promslog.NewNopLogger(), testStoreFile)
|
||||||
|
|
||||||
newGroup := NewGroup(GroupOptions{
|
newGroup := NewGroup(GroupOptions{
|
||||||
|
@ -2425,23 +2425,33 @@ func TestKeepFiringForStateRestore(t *testing.T) {
|
||||||
require.Equal(t, tt.alertsExpected, len(got)+len(got2))
|
require.Equal(t, tt.alertsExpected, len(got)+len(got2))
|
||||||
|
|
||||||
results := [][]*Alert{got, got2}
|
results := [][]*Alert{got, got2}
|
||||||
|
for _, result := range results {
|
||||||
for i, result := range results {
|
|
||||||
sort.Slice(result, func(i, j int) bool {
|
sort.Slice(result, func(i, j int) bool {
|
||||||
return labels.Compare(got[i].Labels, got[j].Labels) < 0
|
return labels.Compare(result[i].Labels, result[j].Labels) < 0
|
||||||
})
|
})
|
||||||
sortAlerts(result)
|
sortAlerts(result)
|
||||||
sortAlerts(expectedAlerts[i])
|
}
|
||||||
|
for _, alerts := range expectedAlerts {
|
||||||
|
sort.Slice(alerts, func(i, j int) bool {
|
||||||
|
return labels.Compare(alerts[i].Labels, alerts[j].Labels) < 0
|
||||||
|
})
|
||||||
|
sortAlerts(alerts)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, expected := range expectedAlerts {
|
for i, expected := range expectedAlerts {
|
||||||
got = results[i]
|
got = results[i]
|
||||||
require.Equal(t, len(expected), len(got))
|
require.Equal(t, len(expected), len(got))
|
||||||
for j, alert := range expected {
|
for j, alert := range expected {
|
||||||
require.Equal(t, alert.Labels, got[j].Labels)
|
diff := float64(alert.KeepFiringSince.Unix() - got[j].KeepFiringSince.Unix())
|
||||||
require.Equal(t, alert.Annotations, got[j].Annotations)
|
require.Equal(t, 0.0, math.Abs(diff), "'keep_firing_for' restored time is wrong")
|
||||||
require.Equal(t, alert.ActiveAt, got[j].ActiveAt)
|
diff = float64(alert.ActiveAt.Unix() - got[j].ActiveAt.Unix())
|
||||||
require.Equal(t, alert.KeepFiringSince, got[j].KeepFiringSince)
|
require.Equal(t, 0.0, math.Abs(diff), "'keep_firing_for' state restored activeAt time is wrong")
|
||||||
|
|
||||||
|
require.Equal(t, alert.Value, got[j].Value)
|
||||||
|
require.NotEmpty(t, alert.Labels)
|
||||||
|
require.Equal(t, alert.Labels.Get("instance"), got[j].Labels.Get("instance"))
|
||||||
|
require.NotEmpty(t, alert.Annotations)
|
||||||
|
require.Equal(t, alert.Annotations.Get("annotation_test"), got[j].Annotations.Get("annotation_test"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -20,7 +20,7 @@ type AlertStore interface {
|
||||||
type FileStore struct {
|
type FileStore struct {
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
alertsByRule map[uint64][]*Alert
|
alertsByRule map[uint64][]*Alert
|
||||||
//protects the `alertsByRule` map
|
// protects the `alertsByRule` map.
|
||||||
stateMtx sync.RWMutex
|
stateMtx sync.RWMutex
|
||||||
path string
|
path string
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ func NewFileStore(l *slog.Logger, storagePath string) *FileStore {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// initState reads the state from file storage into the alertsByRule map
|
// initState reads the state from file storage into the alertsByRule map.
|
||||||
func (s *FileStore) initState() {
|
func (s *FileStore) initState() {
|
||||||
file, err := os.OpenFile(s.path, os.O_RDWR|os.O_CREATE, 0o666)
|
file, err := os.OpenFile(s.path, os.O_RDWR|os.O_CREATE, 0o666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -56,7 +56,7 @@ func (s *FileStore) initState() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAlerts returns the stored alerts for an alerting rule
|
// GetAlerts returns the stored alerts for an alerting rule
|
||||||
// Alert state is read from the in memory map which is populated during initialization
|
// Alert state is read from the in memory map which is populated during initialization.
|
||||||
func (s *FileStore) GetAlerts(key uint64) (map[uint64]*Alert, error) {
|
func (s *FileStore) GetAlerts(key uint64) (map[uint64]*Alert, error) {
|
||||||
s.stateMtx.RLock()
|
s.stateMtx.RLock()
|
||||||
defer s.stateMtx.RUnlock()
|
defer s.stateMtx.RUnlock()
|
||||||
|
@ -76,7 +76,7 @@ func (s *FileStore) GetAlerts(key uint64) (map[uint64]*Alert, error) {
|
||||||
return alerts, nil
|
return alerts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetAlerts updates the stateByRule map and writes state to file storage
|
// SetAlerts updates the stateByRule map and writes state to file storage.
|
||||||
func (s *FileStore) SetAlerts(key uint64, alerts []*Alert) error {
|
func (s *FileStore) SetAlerts(key uint64, alerts []*Alert) error {
|
||||||
s.stateMtx.Lock()
|
s.stateMtx.Lock()
|
||||||
defer s.stateMtx.Unlock()
|
defer s.stateMtx.Unlock()
|
||||||
|
|
|
@ -5,9 +5,10 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/prometheus/common/promslog"
|
"github.com/prometheus/common/promslog"
|
||||||
"github.com/prometheus/prometheus/model/labels"
|
"github.com/prometheus/prometheus/model/labels"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAlertStore(t *testing.T) {
|
func TestAlertStore(t *testing.T) {
|
||||||
|
@ -27,14 +28,22 @@ func TestAlertStore(t *testing.T) {
|
||||||
alertsByRule[4] = []*Alert{}
|
alertsByRule[4] = []*Alert{}
|
||||||
|
|
||||||
for key, alerts := range alertsByRule {
|
for key, alerts := range alertsByRule {
|
||||||
|
sortAlerts(alerts)
|
||||||
err := alertStore.SetAlerts(key, alerts)
|
err := alertStore.SetAlerts(key, alerts)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
got, err := alertStore.GetAlerts(key)
|
got, err := alertStore.GetAlerts(key)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, len(alerts), len(got))
|
require.Equal(t, len(alerts), len(got))
|
||||||
|
|
||||||
|
result := make([]*Alert, 0, len(got))
|
||||||
|
for _, value := range got {
|
||||||
|
result = append(result, value)
|
||||||
|
}
|
||||||
|
sortAlerts(result)
|
||||||
|
|
||||||
j := 0
|
j := 0
|
||||||
for _, al := range got {
|
for _, al := range result {
|
||||||
require.Equal(t, alerts[j].State, al.State)
|
require.Equal(t, alerts[j].State, al.State)
|
||||||
require.Equal(t, alerts[j].Labels, al.Labels)
|
require.Equal(t, alerts[j].Labels, al.Labels)
|
||||||
require.Equal(t, alerts[j].Annotations, al.Annotations)
|
require.Equal(t, alerts[j].Annotations, al.Annotations)
|
||||||
|
|
Loading…
Reference in a new issue