-
Notifications
You must be signed in to change notification settings - Fork 796
/
notifier.go
141 lines (125 loc) · 3.81 KB
/
notifier.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
package ruler
import (
"context"
"fmt"
"strings"
"sync"
gklog "github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
config_util "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/discovery"
sd_config "github.com/prometheus/prometheus/discovery/config"
"github.com/prometheus/prometheus/discovery/dns"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/notifier"
)
// rulerNotifier bundles a notifier.Manager together with an associated
// Alertmanager service discovery manager and handles the lifecycle
// of both actors.
type rulerNotifier struct {
notifier *notifier.Manager
sdCancel context.CancelFunc
sdManager *discovery.Manager
wg sync.WaitGroup
logger gklog.Logger
}
func newRulerNotifier(o *notifier.Options, l gklog.Logger) *rulerNotifier {
sdCtx, sdCancel := context.WithCancel(context.Background())
return &rulerNotifier{
notifier: notifier.NewManager(o, l),
sdCancel: sdCancel,
sdManager: discovery.NewManager(sdCtx, l),
logger: l,
}
}
func (rn *rulerNotifier) run() {
rn.wg.Add(2)
go func() {
if err := rn.sdManager.Run(); err != nil {
level.Error(rn.logger).Log("msg", "error starting notifier discovery manager", "err", err)
}
rn.wg.Done()
}()
go func() {
rn.notifier.Run(rn.sdManager.SyncCh())
rn.wg.Done()
}()
}
func (rn *rulerNotifier) applyConfig(cfg *config.Config) error {
if err := rn.notifier.ApplyConfig(cfg); err != nil {
return err
}
sdCfgs := make(map[string]sd_config.ServiceDiscoveryConfig)
for k, v := range cfg.AlertingConfig.AlertmanagerConfigs.ToMap() {
sdCfgs[k] = v.ServiceDiscoveryConfig
}
return rn.sdManager.ApplyConfig(sdCfgs)
}
func (rn *rulerNotifier) stop() {
rn.sdCancel()
rn.notifier.Stop()
rn.wg.Wait()
}
// Builds a Prometheus config.Config from a ruler.Config with just the required
// options to configure notifications to Alertmanager.
func buildNotifierConfig(rulerConfig *Config) (*config.Config, error) {
if rulerConfig.AlertmanagerURL.URL == nil {
return &config.Config{}, nil
}
u := rulerConfig.AlertmanagerURL
var sdConfig sd_config.ServiceDiscoveryConfig
if rulerConfig.AlertmanagerDiscovery {
if !strings.Contains(u.Host, "_tcp.") {
return nil, fmt.Errorf("When alertmanager-discovery is on, host name must be of the form _portname._tcp.service.fqdn (is %q)", u.Host)
}
dnsSDConfig := dns.SDConfig{
Names: []string{u.Host},
RefreshInterval: model.Duration(rulerConfig.AlertmanagerRefreshInterval),
Type: "SRV",
Port: 0, // Ignored, because of SRV.
}
sdConfig = sd_config.ServiceDiscoveryConfig{
DNSSDConfigs: []*dns.SDConfig{&dnsSDConfig},
}
} else {
sdConfig = sd_config.ServiceDiscoveryConfig{
StaticConfigs: []*targetgroup.Group{
{
Targets: []model.LabelSet{
{
model.AddressLabel: model.LabelValue(u.Host),
},
},
},
},
}
}
amConfig := &config.AlertmanagerConfig{
APIVersion: config.AlertmanagerAPIVersionV1,
Scheme: u.Scheme,
PathPrefix: u.Path,
Timeout: model.Duration(rulerConfig.NotificationTimeout),
ServiceDiscoveryConfig: sdConfig,
}
if rulerConfig.AlertmanangerEnableV2API {
amConfig.APIVersion = config.AlertmanagerAPIVersionV2
}
promConfig := &config.Config{
AlertingConfig: config.AlertingConfig{
AlertmanagerConfigs: []*config.AlertmanagerConfig{amConfig},
},
}
if u.User != nil {
amConfig.HTTPClientConfig = config_util.HTTPClientConfig{
BasicAuth: &config_util.BasicAuth{
Username: u.User.Username(),
},
}
if password, isSet := u.User.Password(); isSet {
amConfig.HTTPClientConfig.BasicAuth.Password = config_util.Secret(password)
}
}
return promConfig, nil
}