Alerting: use alerting GrafanaReceiver and BuildReceiverConfiguration in Grafana (#65224)
* replace receiver errors with one from alerting * add the converter to alerting models * update buildReceiverIntegration to accept GrafanaReceiver --------- Co-authored-by: George Robinson <george.robinson@grafana.com>
This commit is contained in:
co-authored by
George Robinson
parent
e5e0a1cbbf
commit
afd52d0866
@@ -121,7 +121,7 @@ func (m *migration) setupAlertmanagerConfigs(rulesPerOrg map[int64]map[*alertRul
|
||||
|
||||
// Validate the alertmanager configuration produced, this gives a chance to catch bad configuration at migration time.
|
||||
// Validation between legacy and unified alerting can be different (e.g. due to bug fixes) so this would fail the migration in that case.
|
||||
if err := m.validateAlertmanagerConfig(orgID, amConfig); err != nil {
|
||||
if err := m.validateAlertmanagerConfig(amConfig); err != nil {
|
||||
return nil, fmt.Errorf("failed to validate AlertmanagerConfig in orgId %d: %w", orgID, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package ualert
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -12,9 +11,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
alertingLogging "github.com/grafana/alerting/logging"
|
||||
alertingNotify "github.com/grafana/alerting/notify"
|
||||
"github.com/grafana/alerting/receivers"
|
||||
pb "github.com/prometheus/alertmanager/silence/silencepb"
|
||||
"xorm.io/xorm"
|
||||
|
||||
@@ -475,32 +472,21 @@ func (m *migration) writeAlertmanagerConfig(orgID int64, amConfig *PostableUserC
|
||||
}
|
||||
|
||||
// validateAlertmanagerConfig validates the alertmanager configuration produced by the migration against the receivers.
|
||||
func (m *migration) validateAlertmanagerConfig(orgID int64, config *PostableUserConfig) error {
|
||||
func (m *migration) validateAlertmanagerConfig(config *PostableUserConfig) error {
|
||||
for _, r := range config.AlertmanagerConfig.Receivers {
|
||||
for _, gr := range r.GrafanaManagedReceivers {
|
||||
// First, let's decode the secure settings - given they're stored as base64.
|
||||
secureSettings := make(map[string][]byte, len(gr.SecureSettings))
|
||||
for k, v := range gr.SecureSettings {
|
||||
d, err := base64.StdEncoding.DecodeString(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
secureSettings[k] = d
|
||||
}
|
||||
|
||||
data, err := gr.Settings.MarshalJSON()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var (
|
||||
cfg = &receivers.NotificationChannelConfig{
|
||||
cfg = &alertingNotify.GrafanaReceiver{
|
||||
UID: gr.UID,
|
||||
OrgID: orgID,
|
||||
Name: gr.Name,
|
||||
Type: gr.Type,
|
||||
DisableResolveMessage: gr.DisableResolveMessage,
|
||||
Settings: data,
|
||||
SecureSettings: secureSettings,
|
||||
SecureSettings: gr.SecureSettings,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -517,17 +503,9 @@ func (m *migration) validateAlertmanagerConfig(orgID int64, config *PostableUser
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
receiverFactory, exists := alertingNotify.Factory(gr.Type)
|
||||
if !exists {
|
||||
return fmt.Errorf("notifier %s is not supported", gr.Type)
|
||||
}
|
||||
factoryConfig, err := receivers.NewFactoryConfig(cfg, nil, decryptFunc, nil, nil, func(ctx ...interface{}) alertingLogging.Logger {
|
||||
return &alertingLogging.FakeLogger{}
|
||||
}, setting.BuildVersion)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = receiverFactory(factoryConfig)
|
||||
_, err = alertingNotify.BuildReceiverConfiguration(context.Background(), &alertingNotify.APIReceiver{
|
||||
GrafanaReceivers: alertingNotify.GrafanaReceivers{Receivers: []*alertingNotify.GrafanaReceiver{cfg}},
|
||||
}, decryptFunc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -65,14 +65,14 @@ func Test_validateAlertmanagerConfig(t *testing.T) {
|
||||
name: "when a slack receiver does not have a valid URL - it should error",
|
||||
receivers: []*PostableGrafanaReceiver{
|
||||
{
|
||||
UID: util.GenerateShortUID(),
|
||||
UID: "test-uid",
|
||||
Name: "SlackWithBadURL",
|
||||
Type: "slack",
|
||||
Settings: simplejson.NewFromAny(map[string]interface{}{}),
|
||||
SecureSettings: map[string]string{"url": invalidUri},
|
||||
},
|
||||
},
|
||||
err: fmt.Errorf("failed to validate receiver \"SlackWithBadURL\" of type \"slack\": invalid URL %q", invalidUri),
|
||||
err: fmt.Errorf("failed to validate receiver \"SlackWithBadURL\" of type \"slack\": failed to parse notifier SlackWithBadURL (UID: test-uid): invalid URL %q", invalidUri),
|
||||
},
|
||||
{
|
||||
name: "when a slack receiver has an invalid recipient - it should not error",
|
||||
@@ -103,11 +103,10 @@ func Test_validateAlertmanagerConfig(t *testing.T) {
|
||||
for _, tt := range tc {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
mg := newTestMigration(t)
|
||||
orgID := int64(1)
|
||||
|
||||
config := configFromReceivers(t, tt.receivers)
|
||||
require.NoError(t, config.EncryptSecureSettings()) // make sure we encrypt the settings
|
||||
err := mg.validateAlertmanagerConfig(orgID, config)
|
||||
err := mg.validateAlertmanagerConfig(config)
|
||||
if tt.err != nil {
|
||||
require.Error(t, err)
|
||||
require.EqualError(t, err, tt.err.Error())
|
||||
|
||||
Reference in New Issue
Block a user