forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications_limit_flag.go
56 lines (45 loc) · 1.38 KB
/
notifications_limit_flag.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
package validation
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/frelon/loki/v2/pkg/util"
)
var allowedIntegrationNames = []string{
"webhook", "email", "pagerduty", "opsgenie", "wechat", "slack", "victorops", "pushover", "sns",
}
type NotificationRateLimitMap map[string]float64
// String implements flag.Value
func (m NotificationRateLimitMap) String() string {
out, err := json.Marshal(map[string]float64(m))
if err != nil {
return fmt.Sprintf("failed to marshal: %v", err)
}
return string(out)
}
// Set implements flag.Value
func (m NotificationRateLimitMap) Set(s string) error {
newMap := map[string]float64{}
return m.updateMap(json.Unmarshal([]byte(s), &newMap), newMap)
}
// UnmarshalYAML implements yaml.Unmarshaler.
func (m NotificationRateLimitMap) UnmarshalYAML(unmarshal func(interface{}) error) error {
newMap := map[string]float64{}
return m.updateMap(unmarshal(newMap), newMap)
}
func (m NotificationRateLimitMap) updateMap(unmarshalErr error, newMap map[string]float64) error {
if unmarshalErr != nil {
return unmarshalErr
}
for k, v := range newMap {
if !util.StringsContain(allowedIntegrationNames, k) {
return errors.Errorf("unknown integration name: %s", k)
}
m[k] = v
}
return nil
}
// MarshalYAML implements yaml.Marshaler.
func (m NotificationRateLimitMap) MarshalYAML() (interface{}, error) {
return map[string]float64(m), nil
}