This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
registry.go
167 lines (142 loc) · 4.78 KB
/
registry.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
// pmm-managed
// Copyright (C) 2017 Percona LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package checks
import (
"crypto/sha1" //nolint:gosec
"encoding/hex"
"fmt"
"sync"
"time"
"github.com/go-openapi/strfmt"
"github.com/percona-platform/saas/pkg/check"
"github.com/percona/pmm/api/alertmanager/ammodels"
"github.com/prometheus/common/model"
)
// registry stores alerts and delay information by IDs.
type registry struct {
rw sync.RWMutex
// Results stored grouped by interval and by check name. It allows us to remove results for specific group.
checkResults map[check.Interval]map[string][]sttCheckResult
alertTTL time.Duration
nowF func() time.Time // for tests
}
// newRegistry creates a new registry.
func newRegistry(alertTTL time.Duration) *registry {
return ®istry{
checkResults: make(map[check.Interval]map[string][]sttCheckResult),
alertTTL: alertTTL,
nowF: time.Now,
}
}
// set adds check results.
func (r *registry) set(checkResults []sttCheckResult) {
r.rw.Lock()
defer r.rw.Unlock()
for _, result := range checkResults {
// Empty interval means standard.
if result.interval == "" {
result.interval = check.Standard
}
if _, ok := r.checkResults[result.interval]; !ok {
r.checkResults[result.interval] = make(map[string][]sttCheckResult)
}
r.checkResults[result.interval][result.checkName] = append(r.checkResults[result.interval][result.checkName], result)
}
}
// deleteByName removes results for specified checks.
func (r *registry) deleteByName(checkNames []string) {
r.rw.Lock()
defer r.rw.Unlock()
for _, intervalGroup := range r.checkResults {
for _, name := range checkNames {
delete(intervalGroup, name)
}
}
}
// deleteByInterval removes results for specified interval.
func (r *registry) deleteByInterval(interval check.Interval) {
r.rw.Lock()
defer r.rw.Unlock()
delete(r.checkResults, interval)
}
// cleanup removes all stt results form registry.
func (r *registry) cleanup() {
r.rw.Lock()
defer r.rw.Unlock()
r.checkResults = make(map[check.Interval]map[string][]sttCheckResult)
}
// collect returns a slice of alerts created from the stored check results.
func (r *registry) collect() ammodels.PostableAlerts {
r.rw.RLock()
defer r.rw.RUnlock()
var alerts ammodels.PostableAlerts
for _, intervalGroup := range r.checkResults {
for _, checkNameGroup := range intervalGroup {
for _, checkResult := range checkNameGroup {
alerts = append(alerts, r.createAlert(checkResult.checkName, &checkResult.target, &checkResult.result, r.alertTTL))
}
}
}
return alerts
}
func (r *registry) getCheckResults() []sttCheckResult {
r.rw.RLock()
defer r.rw.RUnlock()
var results []sttCheckResult
for _, intervalGroup := range r.checkResults {
for _, checkNameGroup := range intervalGroup {
results = append(results, checkNameGroup...)
}
}
return results
}
func (r *registry) createAlert(name string, target *target, result *check.Result, alertTTL time.Duration) *ammodels.PostableAlert {
labels := make(map[string]string, len(target.labels)+len(result.Labels)+4)
annotations := make(map[string]string, 2)
for k, v := range target.labels {
labels[k] = v
}
for k, v := range result.Labels {
labels[k] = v
}
labels[model.AlertNameLabel] = name
labels["severity"] = result.Severity.String()
labels["stt_check"] = "1"
labels["alert_id"] = makeID(target, result)
annotations["summary"] = result.Summary
annotations["description"] = result.Description
annotations["read_more_url"] = result.ReadMoreURL
endsAt := r.nowF().Add(alertTTL).UTC().Round(0) // strip a monotonic clock reading
return &ammodels.PostableAlert{
Alert: ammodels.Alert{
// GeneratorURL: "TODO",
Labels: labels,
},
EndsAt: strfmt.DateTime(endsAt),
Annotations: annotations,
}
}
// makeID creates an ID for STT check alert.
func makeID(target *target, result *check.Result) string {
s := sha1.New() //nolint:gosec
fmt.Fprintf(s, "%s\n", target.agentID)
fmt.Fprintf(s, "%s\n", target.serviceID)
fmt.Fprintf(s, "%s\n", result.Summary)
fmt.Fprintf(s, "%s\n", result.Description)
fmt.Fprintf(s, "%s\n", result.ReadMoreURL)
fmt.Fprintf(s, "%v\n", result.Severity)
return alertsPrefix + hex.EncodeToString(s.Sum(nil))
}