-
Notifications
You must be signed in to change notification settings - Fork 814
/
Copy pathmultitenant.go
1309 lines (1101 loc) · 46.6 KB
/
multitenant.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package alertmanager
import (
"context"
"flag"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/alertmanager/cluster"
"github.com/prometheus/alertmanager/cluster/clusterpb"
amconfig "github.com/prometheus/alertmanager/config"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
tsdb_errors "github.com/prometheus/prometheus/tsdb/errors"
"github.com/weaveworks/common/httpgrpc"
"github.com/weaveworks/common/httpgrpc/server"
"github.com/weaveworks/common/user"
"golang.org/x/time/rate"
"github.com/cortexproject/cortex/pkg/alertmanager/alertmanagerpb"
"github.com/cortexproject/cortex/pkg/alertmanager/alertspb"
"github.com/cortexproject/cortex/pkg/alertmanager/alertstore"
"github.com/cortexproject/cortex/pkg/ring"
"github.com/cortexproject/cortex/pkg/ring/client"
"github.com/cortexproject/cortex/pkg/ring/kv"
"github.com/cortexproject/cortex/pkg/tenant"
"github.com/cortexproject/cortex/pkg/util"
"github.com/cortexproject/cortex/pkg/util/concurrency"
"github.com/cortexproject/cortex/pkg/util/flagext"
"github.com/cortexproject/cortex/pkg/util/services"
)
const (
// If a config sets the webhook URL to this, it will be rewritten to
// a URL derived from Config.AutoWebhookRoot
autoWebhookURL = "http://internal.monitor"
// Reasons for (re)syncing alertmanager configurations from object storage.
reasonPeriodic = "periodic"
reasonInitial = "initial"
reasonRingChange = "ring-change"
// ringAutoForgetUnhealthyPeriods is how many consecutive timeout periods an unhealthy instance
// in the ring will be automatically removed.
ringAutoForgetUnhealthyPeriods = 5
statusPage = `
<!doctype html>
<html>
<head><title>Cortex Alertmanager Status</title></head>
<body>
<h1>Cortex Alertmanager Status</h1>
<h2>Node</h2>
<dl>
<dt>Name</dt><dd>{{.self.Name}}</dd>
<dt>Addr</dt><dd>{{.self.Addr}}</dd>
<dt>Port</dt><dd>{{.self.Port}}</dd>
</dl>
<h3>Members</h3>
{{ with .members }}
<table>
<tr><th>Name</th><th>Addr</th></tr>
{{ range . }}
<tr><td>{{ .Name }}</td><td>{{ .Addr }}</td></tr>
{{ end }}
</table>
{{ else }}
<p>No peers</p>
{{ end }}
</body>
</html>
`
)
var (
statusTemplate *template.Template
errInvalidExternalURL = errors.New("the configured external URL is invalid: should not end with /")
)
func init() {
statusTemplate = template.Must(template.New("statusPage").Funcs(map[string]interface{}{
"state": func(enabled bool) string {
if enabled {
return "enabled"
}
return "disabled"
},
}).Parse(statusPage))
}
// MultitenantAlertmanagerConfig is the configuration for a multitenant Alertmanager.
type MultitenantAlertmanagerConfig struct {
DataDir string `yaml:"data_dir"`
Retention time.Duration `yaml:"retention"`
ExternalURL flagext.URLValue `yaml:"external_url"`
PollInterval time.Duration `yaml:"poll_interval"`
MaxRecvMsgSize int64 `yaml:"max_recv_msg_size"`
// Enable sharding for the Alertmanager
ShardingEnabled bool `yaml:"sharding_enabled"`
ShardingRing RingConfig `yaml:"sharding_ring"`
FallbackConfigFile string `yaml:"fallback_config_file"`
AutoWebhookRoot string `yaml:"auto_webhook_root"`
Store alertstore.LegacyConfig `yaml:"storage" doc:"description=Deprecated. Use -alertmanager-storage.* CLI flags and their respective YAML config options instead."`
Cluster ClusterConfig `yaml:"cluster"`
EnableAPI bool `yaml:"enable_api"`
// For distributor.
AlertmanagerClient ClientConfig `yaml:"alertmanager_client"`
// For the state persister.
Persister PersisterConfig `yaml:",inline"`
}
type ClusterConfig struct {
ListenAddr string `yaml:"listen_address"`
AdvertiseAddr string `yaml:"advertise_address"`
Peers flagext.StringSliceCSV `yaml:"peers"`
PeerTimeout time.Duration `yaml:"peer_timeout"`
GossipInterval time.Duration `yaml:"gossip_interval"`
PushPullInterval time.Duration `yaml:"push_pull_interval"`
}
const (
defaultClusterAddr = "0.0.0.0:9094"
defaultPeerTimeout = 15 * time.Second
)
// RegisterFlags adds the flags required to config this to the given FlagSet.
func (cfg *MultitenantAlertmanagerConfig) RegisterFlags(f *flag.FlagSet) {
f.StringVar(&cfg.DataDir, "alertmanager.storage.path", "data/", "Base path for data storage.")
f.DurationVar(&cfg.Retention, "alertmanager.storage.retention", 5*24*time.Hour, "How long to keep data for.")
f.Int64Var(&cfg.MaxRecvMsgSize, "alertmanager.max-recv-msg-size", 16<<20, "Maximum size (bytes) of an accepted HTTP request body.")
f.Var(&cfg.ExternalURL, "alertmanager.web.external-url", "The URL under which Alertmanager is externally reachable (for example, if Alertmanager is served via a reverse proxy). Used for generating relative and absolute links back to Alertmanager itself. If the URL has a path portion, it will be used to prefix all HTTP endpoints served by Alertmanager. If omitted, relevant URL components will be derived automatically.")
f.StringVar(&cfg.FallbackConfigFile, "alertmanager.configs.fallback", "", "Filename of fallback config to use if none specified for instance.")
f.StringVar(&cfg.AutoWebhookRoot, "alertmanager.configs.auto-webhook-root", "", "Root of URL to generate if config is "+autoWebhookURL)
f.DurationVar(&cfg.PollInterval, "alertmanager.configs.poll-interval", 15*time.Second, "How frequently to poll Cortex configs")
f.BoolVar(&cfg.EnableAPI, "experimental.alertmanager.enable-api", false, "Enable the experimental alertmanager config api.")
f.BoolVar(&cfg.ShardingEnabled, "alertmanager.sharding-enabled", false, "Shard tenants across multiple alertmanager instances.")
cfg.AlertmanagerClient.RegisterFlagsWithPrefix("alertmanager.alertmanager-client", f)
cfg.Persister.RegisterFlagsWithPrefix("alertmanager", f)
cfg.ShardingRing.RegisterFlags(f)
cfg.Store.RegisterFlags(f)
cfg.Cluster.RegisterFlags(f)
}
func (cfg *ClusterConfig) RegisterFlags(f *flag.FlagSet) {
prefix := "alertmanager.cluster."
f.StringVar(&cfg.ListenAddr, prefix+"listen-address", defaultClusterAddr, "Listen address and port for the cluster. Not specifying this flag disables high-availability mode.")
f.StringVar(&cfg.AdvertiseAddr, prefix+"advertise-address", "", "Explicit address or hostname to advertise in cluster.")
f.Var(&cfg.Peers, prefix+"peers", "Comma-separated list of initial peers.")
f.DurationVar(&cfg.PeerTimeout, prefix+"peer-timeout", defaultPeerTimeout, "Time to wait between peers to send notifications.")
f.DurationVar(&cfg.GossipInterval, prefix+"gossip-interval", cluster.DefaultGossipInterval, "The interval between sending gossip messages. By lowering this value (more frequent) gossip messages are propagated across cluster more quickly at the expense of increased bandwidth usage.")
f.DurationVar(&cfg.PushPullInterval, prefix+"push-pull-interval", cluster.DefaultPushPullInterval, "The interval between gossip state syncs. Setting this interval lower (more frequent) will increase convergence speeds across larger clusters at the expense of increased bandwidth usage.")
}
// Validate config and returns error on failure
func (cfg *MultitenantAlertmanagerConfig) Validate() error {
if cfg.ExternalURL.URL != nil && strings.HasSuffix(cfg.ExternalURL.Path, "/") {
return errInvalidExternalURL
}
if err := cfg.Store.Validate(); err != nil {
return errors.Wrap(err, "invalid storage config")
}
if err := cfg.Persister.Validate(); err != nil {
return err
}
return nil
}
type multitenantAlertmanagerMetrics struct {
lastReloadSuccessful *prometheus.GaugeVec
lastReloadSuccessfulTimestamp *prometheus.GaugeVec
}
func newMultitenantAlertmanagerMetrics(reg prometheus.Registerer) *multitenantAlertmanagerMetrics {
m := &multitenantAlertmanagerMetrics{}
m.lastReloadSuccessful = promauto.With(reg).NewGaugeVec(prometheus.GaugeOpts{
Namespace: "cortex",
Name: "alertmanager_config_last_reload_successful",
Help: "Boolean set to 1 whenever the last configuration reload attempt was successful.",
}, []string{"user"})
m.lastReloadSuccessfulTimestamp = promauto.With(reg).NewGaugeVec(prometheus.GaugeOpts{
Namespace: "cortex",
Name: "alertmanager_config_last_reload_successful_seconds",
Help: "Timestamp of the last successful configuration reload.",
}, []string{"user"})
return m
}
// Limits defines limits used by Alertmanager.
type Limits interface {
// AlertmanagerReceiversBlockCIDRNetworks returns the list of network CIDRs that should be blocked
// in the Alertmanager receivers for the given user.
AlertmanagerReceiversBlockCIDRNetworks(user string) []flagext.CIDR
// AlertmanagerReceiversBlockPrivateAddresses returns true if private addresses should be blocked
// in the Alertmanager receivers for the given user.
AlertmanagerReceiversBlockPrivateAddresses(user string) bool
// EmailNotificationRateLimit returns limit used by rate-limiter. If set to 0, no emails are allowed.
// rate.Inf = all emails are allowed.
//
// Note that when negative or zero values specified by user are translated to rate.Limit by Overrides,
// and may have different meaning there.
EmailNotificationRateLimit(tenant string) rate.Limit
// EmailNotificationBurst returns burst-size for rate limiter. If 0, no notifications are allowed except
// when limit == rate.Inf.
EmailNotificationBurst(tenant string) int
}
// A MultitenantAlertmanager manages Alertmanager instances for multiple
// organizations.
type MultitenantAlertmanager struct {
services.Service
cfg *MultitenantAlertmanagerConfig
// Ring used for sharding alertmanager instances.
// When sharding is disabled, the flow is:
// ServeHTTP() -> serveRequest()
// When sharding is enabled:
// ServeHTTP() -> distributor.DistributeRequest() -> (sends to other AM or even the current)
// -> HandleRequest() (gRPC call) -> grpcServer() -> handlerForGRPCServer.ServeHTTP() -> serveRequest().
ringLifecycler *ring.BasicLifecycler
ring *ring.Ring
distributor *Distributor
grpcServer *server.Server
// Subservices manager (ring, lifecycler)
subservices *services.Manager
subservicesWatcher *services.FailureWatcher
store alertstore.AlertStore
// The fallback config is stored as a string and parsed every time it's needed
// because we mutate the parsed results and don't want those changes to take
// effect here.
fallbackConfig string
alertmanagersMtx sync.Mutex
alertmanagers map[string]*Alertmanager
// Stores the current set of configurations we're running in each tenant's Alertmanager.
// Used for comparing configurations as we synchronize them.
cfgs map[string]alertspb.AlertConfigDesc
logger log.Logger
alertmanagerMetrics *alertmanagerMetrics
multitenantMetrics *multitenantAlertmanagerMetrics
peer *cluster.Peer
alertmanagerClientsPool ClientsPool
limits Limits
registry prometheus.Registerer
ringCheckErrors prometheus.Counter
tenantsOwned prometheus.Gauge
tenantsDiscovered prometheus.Gauge
syncTotal *prometheus.CounterVec
syncFailures *prometheus.CounterVec
}
// NewMultitenantAlertmanager creates a new MultitenantAlertmanager.
func NewMultitenantAlertmanager(cfg *MultitenantAlertmanagerConfig, store alertstore.AlertStore, limits Limits, logger log.Logger, registerer prometheus.Registerer) (*MultitenantAlertmanager, error) {
err := os.MkdirAll(cfg.DataDir, 0777)
if err != nil {
return nil, fmt.Errorf("unable to create Alertmanager data directory %q: %s", cfg.DataDir, err)
}
if cfg.ExternalURL.URL == nil {
return nil, fmt.Errorf("unable to create Alertmanager because the external URL has not been configured")
}
var fallbackConfig []byte
if cfg.FallbackConfigFile != "" {
fallbackConfig, err = ioutil.ReadFile(cfg.FallbackConfigFile)
if err != nil {
return nil, fmt.Errorf("unable to read fallback config %q: %s", cfg.FallbackConfigFile, err)
}
_, err = amconfig.LoadFile(cfg.FallbackConfigFile)
if err != nil {
return nil, fmt.Errorf("unable to load fallback config %q: %s", cfg.FallbackConfigFile, err)
}
}
var peer *cluster.Peer
// We need to take this case into account to support our legacy upstream clustering.
if cfg.Cluster.ListenAddr != "" && !cfg.ShardingEnabled {
peer, err = cluster.Create(
log.With(logger, "component", "cluster"),
registerer,
cfg.Cluster.ListenAddr,
cfg.Cluster.AdvertiseAddr,
cfg.Cluster.Peers,
true,
cfg.Cluster.PushPullInterval,
cfg.Cluster.GossipInterval,
cluster.DefaultTcpTimeout,
cluster.DefaultProbeTimeout,
cluster.DefaultProbeInterval,
)
if err != nil {
return nil, errors.Wrap(err, "unable to initialize gossip mesh")
}
err = peer.Join(cluster.DefaultReconnectInterval, cluster.DefaultReconnectTimeout)
if err != nil {
level.Warn(logger).Log("msg", "unable to join gossip mesh while initializing cluster for high availability mode", "err", err)
}
go peer.Settle(context.Background(), cluster.DefaultGossipInterval)
}
var ringStore kv.Client
if cfg.ShardingEnabled {
ringStore, err = kv.NewClient(
cfg.ShardingRing.KVStore,
ring.GetCodec(),
kv.RegistererWithKVName(registerer, "alertmanager"),
)
if err != nil {
return nil, errors.Wrap(err, "create KV store client")
}
}
return createMultitenantAlertmanager(cfg, fallbackConfig, peer, store, ringStore, limits, logger, registerer)
}
func createMultitenantAlertmanager(cfg *MultitenantAlertmanagerConfig, fallbackConfig []byte, peer *cluster.Peer, store alertstore.AlertStore, ringStore kv.Client, limits Limits, logger log.Logger, registerer prometheus.Registerer) (*MultitenantAlertmanager, error) {
am := &MultitenantAlertmanager{
cfg: cfg,
fallbackConfig: string(fallbackConfig),
cfgs: map[string]alertspb.AlertConfigDesc{},
alertmanagers: map[string]*Alertmanager{},
alertmanagerMetrics: newAlertmanagerMetrics(),
multitenantMetrics: newMultitenantAlertmanagerMetrics(registerer),
peer: peer,
store: store,
logger: log.With(logger, "component", "MultiTenantAlertmanager"),
registry: registerer,
limits: limits,
ringCheckErrors: promauto.With(registerer).NewCounter(prometheus.CounterOpts{
Name: "cortex_alertmanager_ring_check_errors_total",
Help: "Number of errors that have occurred when checking the ring for ownership.",
}),
syncTotal: promauto.With(registerer).NewCounterVec(prometheus.CounterOpts{
Name: "cortex_alertmanager_sync_configs_total",
Help: "Total number of times the alertmanager sync operation triggered.",
}, []string{"reason"}),
syncFailures: promauto.With(registerer).NewCounterVec(prometheus.CounterOpts{
Name: "cortex_alertmanager_sync_configs_failed_total",
Help: "Total number of times the alertmanager sync operation failed.",
}, []string{"reason"}),
tenantsDiscovered: promauto.With(registerer).NewGauge(prometheus.GaugeOpts{
Name: "cortex_alertmanager_tenants_discovered",
Help: "Number of tenants with an Alertmanager configuration discovered.",
}),
tenantsOwned: promauto.With(registerer).NewGauge(prometheus.GaugeOpts{
Name: "cortex_alertmanager_tenants_owned",
Help: "Current number of tenants owned by the Alertmanager instance.",
}),
}
// Initialize the top-level metrics.
for _, r := range []string{reasonInitial, reasonPeriodic, reasonRingChange} {
am.syncTotal.WithLabelValues(r)
am.syncFailures.WithLabelValues(r)
}
if cfg.ShardingEnabled {
lifecyclerCfg, err := am.cfg.ShardingRing.ToLifecyclerConfig()
if err != nil {
return nil, errors.Wrap(err, "failed to initialize Alertmanager's lifecycler config")
}
// Define lifecycler delegates in reverse order (last to be called defined first because they're
// chained via "next delegate").
delegate := ring.BasicLifecyclerDelegate(am)
delegate = ring.NewLeaveOnStoppingDelegate(delegate, am.logger)
delegate = ring.NewAutoForgetDelegate(am.cfg.ShardingRing.HeartbeatTimeout*ringAutoForgetUnhealthyPeriods, delegate, am.logger)
am.ringLifecycler, err = ring.NewBasicLifecycler(lifecyclerCfg, RingNameForServer, RingKey, ringStore, delegate, am.logger, am.registry)
if err != nil {
return nil, errors.Wrap(err, "failed to initialize Alertmanager's lifecycler")
}
am.ring, err = ring.NewWithStoreClientAndStrategy(am.cfg.ShardingRing.ToRingConfig(), RingNameForServer, RingKey, ringStore, ring.NewIgnoreUnhealthyInstancesReplicationStrategy())
if err != nil {
return nil, errors.Wrap(err, "failed to initialize Alertmanager's ring")
}
if am.registry != nil {
am.registry.MustRegister(am.ring)
}
am.grpcServer = server.NewServer(&handlerForGRPCServer{am: am})
am.alertmanagerClientsPool = newAlertmanagerClientsPool(client.NewRingServiceDiscovery(am.ring), cfg.AlertmanagerClient, logger, am.registry)
am.distributor, err = NewDistributor(cfg.AlertmanagerClient, cfg.MaxRecvMsgSize, am.ring, am.alertmanagerClientsPool, log.With(logger, "component", "AlertmanagerDistributor"), am.registry)
if err != nil {
return nil, errors.Wrap(err, "create distributor")
}
}
if registerer != nil {
registerer.MustRegister(am.alertmanagerMetrics)
}
am.Service = services.NewBasicService(am.starting, am.run, am.stopping)
return am, nil
}
// handlerForGRPCServer acts as a handler for gRPC server to serve
// the serveRequest() via the standard ServeHTTP.
type handlerForGRPCServer struct {
am *MultitenantAlertmanager
}
func (h *handlerForGRPCServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
h.am.serveRequest(w, req)
}
func (am *MultitenantAlertmanager) starting(ctx context.Context) (err error) {
err = am.migrateStateFilesToPerTenantDirectories()
if err != nil {
return err
}
defer func() {
if err == nil || am.subservices == nil {
return
}
if stopErr := services.StopManagerAndAwaitStopped(context.Background(), am.subservices); stopErr != nil {
level.Error(am.logger).Log("msg", "failed to gracefully stop alertmanager dependencies", "err", stopErr)
}
}()
if am.cfg.ShardingEnabled {
if am.subservices, err = services.NewManager(am.ringLifecycler, am.ring, am.distributor); err != nil {
return errors.Wrap(err, "failed to start alertmanager's subservices")
}
if err = services.StartManagerAndAwaitHealthy(ctx, am.subservices); err != nil {
return errors.Wrap(err, "failed to start alertmanager's subservices")
}
am.subservicesWatcher = services.NewFailureWatcher()
am.subservicesWatcher.WatchManager(am.subservices)
// We wait until the instance is in the JOINING state, once it does we know that tokens are assigned to this instance and we'll be ready to perform an initial sync of configs.
level.Info(am.logger).Log("waiting until alertmanager is JOINING in the ring")
if err = ring.WaitInstanceState(ctx, am.ring, am.ringLifecycler.GetInstanceID(), ring.JOINING); err != nil {
return err
}
level.Info(am.logger).Log("msg", "alertmanager is JOINING in the ring")
}
// At this point, if sharding is enabled, the instance is registered with some tokens
// and we can run the initial iteration to sync configs. If no sharding is enabled we load _all_ the configs.
if err := am.loadAndSyncConfigs(ctx, reasonInitial); err != nil {
return err
}
if am.cfg.ShardingEnabled {
// With the initial sync now completed, we should have loaded all assigned alertmanager configurations to this instance. We can switch it to ACTIVE and start serving requests.
if err := am.ringLifecycler.ChangeState(ctx, ring.ACTIVE); err != nil {
return errors.Wrapf(err, "switch instance to %s in the ring", ring.ACTIVE)
}
// Wait until the ring client detected this instance in the ACTIVE state.
level.Info(am.logger).Log("msg", "waiting until alertmanager is ACTIVE in the ring")
if err := ring.WaitInstanceState(ctx, am.ring, am.ringLifecycler.GetInstanceID(), ring.ACTIVE); err != nil {
return err
}
level.Info(am.logger).Log("msg", "alertmanager is ACTIVE in the ring")
}
return nil
}
// migrateStateFilesToPerTenantDirectories migrates any existing configuration from old place to new hierarchy.
// TODO: Remove in Cortex 1.11.
func (am *MultitenantAlertmanager) migrateStateFilesToPerTenantDirectories() error {
migrate := func(from, to string) error {
level.Info(am.logger).Log("msg", "migrating alertmanager state", "from", from, "to", to)
err := os.Rename(from, to)
return errors.Wrapf(err, "failed to migrate alertmanager state from %v to %v", from, to)
}
st, err := am.getObsoleteFilesPerUser()
if err != nil {
return errors.Wrap(err, "failed to migrate alertmanager state files")
}
for userID, files := range st {
tenantDir := am.getTenantDirectory(userID)
err := os.MkdirAll(tenantDir, 0777)
if err != nil {
return errors.Wrapf(err, "failed to create per-tenant directory %v", tenantDir)
}
errs := tsdb_errors.NewMulti()
if files.notificationLogSnapshot != "" {
errs.Add(migrate(files.notificationLogSnapshot, filepath.Join(tenantDir, notificationLogSnapshot)))
}
if files.silencesSnapshot != "" {
errs.Add(migrate(files.silencesSnapshot, filepath.Join(tenantDir, silencesSnapshot)))
}
if files.templatesDir != "" {
errs.Add(migrate(files.templatesDir, filepath.Join(tenantDir, templatesDir)))
}
if err := errs.Err(); err != nil {
return err
}
}
return nil
}
type obsoleteStateFiles struct {
notificationLogSnapshot string
silencesSnapshot string
templatesDir string
}
// getObsoleteFilesPerUser returns per-user set of files that should be migrated from old structure to new structure.
func (am *MultitenantAlertmanager) getObsoleteFilesPerUser() (map[string]obsoleteStateFiles, error) {
files, err := ioutil.ReadDir(am.cfg.DataDir)
if err != nil {
return nil, errors.Wrapf(err, "failed to list dir %v", am.cfg.DataDir)
}
// old names
const (
notificationLogPrefix = "nflog:"
silencesPrefix = "silences:"
templates = "templates"
)
result := map[string]obsoleteStateFiles{}
for _, f := range files {
fullPath := filepath.Join(am.cfg.DataDir, f.Name())
if f.IsDir() {
// Process templates dir.
if f.Name() != templates {
// Ignore other files -- those are likely per tenant directories.
continue
}
templateDirs, err := ioutil.ReadDir(fullPath)
if err != nil {
return nil, errors.Wrapf(err, "failed to list dir %v", fullPath)
}
// Previously templates directory contained per-tenant subdirectory.
for _, d := range templateDirs {
if d.IsDir() {
v := result[d.Name()]
v.templatesDir = filepath.Join(fullPath, d.Name())
result[d.Name()] = v
} else {
level.Warn(am.logger).Log("msg", "ignoring unknown local file while migrating local alertmanager state files", "file", filepath.Join(fullPath, d.Name()))
}
}
continue
}
switch {
case strings.HasPrefix(f.Name(), notificationLogPrefix):
userID := strings.TrimPrefix(f.Name(), notificationLogPrefix)
v := result[userID]
v.notificationLogSnapshot = fullPath
result[userID] = v
case strings.HasPrefix(f.Name(), silencesPrefix):
userID := strings.TrimPrefix(f.Name(), silencesPrefix)
v := result[userID]
v.silencesSnapshot = fullPath
result[userID] = v
default:
level.Warn(am.logger).Log("msg", "ignoring unknown local data file while migrating local alertmanager state files", "file", fullPath)
}
}
return result, nil
}
func (am *MultitenantAlertmanager) run(ctx context.Context) error {
tick := time.NewTicker(am.cfg.PollInterval)
defer tick.Stop()
var ringTickerChan <-chan time.Time
var ringLastState ring.ReplicationSet
if am.cfg.ShardingEnabled {
ringLastState, _ = am.ring.GetAllHealthy(RingOp)
ringTicker := time.NewTicker(util.DurationWithJitter(am.cfg.ShardingRing.RingCheckPeriod, 0.2))
defer ringTicker.Stop()
ringTickerChan = ringTicker.C
}
for {
select {
case <-ctx.Done():
return nil
case err := <-am.subservicesWatcher.Chan():
return errors.Wrap(err, "alertmanager subservices failed")
case <-tick.C:
// We don't want to halt execution here but instead just log what happened.
if err := am.loadAndSyncConfigs(ctx, reasonPeriodic); err != nil {
level.Warn(am.logger).Log("msg", "error while synchronizing alertmanager configs", "err", err)
}
case <-ringTickerChan:
// We ignore the error because in case of error it will return an empty
// replication set which we use to compare with the previous state.
currRingState, _ := am.ring.GetAllHealthy(RingOp)
if ring.HasReplicationSetChanged(ringLastState, currRingState) {
ringLastState = currRingState
if err := am.loadAndSyncConfigs(ctx, reasonRingChange); err != nil {
level.Warn(am.logger).Log("msg", "error while synchronizing alertmanager configs", "err", err)
}
}
}
}
}
func (am *MultitenantAlertmanager) loadAndSyncConfigs(ctx context.Context, syncReason string) error {
level.Info(am.logger).Log("msg", "synchronizing alertmanager configs for users")
am.syncTotal.WithLabelValues(syncReason).Inc()
cfgs, err := am.loadAlertmanagerConfigs(ctx)
if err != nil {
am.syncFailures.WithLabelValues(syncReason).Inc()
return err
}
am.syncConfigs(cfgs)
am.deleteUnusedLocalUserState()
return nil
}
// stopping runs when MultitenantAlertmanager transitions to Stopping state.
func (am *MultitenantAlertmanager) stopping(_ error) error {
am.alertmanagersMtx.Lock()
for _, am := range am.alertmanagers {
am.StopAndWait()
}
am.alertmanagersMtx.Unlock()
if am.peer != nil { // Tests don't setup any peer.
err := am.peer.Leave(am.cfg.Cluster.PeerTimeout)
if err != nil {
level.Warn(am.logger).Log("msg", "failed to leave the cluster", "err", err)
}
}
if am.subservices != nil {
// subservices manages ring and lifecycler, if sharding was enabled.
_ = services.StopManagerAndAwaitStopped(context.Background(), am.subservices)
}
return nil
}
// loadAlertmanagerConfigs Loads (and filters) the alertmanagers configuration from object storage, taking into consideration the sharding strategy.
func (am *MultitenantAlertmanager) loadAlertmanagerConfigs(ctx context.Context) (map[string]alertspb.AlertConfigDesc, error) {
// Find all users with an alertmanager config.
userIDs, err := am.store.ListAllUsers(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to list users with alertmanager configuration")
}
numUsersDiscovered := len(userIDs)
// Filter out users not owned by this shard.
for i := 0; i < len(userIDs); {
if !am.isUserOwned(userIDs[i]) {
userIDs = append(userIDs[:i], userIDs[i+1:]...)
continue
}
i++
}
numUsersOwned := len(userIDs)
// Load the configs for the owned users.
configs, err := am.store.GetAlertConfigs(ctx, userIDs)
if err != nil {
return nil, errors.Wrapf(err, "failed to load alertmanager configurations for owned users")
}
am.tenantsDiscovered.Set(float64(numUsersDiscovered))
am.tenantsOwned.Set(float64(numUsersOwned))
return configs, nil
}
func (am *MultitenantAlertmanager) isUserOwned(userID string) bool {
// If sharding is disabled, any alertmanager instance owns all users.
if !am.cfg.ShardingEnabled {
return true
}
alertmanagers, err := am.ring.Get(shardByUser(userID), SyncRingOp, nil, nil, nil)
if err != nil {
am.ringCheckErrors.Inc()
level.Error(am.logger).Log("msg", "failed to load alertmanager configuration", "user", userID, "err", err)
return false
}
return alertmanagers.Includes(am.ringLifecycler.GetInstanceAddr())
}
func (am *MultitenantAlertmanager) syncConfigs(cfgs map[string]alertspb.AlertConfigDesc) {
level.Debug(am.logger).Log("msg", "adding configurations", "num_configs", len(cfgs))
for user, cfg := range cfgs {
err := am.setConfig(cfg)
if err != nil {
am.multitenantMetrics.lastReloadSuccessful.WithLabelValues(user).Set(float64(0))
level.Warn(am.logger).Log("msg", "error applying config", "err", err)
continue
}
am.multitenantMetrics.lastReloadSuccessful.WithLabelValues(user).Set(float64(1))
am.multitenantMetrics.lastReloadSuccessfulTimestamp.WithLabelValues(user).SetToCurrentTime()
}
userAlertmanagersToStop := map[string]*Alertmanager{}
am.alertmanagersMtx.Lock()
for userID, userAM := range am.alertmanagers {
if _, exists := cfgs[userID]; !exists {
userAlertmanagersToStop[userID] = userAM
delete(am.alertmanagers, userID)
delete(am.cfgs, userID)
am.multitenantMetrics.lastReloadSuccessful.DeleteLabelValues(userID)
am.multitenantMetrics.lastReloadSuccessfulTimestamp.DeleteLabelValues(userID)
am.alertmanagerMetrics.removeUserRegistry(userID)
}
}
am.alertmanagersMtx.Unlock()
// Now stop alertmanagers and wait until they are really stopped, without holding lock.
for userID, userAM := range userAlertmanagersToStop {
level.Info(am.logger).Log("msg", "deactivating per-tenant alertmanager", "user", userID)
userAM.StopAndWait()
level.Info(am.logger).Log("msg", "deactivated per-tenant alertmanager", "user", userID)
}
}
// setConfig applies the given configuration to the alertmanager for `userID`,
// creating an alertmanager if it doesn't already exist.
func (am *MultitenantAlertmanager) setConfig(cfg alertspb.AlertConfigDesc) error {
var userAmConfig *amconfig.Config
var err error
var hasTemplateChanges bool
for _, tmpl := range cfg.Templates {
templateFilepath, err := safeTemplateFilepath(filepath.Join(am.getTenantDirectory(cfg.User), templatesDir), tmpl.Filename)
if err != nil {
return err
}
hasChanged, err := storeTemplateFile(templateFilepath, tmpl.Body)
if err != nil {
return err
}
if hasChanged {
hasTemplateChanges = true
}
}
level.Debug(am.logger).Log("msg", "setting config", "user", cfg.User)
am.alertmanagersMtx.Lock()
defer am.alertmanagersMtx.Unlock()
existing, hasExisting := am.alertmanagers[cfg.User]
rawCfg := cfg.RawConfig
if cfg.RawConfig == "" {
if am.fallbackConfig == "" {
return fmt.Errorf("blank Alertmanager configuration for %v", cfg.User)
}
level.Debug(am.logger).Log("msg", "blank Alertmanager configuration; using fallback", "user", cfg.User)
userAmConfig, err = amconfig.Load(am.fallbackConfig)
if err != nil {
return fmt.Errorf("unable to load fallback configuration for %v: %v", cfg.User, err)
}
rawCfg = am.fallbackConfig
} else {
userAmConfig, err = amconfig.Load(cfg.RawConfig)
if err != nil && hasExisting {
// This means that if a user has a working config and
// they submit a broken one, the Manager will keep running the last known
// working configuration.
return fmt.Errorf("invalid Cortex configuration for %v: %v", cfg.User, err)
}
}
// We can have an empty configuration here if:
// 1) the user had a previous alertmanager
// 2) then, submitted a non-working configuration (and we kept running the prev working config)
// 3) finally, the cortex AM instance is restarted and the running version is no longer present
if userAmConfig == nil {
return fmt.Errorf("no usable Alertmanager configuration for %v", cfg.User)
}
// Transform webhook configs URLs to the per tenant monitor
if am.cfg.AutoWebhookRoot != "" {
for i, r := range userAmConfig.Receivers {
for j, w := range r.WebhookConfigs {
if w.URL.String() == autoWebhookURL {
u, err := url.Parse(am.cfg.AutoWebhookRoot + "/" + cfg.User + "/monitor")
if err != nil {
return err
}
userAmConfig.Receivers[i].WebhookConfigs[j].URL = &amconfig.URL{URL: u}
}
}
}
}
// If no Alertmanager instance exists for this user yet, start one.
if !hasExisting {
level.Debug(am.logger).Log("msg", "initializing new per-tenant alertmanager", "user", cfg.User)
newAM, err := am.newAlertmanager(cfg.User, userAmConfig, rawCfg)
if err != nil {
return err
}
am.alertmanagers[cfg.User] = newAM
} else if am.cfgs[cfg.User].RawConfig != cfg.RawConfig || hasTemplateChanges {
level.Info(am.logger).Log("msg", "updating new per-tenant alertmanager", "user", cfg.User)
// If the config changed, apply the new one.
err := existing.ApplyConfig(cfg.User, userAmConfig, rawCfg)
if err != nil {
return fmt.Errorf("unable to apply Alertmanager config for user %v: %v", cfg.User, err)
}
}
am.cfgs[cfg.User] = cfg
return nil
}
func (am *MultitenantAlertmanager) getTenantDirectory(userID string) string {
return filepath.Join(am.cfg.DataDir, userID)
}
func (am *MultitenantAlertmanager) newAlertmanager(userID string, amConfig *amconfig.Config, rawCfg string) (*Alertmanager, error) {
reg := prometheus.NewRegistry()
tenantDir := am.getTenantDirectory(userID)
err := os.MkdirAll(tenantDir, 0777)
if err != nil {
return nil, errors.Wrapf(err, "failed to create per-tenant directory %v", tenantDir)
}
newAM, err := New(&Config{
UserID: userID,
TenantDataDir: tenantDir,
Logger: am.logger,
Peer: am.peer,
PeerTimeout: am.cfg.Cluster.PeerTimeout,
Retention: am.cfg.Retention,
ExternalURL: am.cfg.ExternalURL.URL,
ShardingEnabled: am.cfg.ShardingEnabled,
Replicator: am,
ReplicationFactor: am.cfg.ShardingRing.ReplicationFactor,
Store: am.store,
PersisterConfig: am.cfg.Persister,
Limits: am.limits,
}, reg)
if err != nil {
return nil, fmt.Errorf("unable to start Alertmanager for user %v: %v", userID, err)
}
if err := newAM.ApplyConfig(userID, amConfig, rawCfg); err != nil {
return nil, fmt.Errorf("unable to apply initial config for user %v: %v", userID, err)
}
am.alertmanagerMetrics.addUserRegistry(userID, reg)
return newAM, nil
}
// GetPositionForUser returns the position this Alertmanager instance holds in the ring related to its other replicas for an specific user.
func (am *MultitenantAlertmanager) GetPositionForUser(userID string) int {
// If we have a replication factor of 1 or less we don't need to do any work and can immediately return.
if am.ring == nil || am.ring.ReplicationFactor() <= 1 {
return 0
}
set, err := am.ring.Get(shardByUser(userID), RingOp, nil, nil, nil)
if err != nil {
level.Error(am.logger).Log("msg", "unable to read the ring while trying to determine the alertmanager position", "err", err)
// If we're unable to determine the position, we don't want a tenant to miss out on the notification - instead,
// just assume we're the first in line and run the risk of a double notification.
return 0
}
var position int
for i, instance := range set.Instances {
if instance.Addr == am.ringLifecycler.GetInstanceAddr() {
position = i
break
}
}
return position
}
// ServeHTTP serves the Alertmanager's web UI and API.
func (am *MultitenantAlertmanager) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if am.State() != services.Running {
http.Error(w, "Alertmanager not ready", http.StatusServiceUnavailable)
return
}
if am.cfg.ShardingEnabled && am.distributor.IsPathSupported(req.URL.Path) {
am.distributor.DistributeRequest(w, req)
return
}
// If sharding is not enabled or Distributor does not support this path,
// it is served by this instance.
am.serveRequest(w, req)
}
// HandleRequest implements gRPC Alertmanager service, which receives request from AlertManager-Distributor.
func (am *MultitenantAlertmanager) HandleRequest(ctx context.Context, in *httpgrpc.HTTPRequest) (*httpgrpc.HTTPResponse, error) {
return am.grpcServer.Handle(ctx, in)
}
// serveRequest serves the Alertmanager's web UI and API.
func (am *MultitenantAlertmanager) serveRequest(w http.ResponseWriter, req *http.Request) {
userID, err := tenant.TenantID(req.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
am.alertmanagersMtx.Lock()
userAM, ok := am.alertmanagers[userID]
am.alertmanagersMtx.Unlock()
if ok {
userAM.mux.ServeHTTP(w, req)
return
}
if am.fallbackConfig != "" {
userAM, err = am.alertmanagerFromFallbackConfig(userID)
if err != nil {
level.Error(am.logger).Log("msg", "unable to initialize the Alertmanager with a fallback configuration", "user", userID, "err", err)
http.Error(w, "Failed to initialize the Alertmanager", http.StatusInternalServerError)
return
}
userAM.mux.ServeHTTP(w, req)
return
}
level.Debug(am.logger).Log("msg", "the Alertmanager has no configuration and no fallback specified", "user", userID)
http.Error(w, "the Alertmanager is not configured", http.StatusNotFound)
}
func (am *MultitenantAlertmanager) alertmanagerFromFallbackConfig(userID string) (*Alertmanager, error) {
// Upload an empty config so that the Alertmanager is no de-activated in the next poll
cfgDesc := alertspb.ToProto("", nil, userID)