-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalert_based_sli.go
179 lines (167 loc) · 4.7 KB
/
alert_based_sli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package shimesaba
import (
"context"
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/mashiike/shimesaba/internal/timeutils"
"golang.org/x/sync/errgroup"
)
type AlertBasedSLI struct {
cfg *AlertBasedSLIConfig
}
func NewAlertBasedSLI(cfg *AlertBasedSLIConfig) *AlertBasedSLI {
return &AlertBasedSLI{cfg: cfg}
}
var evaluateReliabilitiesWorkerNum int = 10
func init() {
if str := os.Getenv("SHIMESABA_EVALUATE_RELIABILITIES_WORKER_NUM"); str != "" {
i, err := strconv.ParseInt(str, 10, 32)
if err != nil {
panic(fmt.Errorf("SHIMESABA_EVALUATE_RELIABILITIES_WORKER_NUM can not parse as int: %w", err))
}
evaluateReliabilitiesWorkerNum = int(i)
if evaluateReliabilitiesWorkerNum <= 0 {
evaluateReliabilitiesWorkerNum = 1
}
}
}
func (o AlertBasedSLI) EvaluateReliabilities(timeFrame time.Duration, alerts Alerts, startAt, endAt time.Time) (Reliabilities, error) {
iter := timeutils.NewIterator(startAt, endAt, timeFrame)
iter.SetEnableOverWindow(true)
rc := make([]*Reliability, 0)
for iter.HasNext() {
cursorAt, _ := iter.Next()
rc = append(rc, NewReliability(cursorAt, timeFrame, nil))
}
reliabilities, err := NewReliabilities(rc)
if err != nil {
return nil, fmt.Errorf("failed to create Reliabilities: %w", err)
}
inputQueue := make(chan *Alert, len(alerts))
outputQueue := make(chan Reliabilities, evaluateReliabilitiesWorkerNum*2)
quit := make(chan struct{})
cancelCtx, cancel := context.WithCancel(context.Background())
defer cancel()
eg, egCtx := errgroup.WithContext(cancelCtx)
for i := 0; i < evaluateReliabilitiesWorkerNum; i++ {
//input workers
workerID := i
eg.Go(func() error {
log.Printf("[debug] start EvaluateReliabilities input worker_id=%d", workerID)
for {
select {
case <-egCtx.Done():
log.Printf("[debug] end EvaluateReliabilities input worker_id=%d: %v", workerID, egCtx.Err())
return egCtx.Err()
case <-quit:
log.Printf("[debug] end EvaluateReliabilities input worker_id=%d: quit", workerID)
return nil
case alert, ok := <-inputQueue:
if !ok {
log.Printf("[debug] end EvaluateReliabilities input worker_id=%d: success", workerID)
return nil
}
log.Printf("[debug] worker_id=%d EvaluateReliabilities %s", workerID, alert.String())
tmp, err := alert.EvaluateReliabilities(timeFrame, o.cfg.TryReassessment)
if err != nil {
log.Printf("[debug] end EvaluateReliabilities input worker_id=%d: EvaluateReliabilities err: %v", workerID, err)
return err
}
outputQueue <- tmp
}
}
})
}
var outputErr error
var wg sync.WaitGroup
wg.Add(1)
go func() {
// output worker
log.Printf("[debug] start EvaluateReliabilities output worker")
defer wg.Done()
log.Printf("[debug] end EvaluateReliabilities output worker")
for {
select {
case <-cancelCtx.Done():
log.Printf("[debug] end EvaluateReliabilities output worker: %v", cancelCtx.Err())
return
case <-quit:
log.Printf("[debug] end EvaluateReliabilities output worker: quit")
return
case tmp, ok := <-outputQueue:
if !ok {
// Completed evaluation of all alerts
log.Printf("[debug] end EvaluateReliabilities output worker: success")
return
}
reliabilities, outputErr = reliabilities.MergeInRange(tmp, startAt, endAt)
if outputErr != nil {
log.Printf("[debug] end EvaluateReliabilities output worker: MergeInRange err: %v", err)
return
}
}
}
}()
for _, alert := range alerts {
if !o.matchAlert(alert) {
continue
}
inputQueue <- alert
}
close(inputQueue)
// wait input wokers done
if err := eg.Wait(); err != nil {
// send quit to output worker and wait output woker done.
close(quit)
wg.Wait()
return nil, err
}
// Evaluation of all alerts was completed. Close queue of ouptut and wait for merge process.
close(outputQueue)
wg.Wait()
return reliabilities, nil
}
func (o AlertBasedSLI) matchAlert(alert *Alert) bool {
if alert.IsVirtual() {
return true
}
log.Printf("[debug] try match %s vs %v", alert, o.cfg)
if o.MatchMonitor(alert.Monitor) {
log.Printf("[debug] match %s", alert)
return true
}
return false
}
func (o AlertBasedSLI) MatchMonitor(monitor *Monitor) bool {
if o.cfg.MonitorID != "" {
if monitor.ID() != o.cfg.MonitorID {
return false
}
}
if o.cfg.MonitorName != "" {
if monitor.Name() != o.cfg.MonitorName {
return false
}
}
if o.cfg.MonitorNamePrefix != "" {
if !strings.HasPrefix(monitor.Name(), o.cfg.MonitorNamePrefix) {
return false
}
}
if o.cfg.MonitorNameSuffix != "" {
if !strings.HasSuffix(monitor.Name(), o.cfg.MonitorNameSuffix) {
return false
}
}
if o.cfg.MonitorType != "" {
if !strings.EqualFold(monitor.Type(), o.cfg.MonitorType) {
return false
}
}
return true
}