2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								package  notifier 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								import  ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									"context" 
							 
						 
					
						
							
								
									
										
										
										
											2024-05-03 15:32:30 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									"errors" 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
									"fmt" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									"sync" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									"time" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-01-30 09:55:35 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									"github.com/prometheus/client_golang/prometheus" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-04-09 13:39:34 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									alertingCluster  "github.com/grafana/alerting/cluster" 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-13 07:11:35 +03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									"github.com/grafana/grafana/pkg/apimachinery/errutil" 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-09 13:39:34 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-02-03 11:36:49 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									alertingNotify  "github.com/grafana/alerting/notify" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-09-09 17:25:22 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									"github.com/grafana/grafana/pkg/infra/kvstore" 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
									"github.com/grafana/grafana/pkg/infra/log" 
							 
						 
					
						
							
								
									
										
										
										
											2024-02-15 09:45:10 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									"github.com/grafana/grafana/pkg/services/featuremgmt" 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-06 07:59:29 -03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									apimodels  "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
									"github.com/grafana/grafana/pkg/services/ngalert/metrics" 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-21 11:01:23 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									"github.com/grafana/grafana/pkg/services/ngalert/models" 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
									"github.com/grafana/grafana/pkg/services/ngalert/store" 
							 
						 
					
						
							
								
									
										
										
										
											2023-01-12 14:47:22 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									"github.com/grafana/grafana/pkg/services/notifications" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									"github.com/grafana/grafana/pkg/services/secrets" 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
									"github.com/grafana/grafana/pkg/setting" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								var  ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									ErrNoAlertmanagerForOrg  =  fmt . Errorf ( "Alertmanager does not exist for this organization" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									ErrAlertmanagerNotReady  =  fmt . Errorf ( "Alertmanager is not ready yet" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-05-03 15:32:30 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								// errutil-based errors. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// TODO: Should completely replace the fmt.Errorf-based errors. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								var  ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									ErrAlertmanagerNotFound  =  errutil . NotFound ( "alerting.notifications.alertmanager.notFound" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									ErrAlertmanagerConflict  =  errutil . Conflict ( "alerting.notifications.alertmanager.conflict" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									ErrSilenceNotFound     =  errutil . NotFound ( "alerting.notifications.silences.notFound" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									ErrSilencesBadRequest  =  errutil . BadRequest ( "alerting.notifications.silences.badRequest" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									ErrSilenceInternal     =  errutil . Internal ( "alerting.notifications.silences.internal" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-11-08 12:03:40 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								//go:generate mockery --name Alertmanager --structname AlertmanagerMock --with-expecter --output alertmanager_mock --outpkg alertmanager_mock 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-06 07:59:29 -03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								type  Alertmanager  interface  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									// Configuration 
							 
						 
					
						
							
								
									
										
										
										
											2023-10-19 11:27:37 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									ApplyConfig ( context . Context ,  * models . AlertConfiguration )  error 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-06 07:59:29 -03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									SaveAndApplyConfig ( ctx  context . Context ,  config  * apimodels . PostableUserConfig )  error 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									SaveAndApplyDefaultConfig ( ctx  context . Context )  error 
							 
						 
					
						
							
								
									
										
										
										
											2024-05-03 13:59:02 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									GetStatus ( context . Context )  ( apimodels . GettableStatus ,  error ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-06 07:59:29 -03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									// Silences 
							 
						 
					
						
							
								
									
										
										
										
											2023-10-02 07:36:11 -03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									CreateSilence ( context . Context ,  * apimodels . PostableSilence )  ( string ,  error ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									DeleteSilence ( context . Context ,  string )  error 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									GetSilence ( context . Context ,  string )  ( apimodels . GettableSilence ,  error ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									ListSilences ( context . Context ,  [ ] string )  ( apimodels . GettableSilences ,  error ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-06 07:59:29 -03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-04-09 13:39:34 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									// SilenceState returns the current state of silences in the Alertmanager. This is used to persist the state 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									// to the kvstore. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									SilenceState ( context . Context )  ( alertingNotify . SilenceState ,  error ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-09-06 07:59:29 -03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									// Alerts 
							 
						 
					
						
							
								
									
										
										
										
											2023-10-19 11:27:37 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									GetAlerts ( ctx  context . Context ,  active ,  silenced ,  inhibited  bool ,  filter  [ ] string ,  receiver  string )  ( apimodels . GettableAlerts ,  error ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									GetAlertGroups ( ctx  context . Context ,  active ,  silenced ,  inhibited  bool ,  filter  [ ] string ,  receiver  string )  ( apimodels . AlertGroups ,  error ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									PutAlerts ( context . Context ,  apimodels . PostableAlerts )  error 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-06 07:59:29 -03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									// Receivers 
							 
						 
					
						
							
								
									
										
										
										
											2023-10-20 11:34:17 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									GetReceivers ( ctx  context . Context )  ( [ ] apimodels . Receiver ,  error ) 
							 
						 
					
						
							
								
									
										
										
										
											2024-08-12 15:37:15 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									TestReceivers ( ctx  context . Context ,  c  apimodels . TestReceiversConfigBodyParams )  ( * alertingNotify . TestReceiversResult ,  int ,  error ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-06 07:59:29 -03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									TestTemplate ( ctx  context . Context ,  c  apimodels . TestTemplatesConfigBodyParams )  ( * TestTemplatesResults ,  error ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-04-09 12:13:27 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									// Lifecycle 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-06 07:59:29 -03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									StopAndWait ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									Ready ( )  bool 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								type  MultiOrgAlertmanager  struct  { 
							 
						 
					
						
							
								
									
										
										
										
											2022-04-22 11:57:56 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									Crypto     Crypto 
							 
						 
					
						
							
								
									
										
										
										
											2023-07-25 10:04:27 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									ProvStore  provisioningStore 
							 
						 
					
						
							
								
									
										
										
										
											2022-04-14 13:06:21 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
									alertmanagersMtx  sync . RWMutex 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-06 07:59:29 -03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									alertmanagers     map [ int64 ] Alertmanager 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-02-15 09:45:10 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									settings        * setting . Cfg 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									featureManager  featuremgmt . FeatureToggles 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									logger          log . Logger 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									// clusterPeer represents the clustering peers of Alertmanagers between Grafana instances. 
							 
						 
					
						
							
								
									
										
										
										
											2023-02-03 11:36:49 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									peer          alertingNotify . ClusterPeer 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									settleCancel  context . CancelFunc 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-05-23 14:24:20 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									configStore  AlertingStore 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
									orgStore     store . OrgStore 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-09 17:25:22 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									kvStore      kvstore . KVStore 
							 
						 
					
						
							
								
									
										
										
										
											2023-12-21 15:26:31 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									factory      OrgAlertmanagerFactory 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-04-25 13:39:46 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									decryptFn  alertingNotify . GetDecryptedValueFn 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-07 16:33:50 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-09-14 12:55:01 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									metrics  * metrics . MultiOrgAlertmanager 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-26 16:42:40 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									ns       notifications . Service 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-12-21 15:26:31 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								type  OrgAlertmanagerFactory  func ( ctx  context . Context ,  orgID  int64 )  ( Alertmanager ,  error ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-10-20 13:08:13 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								type  Option  func ( * MultiOrgAlertmanager ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-12-21 15:26:31 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								func  WithAlertmanagerOverride ( f  func ( OrgAlertmanagerFactory )  OrgAlertmanagerFactory )  Option  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-10-20 13:08:13 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									return  func ( moa  * MultiOrgAlertmanager )  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-12-21 15:26:31 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										moa . factory  =  f ( moa . factory ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-10-20 13:08:13 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-02-15 09:45:10 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								func  NewMultiOrgAlertmanager ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									cfg  * setting . Cfg , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									configStore  AlertingStore , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									orgStore  store . OrgStore , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									kvStore  kvstore . KVStore , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									provStore  provisioningStore , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									decryptFn  alertingNotify . GetDecryptedValueFn , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									m  * metrics . MultiOrgAlertmanager , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									ns  notifications . Service , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									l  log . Logger , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									s  secrets . Service , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									featureManager  featuremgmt . FeatureToggles , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									opts  ... Option , 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-07 16:33:50 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								)  ( * MultiOrgAlertmanager ,  error )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									moa  :=  & MultiOrgAlertmanager { 
							 
						 
					
						
							
								
									
										
										
										
											2022-04-22 11:57:56 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										Crypto :     NewCrypto ( s ,  configStore ,  l ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										ProvStore :  provStore , 
							 
						 
					
						
							
								
									
										
										
										
											2022-04-14 13:06:21 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-02-15 09:45:10 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										logger :          l , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										settings :        cfg , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										featureManager :  featureManager , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										alertmanagers :   map [ int64 ] Alertmanager { } , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										configStore :     configStore , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										orgStore :        orgStore , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										kvStore :         kvStore , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										decryptFn :       decryptFn , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										metrics :         m , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										ns :              ns , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										peer :            & NilPeer { } , 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2023-10-20 13:08:13 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-10 13:51:11 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									if  cfg . UnifiedAlerting . SkipClustering  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										l . Info ( "Skipping setting up clustering for MOA" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									}  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										if  err  :=  moa . setupClustering ( cfg ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											return  nil ,  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-19 17:05:26 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2023-10-20 13:08:13 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									// Set up the default per tenant Alertmanager factory. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									moa . factory  =  func ( ctx  context . Context ,  orgID  int64 )  ( Alertmanager ,  error )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										m  :=  metrics . NewAlertmanagerMetrics ( moa . metrics . GetOrCreateOrgRegistry ( orgID ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2024-03-22 18:37:33 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										stateStore  :=  NewFileStore ( orgID ,  kvStore ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										return  NewAlertmanager ( ctx ,  orgID ,  moa . settings ,  moa . configStore ,  stateStore ,  moa . peer ,  moa . decryptFn ,  moa . ns ,  m ,  featureManager . IsEnabled ( ctx ,  featuremgmt . FlagAlertingSimplifiedRouting ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-10-20 13:08:13 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									for  _ ,  opt  :=  range  opts  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										opt ( moa ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-04-19 17:05:26 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									return  moa ,  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-04-19 17:05:26 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								func  ( moa  * MultiOrgAlertmanager )  setupClustering ( cfg  * setting . Cfg )  error  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									clusterLogger  :=  moa . logger . New ( "component" ,  "clustering" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									// We set the settlement timeout to be a multiple of the gossip interval, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									// ensuring that a sufficient number of broadcasts have occurred, thereby 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									// increasing the probability of success when waiting for the cluster to settle. 
							 
						 
					
						
							
								
									
										
										
										
											2023-12-21 12:34:48 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									const  settleTimeout  =  alertingCluster . DefaultGossipInterval  *  10 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-19 17:05:26 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									// Redis setup. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  cfg . UnifiedAlerting . HARedisAddr  !=  ""  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										redisPeer ,  err  :=  newRedisPeer ( redisConfig { 
							 
						 
					
						
							
								
									
										
										
										
											2024-05-14 14:21:42 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											addr :        cfg . UnifiedAlerting . HARedisAddr , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											name :        cfg . UnifiedAlerting . HARedisPeerName , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											prefix :      cfg . UnifiedAlerting . HARedisPrefix , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											password :    cfg . UnifiedAlerting . HARedisPassword , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											username :    cfg . UnifiedAlerting . HARedisUsername , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											db :          cfg . UnifiedAlerting . HARedisDB , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											maxConns :    cfg . UnifiedAlerting . HARedisMaxConns , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											tlsEnabled :  cfg . UnifiedAlerting . HARedisTLSEnabled , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											tls :         cfg . UnifiedAlerting . HARedisTLSConfig , 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-19 17:05:26 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										} ,  clusterLogger ,  moa . metrics . Registerer ,  cfg . UnifiedAlerting . HAPushPullInterval ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											return  fmt . Errorf ( "unable to initialize redis: %w" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										var  ctx  context . Context 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										ctx ,  moa . settleCancel  =  context . WithTimeout ( context . Background ( ) ,  30 * time . Second ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										go  redisPeer . Settle ( ctx ,  settleTimeout ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										moa . peer  =  redisPeer 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									// Memberlist setup. 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-20 08:12:21 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									if  len ( cfg . UnifiedAlerting . HAPeers )  >  0  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-12-21 12:34:48 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										peer ,  err  :=  alertingCluster . Create ( 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											clusterLogger , 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-19 17:05:26 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											moa . metrics . Registerer , 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-20 08:12:21 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											cfg . UnifiedAlerting . HAListenAddr , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											cfg . UnifiedAlerting . HAAdvertiseAddr , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											cfg . UnifiedAlerting . HAPeers ,  // peers 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											true , 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-20 08:12:21 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											cfg . UnifiedAlerting . HAPushPullInterval , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											cfg . UnifiedAlerting . HAGossipInterval , 
							 
						 
					
						
							
								
									
										
										
										
											2023-12-21 12:34:48 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											alertingCluster . DefaultTCPTimeout , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											alertingCluster . DefaultProbeTimeout , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											alertingCluster . DefaultProbeInterval , 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											nil , 
							 
						 
					
						
							
								
									
										
										
										
											2021-11-19 16:50:55 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											true , 
							 
						 
					
						
							
								
									
										
										
										
											2023-05-09 10:32:23 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											cfg . UnifiedAlerting . HALabel , 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-19 17:05:26 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											return  fmt . Errorf ( "unable to initialize gossip mesh: %w" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-11 19:25:48 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										err  =  peer . Join ( alertingCluster . DefaultReconnectInterval ,  cfg . UnifiedAlerting . HAReconnectTimeout ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-04 18:46:34 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											moa . logger . Error ( "Msg" ,  "Unable to join gossip mesh while initializing cluster for high availability mode" ,  "error" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										// Attempt to verify the number of peers for 30s every 2s. The risk here is what we send a notification "too soon". 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										// Which should _never_ happen given we share the notification log via the database so the risk of double notification is very low. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										var  ctx  context . Context 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										ctx ,  moa . settleCancel  =  context . WithTimeout ( context . Background ( ) ,  30 * time . Second ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-19 17:05:26 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										go  peer . Settle ( ctx ,  settleTimeout ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										moa . peer  =  peer 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-19 17:05:26 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										return  nil 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-19 17:05:26 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									return  nil 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								func  ( moa  * MultiOrgAlertmanager )  Run ( ctx  context . Context )  error  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-05-25 08:55:01 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									moa . logger . Info ( "Starting MultiOrg Alertmanager" ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									for  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										select  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										case  <- ctx . Done ( ) : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											moa . StopAndWait ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											return  nil 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-20 08:12:21 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										case  <- time . After ( moa . settings . UnifiedAlerting . AlertmanagerConfigPollInterval ) : 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
											if  err  :=  moa . LoadAndSyncAlertmanagersForOrgs ( ctx ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-04 18:46:34 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
												moa . logger . Error ( "Error while synchronizing Alertmanager orgs" ,  "error" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								func  ( moa  * MultiOrgAlertmanager )  LoadAndSyncAlertmanagersForOrgs ( ctx  context . Context )  error  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-04 18:46:34 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									moa . logger . Debug ( "Synchronizing Alertmanagers for orgs" ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
									// First, load all the organizations from the database. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									orgIDs ,  err  :=  moa . orgStore . GetOrgs ( ctx ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										return  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									// Then, sync them by creating or deleting Alertmanagers as necessary. 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-14 12:55:01 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									moa . metrics . DiscoveredConfigurations . Set ( float64 ( len ( orgIDs ) ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-21 11:01:23 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									moa . SyncAlertmanagersForOrgs ( ctx ,  orgIDs ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-09-04 18:46:34 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									moa . logger . Debug ( "Done synchronizing Alertmanagers for orgs" ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-09-21 11:01:23 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								// getLatestConfigs retrieves the latest Alertmanager configuration for every organization. It returns a map where the key is the ID of each organization and the value is the configuration. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								func  ( moa  * MultiOrgAlertmanager )  getLatestConfigs ( ctx  context . Context )  ( map [ int64 ] * models . AlertConfiguration ,  error )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									configs ,  err  :=  moa . configStore . GetAllLatestAlertmanagerConfiguration ( ctx ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										return  nil ,  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									result  :=  make ( map [ int64 ] * models . AlertConfiguration ,  len ( configs ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									for  _ ,  config  :=  range  configs  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										result [ config . OrgID ]  =  config 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									return  result ,  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// SyncAlertmanagersForOrgs syncs configuration of the Alertmanager required by each organization. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								func  ( moa  * MultiOrgAlertmanager )  SyncAlertmanagersForOrgs ( ctx  context . Context ,  orgIDs  [ ] int64 )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
									orgsFound  :=  make ( map [ int64 ] struct { } ,  len ( orgIDs ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-21 11:01:23 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									dbConfigs ,  err  :=  moa . getLatestConfigs ( ctx ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-04 18:46:34 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										moa . logger . Error ( "Failed to load Alertmanager configurations" ,  "error" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-21 11:01:23 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										return 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
									moa . alertmanagersMtx . Lock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									for  _ ,  orgID  :=  range  orgIDs  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-29 17:16:40 +03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										if  _ ,  isDisabledOrg  :=  moa . settings . UnifiedAlerting . DisabledOrgs [ orgID ] ;  isDisabledOrg  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-04 18:46:34 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											moa . logger . Debug ( "Skipping syncing Alertmanager for disabled org" ,  "org" ,  orgID ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-29 17:16:40 +03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											continue 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
										orgsFound [ orgID ]  =  struct { } { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-09-21 11:01:23 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										alertmanager ,  found  :=  moa . alertmanagers [ orgID ] 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-29 17:16:40 +03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
										if  ! found  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-14 12:55:01 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											// These metrics are not exported by Grafana and are mostly a placeholder. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											// To export them, we need to translate the metrics from each individual registry and, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											// then aggregate them on the main registry. 
							 
						 
					
						
							
								
									
										
										
										
											2023-10-20 13:08:13 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											am ,  err  :=  moa . factory ( ctx ,  orgID ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
											if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-04 18:46:34 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
												moa . logger . Error ( "Unable to create Alertmanager for org" ,  "org" ,  orgID ,  "error" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-12-18 15:33:07 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
												continue 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
											} 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-12 18:10:08 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											moa . alertmanagers [ orgID ]  =  am 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-21 11:01:23 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											alertmanager  =  am 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-09-21 11:01:23 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										dbConfig ,  cfgFound  :=  dbConfigs [ orgID ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										if  ! cfgFound  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											if  found  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
												// This means that the configuration is gone but the organization, as well as the Alertmanager, exists. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
												moa . logger . Warn ( "Alertmanager exists for org but the configuration is gone. Applying the default configuration" ,  "org" ,  orgID ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											} 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-09 09:22:09 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											err  :=  alertmanager . SaveAndApplyDefaultConfig ( ctx ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-21 11:01:23 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-04 18:46:34 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
												moa . logger . Error ( "Failed to apply the default Alertmanager configuration" ,  "org" ,  orgID ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-21 11:01:23 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
												continue 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											moa . alertmanagers [ orgID ]  =  alertmanager 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											continue 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-02-02 14:45:17 -03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										err  :=  alertmanager . ApplyConfig ( ctx ,  dbConfig ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-21 11:01:23 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-04 18:46:34 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											moa . logger . Error ( "Failed to apply Alertmanager config for org" ,  "org" ,  orgID ,  "id" ,  dbConfig . ID ,  "error" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-21 11:01:23 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											continue 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-21 11:01:23 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										moa . alertmanagers [ orgID ]  =  alertmanager 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-09-06 07:59:29 -03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									amsToStop  :=  map [ int64 ] Alertmanager { } 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
									for  orgId ,  am  :=  range  moa . alertmanagers  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										if  _ ,  exists  :=  orgsFound [ orgId ] ;  ! exists  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											amsToStop [ orgId ]  =  am 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											delete ( moa . alertmanagers ,  orgId ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-14 12:55:01 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											moa . metrics . RemoveOrgRegistry ( orgId ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-14 12:55:01 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									moa . metrics . ActiveConfigurations . Set ( float64 ( len ( moa . alertmanagers ) ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
									moa . alertmanagersMtx . Unlock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									// Now, we can stop the Alertmanagers without having to hold a lock. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									for  orgID ,  am  :=  range  amsToStop  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-04 18:46:34 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										moa . logger . Info ( "Stopping Alertmanager" ,  "org" ,  orgID ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
										am . StopAndWait ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-04 18:46:34 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										moa . logger . Info ( "Stopped Alertmanager" ,  "org" ,  orgID ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-12 12:05:02 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 12:04:00 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									moa . cleanupOrphanLocalOrgState ( ctx ,  orgsFound ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-12 12:05:02 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-03-22 18:37:33 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								// cleanupOrphanLocalOrgState will remove all orphaned nflog and silence states in kvstore by existing to currently 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// active organizations. The original intention for this was the cleanup deleted orgs, that have had their states 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// saved to the kvstore after deletion on instance shutdown. 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 12:04:00 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								func  ( moa  * MultiOrgAlertmanager )  cleanupOrphanLocalOrgState ( ctx  context . Context , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									activeOrganizations  map [ int64 ] struct { } )  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-11-29 12:49:39 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									storedFiles  :=  [ ] string { NotificationLogFilename ,  SilencesFilename } 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 12:04:00 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									for  _ ,  fileName  :=  range  storedFiles  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										keys ,  err  :=  moa . kvStore . Keys ( ctx ,  kvstore . AllOrganizations ,  KVNamespace ,  fileName ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-04 18:46:34 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											moa . logger . Error ( "Failed to fetch items from kvstore" ,  "error" ,  err , 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 12:04:00 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
												"namespace" ,  KVNamespace ,  "key" ,  fileName ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										for  _ ,  key  :=  range  keys  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											if  _ ,  exists  :=  activeOrganizations [ key . OrgId ] ;  exists  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
												continue 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											err  =  moa . kvStore . Del ( ctx ,  key . OrgId ,  key . Namespace ,  key . Key ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-04 18:46:34 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
												moa . logger . Error ( "Failed to delete item from kvstore" ,  "error" ,  err , 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-14 12:04:00 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
													"orgID" ,  key . OrgId ,  "namespace" ,  KVNamespace ,  "key" ,  key . Key ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								func  ( moa  * MultiOrgAlertmanager )  StopAndWait ( )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									moa . alertmanagersMtx . Lock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									defer  moa . alertmanagersMtx . Unlock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									for  _ ,  am  :=  range  moa . alertmanagers  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										am . StopAndWait ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-12-21 12:34:48 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									p ,  ok  :=  moa . peer . ( * alertingCluster . Peer ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									if  ok  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										moa . settleCancel ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										if  err  :=  p . Leave ( 10  *  time . Second ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-04 18:46:34 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
											moa . logger . Warn ( "Unable to leave the gossip mesh" ,  "error" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-19 17:05:26 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									r ,  ok  :=  moa . peer . ( * redisPeer ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  ok  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										moa . settleCancel ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										r . Shutdown ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// AlertmanagerFor returns the Alertmanager instance for the organization provided. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// When the organization does not have an active Alertmanager, it returns a ErrNoAlertmanagerForOrg. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// When the Alertmanager of the organization is not ready, it returns a ErrAlertmanagerNotReady. 
							 
						 
					
						
							
								
									
										
										
										
											2023-09-06 07:59:29 -03:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								func  ( moa  * MultiOrgAlertmanager )  AlertmanagerFor ( orgID  int64 )  ( Alertmanager ,  error )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
									moa . alertmanagersMtx . RLock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									defer  moa . alertmanagersMtx . RUnlock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									orgAM ,  existing  :=  moa . alertmanagers [ orgID ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  ! existing  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										return  nil ,  ErrNoAlertmanagerForOrg 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  ! orgAM . Ready ( )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-12-27 17:01:17 -06:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										return  orgAM ,  ErrAlertmanagerNotReady 
							 
						 
					
						
							
								
									
										
										
										
											2021-08-24 03:28:09 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									return  orgAM ,  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-05-03 15:32:30 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								// alertmanagerForOrg returns the Alertmanager instance for the organization provided. Should only be called when the 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// caller has already locked the alertmanagersMtx. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// TODO: This should eventually replace AlertmanagerFor once the API layer has been refactored to not access the alertmanagers directly 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// and AM route error handling has been fully moved to errorutil. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								func  ( moa  * MultiOrgAlertmanager )  alertmanagerForOrg ( orgID  int64 )  ( Alertmanager ,  error )  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-09 13:39:34 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									orgAM ,  existing  :=  moa . alertmanagers [ orgID ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  ! existing  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-05-03 15:32:30 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										return  nil ,  WithPublicError ( ErrAlertmanagerNotFound . Errorf ( "Alertmanager does not exist for org %d" ,  orgID ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-09 13:39:34 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  ! orgAM . Ready ( )  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-05-03 15:32:30 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										return  nil ,  WithPublicError ( ErrAlertmanagerConflict . Errorf ( "Alertmanager is not ready for org %d" ,  orgID ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-09 13:39:34 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-05-03 15:32:30 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									return  orgAM ,  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// ListSilences lists silences for the organization provided. Currently, this is a pass-through to the Alertmanager 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// implementation. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								func  ( moa  * MultiOrgAlertmanager )  ListSilences ( ctx  context . Context ,  orgID  int64 ,  filter  [ ] string )  ( [ ] * models . Silence ,  error )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									moa . alertmanagersMtx . RLock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									defer  moa . alertmanagersMtx . RUnlock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									orgAM ,  err  :=  moa . alertmanagerForOrg ( orgID ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										return  nil ,  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									silences ,  err  :=  orgAM . ListSilences ( ctx ,  filter ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										if  errors . Is ( err ,  alertingNotify . ErrListSilencesBadPayload )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											return  nil ,  WithPublicError ( ErrSilencesBadRequest . Errorf ( "invalid filters: %w" ,  err ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										return  nil ,  WithPublicError ( ErrSilenceInternal . Errorf ( "failed to list silences: %w" ,  err ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									return  GettableSilencesToSilences ( silences ) ,  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// GetSilence gets a silence for the organization and silence id provided. Currently, this is a pass-through to the 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// Alertmanager implementation. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								func  ( moa  * MultiOrgAlertmanager )  GetSilence ( ctx  context . Context ,  orgID  int64 ,  id  string )  ( * models . Silence ,  error )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									moa . alertmanagersMtx . RLock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									defer  moa . alertmanagersMtx . RUnlock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									orgAM ,  err  :=  moa . alertmanagerForOrg ( orgID ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										return  nil ,  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									s ,  err  :=  orgAM . GetSilence ( ctx ,  id ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										if  errors . Is ( err ,  alertingNotify . ErrSilenceNotFound )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											return  nil ,  WithPublicError ( ErrSilenceNotFound . Errorf ( "silence %s not found" ,  id ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										return  nil ,  WithPublicError ( ErrSilenceInternal . Errorf ( "failed to get silence: %w" ,  err ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									return  GettableSilenceToSilence ( s ) ,  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// CreateSilence creates a silence in the Alertmanager for the organization provided, returning the silence ID. It will 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// also persist the silence state to the kvstore immediately after creating the silence. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								func  ( moa  * MultiOrgAlertmanager )  CreateSilence ( ctx  context . Context ,  orgID  int64 ,  ps  models . Silence )  ( string ,  error )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									moa . alertmanagersMtx . RLock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									defer  moa . alertmanagersMtx . RUnlock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									orgAM ,  err  :=  moa . alertmanagerForOrg ( orgID ) 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-09 13:39:34 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										return  "" ,  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-05-03 15:32:30 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									// Need to create the silence in the AM first to get the silence ID. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									silenceID ,  err  :=  orgAM . CreateSilence ( ctx ,  SilenceToPostableSilence ( ps ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										if  errors . Is ( err ,  alertingNotify . ErrSilenceNotFound )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											return  "" ,  WithPublicError ( ErrSilenceNotFound . Errorf ( "silence %v not found" ,  ps . ID ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										if  errors . Is ( err ,  alertingNotify . ErrCreateSilenceBadPayload )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											return  "" ,  WithPublicError ( ErrSilencesBadRequest . Errorf ( "invalid silence: %w" ,  err ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										return  "" ,  WithPublicError ( ErrSilenceInternal . Errorf ( "failed to upsert silence: %w" ,  err ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-04-09 13:39:34 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									err  =  moa . updateSilenceState ( ctx ,  orgAM ,  orgID ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										moa . logger . Warn ( "Failed to persist silence state on create, will be corrected by next maintenance run" ,  "orgID" ,  orgID ,  "silenceID" ,  silenceID ,  "error" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									return  silenceID ,  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-05-03 15:32:30 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								// UpdateSilence updates a silence in the Alertmanager for the organization provided, returning the silence ID. It will 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// also persist the silence state to the kvstore immediately after creating the silence. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// Currently, this just calls CreateSilence as the underlying Alertmanager implementation upserts. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								func  ( moa  * MultiOrgAlertmanager )  UpdateSilence ( ctx  context . Context ,  orgID  int64 ,  ps  models . Silence )  ( string ,  error )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  ps . ID  ==  nil  ||  * ps . ID  ==  ""  {  // TODO: Alertmanager interface should probably include a method for updating silences. For now, we leak this implementation detail. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										return  "" ,  WithPublicError ( ErrSilencesBadRequest . Errorf ( "silence ID is required" ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									return  moa . CreateSilence ( ctx ,  orgID ,  ps ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-04-09 13:39:34 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								// DeleteSilence deletes a silence in the Alertmanager for the organization provided. It will also persist the silence 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// state to the kvstore immediately after deleting the silence. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								func  ( moa  * MultiOrgAlertmanager )  DeleteSilence ( ctx  context . Context ,  orgID  int64 ,  silenceID  string )  error  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									moa . alertmanagersMtx . RLock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									defer  moa . alertmanagersMtx . RUnlock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-05-03 15:32:30 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									orgAM ,  err  :=  moa . alertmanagerForOrg ( orgID ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										return  err 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-09 13:39:34 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-05-03 15:32:30 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									err  =  orgAM . DeleteSilence ( ctx ,  silenceID ) 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-09 13:39:34 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-05-03 15:32:30 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
										if  errors . Is ( err ,  alertingNotify . ErrSilenceNotFound )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
											return  WithPublicError ( ErrSilenceNotFound . Errorf ( "silence %s not found" ,  silenceID ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										return  WithPublicError ( ErrSilenceInternal . Errorf ( "failed to delete silence %s: %w" ,  silenceID ,  err ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-09 13:39:34 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									err  =  moa . updateSilenceState ( ctx ,  orgAM ,  orgID ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										moa . logger . Warn ( "Failed to persist silence state on delete, will be corrected by next maintenance run" ,  "orgID" ,  orgID ,  "silenceID" ,  silenceID ,  "error" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// updateSilenceState persists the silence state to the kvstore immediately instead of waiting for the next maintenance 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// run. This is used after Create/Delete to prevent silences from being lost when a new Alertmanager is started before 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// the state has persisted. This can happen, for example, in a rolling deployment scenario. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								func  ( moa  * MultiOrgAlertmanager )  updateSilenceState ( ctx  context . Context ,  orgAM  Alertmanager ,  orgID  int64 )  error  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									// Collect the internal silence state from the AM. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									// TODO: Currently, we rely on the AM itself for the persisted silence state representation. Preferably, we would 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									//  define the state ourselves and persist it in a format that is easy to guarantee consistency for writes to 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									//  individual silences. In addition to the consistency benefits, this would also allow us to avoid the need for 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									//  a network request to the AM to get the state in the case of remote alertmanagers. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									silences ,  err  :=  orgAM . SilenceState ( ctx ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										return  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									// Persist to kvstore. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									fs  :=  NewFileStore ( orgID ,  moa . kvStore ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									_ ,  err  =  fs . SaveSilences ( ctx ,  silences ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
									return  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								// NilPeer and NilChannel implements the Alertmanager clustering interface. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								type  NilPeer  struct { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								func  ( p  * NilPeer )  Position ( )  int                    {  return  0  } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								func  ( p  * NilPeer )  WaitReady ( context . Context )  error  {  return  nil  } 
							 
						 
					
						
							
								
									
										
										
										
											2023-12-21 12:34:48 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								func  ( p  * NilPeer )  AddState ( string ,  alertingCluster . State ,  prometheus . Registerer )  alertingCluster . ClusterChannel  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-16 15:33:51 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
									return  & NilChannel { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								type  NilChannel  struct { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								func  ( c  * NilChannel )  Broadcast ( [ ] byte )  { }