Alerting: Send static headers to the remote Alertmanager (#89846)

This commit is contained in:
Santiago 2024-07-01 17:48:40 +02:00 committed by GitHub
parent 579250a89a
commit fce03cd724
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 17 additions and 1 deletions

View File

@ -204,6 +204,7 @@ func (ng *AlertNG) init() error {
PromoteConfig: true,
SyncInterval: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.SyncInterval,
ExternalURL: ng.Cfg.AppURL,
StaticHeaders: ng.Cfg.Smtp.StaticHeaders,
}
remoteAM, err := createRemoteAlertmanager(cfg, ng.KVStore, ng.SecretsService.Decrypt, autogenFn, m, ng.tracer)
if err != nil {
@ -239,6 +240,7 @@ func (ng *AlertNG) init() error {
TenantID: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.TenantID,
URL: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.URL,
ExternalURL: ng.Cfg.AppURL,
StaticHeaders: ng.Cfg.Smtp.StaticHeaders,
}
remoteAM, err := createRemoteAlertmanager(cfg, ng.KVStore, ng.SecretsService.Decrypt, autogenFn, m, ng.tracer)
if err != nil {
@ -276,6 +278,7 @@ func (ng *AlertNG) init() error {
URL: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.URL,
SyncInterval: ng.Cfg.UnifiedAlerting.RemoteAlertmanager.SyncInterval,
ExternalURL: ng.Cfg.AppURL,
StaticHeaders: ng.Cfg.Smtp.StaticHeaders,
}
remoteAM, err := createRemoteAlertmanager(cfg, ng.KVStore, ng.SecretsService.Decrypt, autogenFn, m, ng.tracer)
if err != nil {

View File

@ -80,9 +80,14 @@ type AlertmanagerConfig struct {
// 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.
// The same flag is used for promoting state.
PromoteConfig bool
// StaticHeaders are used in email notifications sent by the remote Alertmanager.
StaticHeaders map[string]string
// SyncInterval determines how often we should attempt to synchronize configuration.
SyncInterval time.Duration
}
@ -120,6 +125,7 @@ func NewAlertmanager(cfg AlertmanagerConfig, store stateStore, decryptFn Decrypt
URL: u,
PromoteConfig: cfg.PromoteConfig,
ExternalURL: cfg.ExternalURL,
StaticHeaders: cfg.StaticHeaders,
}
mc, err := remoteClient.New(mcCfg, metrics, tracer)
if err != nil {

View File

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

View File

@ -22,6 +22,7 @@ type UserGrafanaConfig struct {
Default bool `json:"default"`
Promoted bool `json:"promoted"`
ExternalURL string `json:"external_url"`
StaticHeaders map[string]string `json:"static_headers"`
}
func (mc *Mimir) ShouldPromoteConfig() bool {
@ -55,6 +56,7 @@ func (mc *Mimir) CreateGrafanaAlertmanagerConfig(ctx context.Context, cfg *apimo
Default: isDefault,
Promoted: mc.promoteConfig,
ExternalURL: mc.externalURL,
StaticHeaders: mc.staticHeaders,
})
if err != nil {
return err

View File

@ -41,6 +41,7 @@ type Mimir struct {
metrics *metrics.RemoteAlertmanager
promoteConfig bool
externalURL string
staticHeaders map[string]string
}
type Config struct {
@ -51,6 +52,7 @@ type Config struct {
Logger log.Logger
PromoteConfig bool
ExternalURL string
StaticHeaders map[string]string
}
// successResponse represents a successful response from the Mimir API.
@ -94,6 +96,7 @@ func New(cfg *Config, metrics *metrics.RemoteAlertmanager, tracer tracing.Tracer
metrics: metrics,
promoteConfig: cfg.PromoteConfig,
externalURL: cfg.ExternalURL,
staticHeaders: cfg.StaticHeaders,
}, nil
}