Alerting: Send external URL to the remote Alertmanager (#89701)

* Alerting: Send external URL to the remote Alertmanager

* test that the URL is sent to the remote Alertmanager

* AppURL -> ExternalURL
This commit is contained in:
Santiago 2024-06-26 14:02:02 +02:00 committed by GitHub
parent 3db4e5a0c6
commit fe1309dd96
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 16 additions and 1 deletions

View File

@ -203,6 +203,7 @@ func (ng *AlertNG) init() error {
URL: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.URL, URL: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.URL,
PromoteConfig: true, PromoteConfig: true,
SyncInterval: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.SyncInterval, SyncInterval: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.SyncInterval,
ExternalURL: ng.Cfg.AppURL,
} }
remoteAM, err := createRemoteAlertmanager(cfg, ng.KVStore, ng.SecretsService.Decrypt, autogenFn, m, ng.tracer) remoteAM, err := createRemoteAlertmanager(cfg, ng.KVStore, ng.SecretsService.Decrypt, autogenFn, m, ng.tracer)
if err != nil { if err != nil {
@ -237,6 +238,7 @@ func (ng *AlertNG) init() error {
PromoteConfig: true, PromoteConfig: true,
TenantID: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.TenantID, TenantID: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.TenantID,
URL: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.URL, URL: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.URL,
ExternalURL: ng.Cfg.AppURL,
} }
remoteAM, err := createRemoteAlertmanager(cfg, ng.KVStore, ng.SecretsService.Decrypt, autogenFn, m, ng.tracer) remoteAM, err := createRemoteAlertmanager(cfg, ng.KVStore, ng.SecretsService.Decrypt, autogenFn, m, ng.tracer)
if err != nil { if err != nil {
@ -273,6 +275,7 @@ func (ng *AlertNG) init() error {
TenantID: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.TenantID, TenantID: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.TenantID,
URL: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.URL, URL: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.URL,
SyncInterval: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.SyncInterval, SyncInterval: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.SyncInterval,
ExternalURL: ng.Cfg.AppURL,
} }
remoteAM, err := createRemoteAlertmanager(cfg, ng.KVStore, ng.SecretsService.Decrypt, autogenFn, m, ng.tracer) remoteAM, err := createRemoteAlertmanager(cfg, ng.KVStore, ng.SecretsService.Decrypt, autogenFn, m, ng.tracer)
if err != nil { if err != nil {

View File

@ -77,10 +77,12 @@ type AlertmanagerConfig struct {
BasicAuthPassword string BasicAuthPassword string
DefaultConfig string DefaultConfig string
// ExternalURL is used in notifications sent by the remote Alertmanager.
ExternalURL string
// PromoteConfig is a flag that determines whether the configuration should be used in the remote Alertmanager. // PromoteConfig is a flag that determines whether the configuration should be used in the remote Alertmanager.
// The same flag is used for promoting state. // The same flag is used for promoting state.
PromoteConfig bool PromoteConfig bool
// SyncInterval determines how often we should attempt to synchronize configuration. // SyncInterval determines how often we should attempt to synchronize configuration.
SyncInterval time.Duration SyncInterval time.Duration
} }
@ -117,6 +119,7 @@ func NewAlertmanager(cfg AlertmanagerConfig, store stateStore, decryptFn Decrypt
TenantID: cfg.TenantID, TenantID: cfg.TenantID,
URL: u, URL: u,
PromoteConfig: cfg.PromoteConfig, PromoteConfig: cfg.PromoteConfig,
ExternalURL: cfg.ExternalURL,
} }
mc, err := remoteClient.New(mcCfg, metrics, tracer) mc, err := remoteClient.New(mcCfg, metrics, tracer)
if err != nil { if err != nil {

View File

@ -168,6 +168,7 @@ func TestApplyConfig(t *testing.T) {
DefaultConfig: defaultGrafanaConfig, DefaultConfig: defaultGrafanaConfig,
PromoteConfig: true, PromoteConfig: true,
SyncInterval: 1 * time.Hour, SyncInterval: 1 * time.Hour,
ExternalURL: "https://test.grafana.com",
} }
ctx := context.Background() ctx := context.Background()
@ -198,6 +199,9 @@ func TestApplyConfig(t *testing.T) {
require.JSONEq(t, testGrafanaConfigWithSecret, string(amCfg)) require.JSONEq(t, testGrafanaConfigWithSecret, string(amCfg))
require.True(t, configSent.Promoted) require.True(t, configSent.Promoted)
// Grafana's URL should be sent alongside the configuration.
require.Equal(t, cfg.ExternalURL, configSent.ExternalURL)
// If we already got a 200 status code response and the sync interval hasn't elapsed, // If we already got a 200 status code response and the sync interval hasn't elapsed,
// we shouldn't send the state/configuration again. // we shouldn't send the state/configuration again.
expStateSync := lastStateSync expStateSync := lastStateSync

View File

@ -21,6 +21,7 @@ type UserGrafanaConfig struct {
CreatedAt int64 `json:"created"` CreatedAt int64 `json:"created"`
Default bool `json:"default"` Default bool `json:"default"`
Promoted bool `json:"promoted"` Promoted bool `json:"promoted"`
ExternalURL string `json:"external_url"`
} }
func (mc *Mimir) ShouldPromoteConfig() bool { func (mc *Mimir) ShouldPromoteConfig() bool {
@ -53,6 +54,7 @@ func (mc *Mimir) CreateGrafanaAlertmanagerConfig(ctx context.Context, cfg *apimo
CreatedAt: createdAt, CreatedAt: createdAt,
Default: isDefault, Default: isDefault,
Promoted: mc.promoteConfig, Promoted: mc.promoteConfig,
ExternalURL: mc.externalURL,
}) })
if err != nil { if err != nil {
return err return err

View File

@ -40,6 +40,7 @@ type Mimir struct {
logger log.Logger logger log.Logger
metrics *metrics.RemoteAlertmanager metrics *metrics.RemoteAlertmanager
promoteConfig bool promoteConfig bool
externalURL string
} }
type Config struct { type Config struct {
@ -49,6 +50,7 @@ type Config struct {
Logger log.Logger Logger log.Logger
PromoteConfig bool PromoteConfig bool
ExternalURL string
} }
// successResponse represents a successful response from the Mimir API. // successResponse represents a successful response from the Mimir API.
@ -91,6 +93,7 @@ func New(cfg *Config, metrics *metrics.RemoteAlertmanager, tracer tracing.Tracer
logger: cfg.Logger, logger: cfg.Logger,
metrics: metrics, metrics: metrics,
promoteConfig: cfg.PromoteConfig, promoteConfig: cfg.PromoteConfig,
externalURL: cfg.ExternalURL,
}, nil }, nil
} }