@@ -42,6 +42,7 @@ const (
42
42
bootstrapCount = 3
43
43
reBootstrapInterval = 30 * time .Second
44
44
mdnsServiceTag = "_ipfs-cluster-discovery._udp"
45
+ maxAlerts = 1000
45
46
)
46
47
47
48
var (
@@ -74,7 +75,7 @@ type Cluster struct {
74
75
informers []Informer
75
76
tracer Tracer
76
77
77
- alerts map [ string ]api.Alert
78
+ alerts [ ]api.Alert
78
79
alertsMux sync.Mutex
79
80
80
81
doneCh chan struct {}
@@ -163,7 +164,7 @@ func NewCluster(
163
164
allocator : allocator ,
164
165
informers : informers ,
165
166
tracer : tracer ,
166
- alerts : make ( map [ string ]api.Alert ) ,
167
+ alerts : [ ]api.Alert {} ,
167
168
peerManager : peerManager ,
168
169
shutdownB : false ,
169
170
removed : false ,
@@ -388,16 +389,16 @@ func (c *Cluster) pushPingMetrics(ctx context.Context) {
388
389
}
389
390
}
390
391
391
- // Alerts returns things that are wrong with the cluster.
392
- func (c * Cluster ) Alerts () map [string ]api.Alert {
393
- alerts := make (map [string ]api.Alert )
392
+ // Alerts returns the last alerts recorded by this cluster peer with the most
393
+ // recent first.
394
+ func (c * Cluster ) Alerts () []api.Alert {
395
+ alerts := make ([]api.Alert , len (c .alerts ), len (c .alerts ))
394
396
395
397
c .alertsMux .Lock ()
396
398
{
397
- for i , alert := range c .alerts {
398
- if time .Now ().Before (time .Unix (0 , alert .Expiry )) {
399
- alerts [i ] = alert
400
- }
399
+ total := len (alerts )
400
+ for i , a := range c .alerts {
401
+ alerts [total - 1 - i ] = a
401
402
}
402
403
}
403
404
c .alertsMux .Unlock ()
@@ -418,17 +419,18 @@ func (c *Cluster) alertsHandler() {
418
419
continue
419
420
}
420
421
421
- logger .Warnf ("metric alert for %s: Peer: %s." , alrt .MetricName , alrt .Peer )
422
+ logger .Warnf ("metric alert for %s: Peer: %s." , alrt .Name , alrt .Peer )
422
423
c .alertsMux .Lock ()
423
- for pID , alert := range c . alerts {
424
- if time . Now (). After ( time . Unix ( 0 , alert . Expiry )) {
425
- delete ( c .alerts , pID )
424
+ {
425
+ if len ( c . alerts ) > maxAlerts {
426
+ c .alerts = c . alerts [: 0 ]
426
427
}
428
+
429
+ c .alerts = append (c .alerts , * alrt )
427
430
}
428
- c .alerts [peer .IDB58Encode (alrt .Peer )] = * alrt
429
431
c .alertsMux .Unlock ()
430
432
431
- if alrt .MetricName != pingMetricName {
433
+ if alrt .Name != pingMetricName {
432
434
continue // only handle ping alerts
433
435
}
434
436
0 commit comments