Alerting: Use a default configuration and periodically poll for new ones (#32851)

* Alerting: Use a default configuration and periodically poll for new ones

Use a default configuration to make sure we always start the grafana
instance. Then, regularly poll for new ones.

I've also made sure that failures to apply configuration do not stop the
Grafana server but instead keep polling until it is a success.
This commit is contained in:
gotjosh
2021-04-13 13:02:44 +01:00
committed by GitHub
parent 4178ebc0a1
commit 528ca9134b
9 changed files with 137 additions and 166 deletions
+4 -20
View File
@@ -2,11 +2,8 @@ package api
import (
"errors"
"fmt"
"net/http"
"gopkg.in/yaml.v3"
apimodels "github.com/grafana/alerting-api/pkg/api"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/infra/log"
@@ -64,8 +61,8 @@ func (srv AlertmanagerSrv) RouteGetAlertingConfig(c *models.ReqContext) response
return response.Error(http.StatusInternalServerError, "failed to get latest configuration", err)
}
cfg := apimodels.PostableUserConfig{}
if err := yaml.Unmarshal([]byte(query.Result.AlertmanagerConfiguration), &cfg); err != nil {
cfg, err := notifier.Load([]byte(query.Result.AlertmanagerConfiguration))
if err != nil {
return response.Error(http.StatusInternalServerError, "failed to unmarshal alertmanager configuration", err)
}
@@ -178,21 +175,8 @@ func (srv AlertmanagerSrv) RouteGetSilences(c *models.ReqContext) response.Respo
}
func (srv AlertmanagerSrv) RoutePostAlertingConfig(c *models.ReqContext, body apimodels.PostableUserConfig) response.Response {
config, err := yaml.Marshal(&body)
if err != nil {
return response.Error(http.StatusInternalServerError, "failed to serialize to the Alertmanager configuration", err)
}
cmd := ngmodels.SaveAlertmanagerConfigurationCmd{
AlertmanagerConfiguration: string(config),
ConfigurationVersion: fmt.Sprintf("v%d", ngmodels.AlertConfigurationVersion),
}
if err := srv.store.SaveAlertmanagerConfiguration(&cmd); err != nil {
return response.Error(http.StatusInternalServerError, "failed to save Alertmanager configuration", err)
}
if err := srv.am.ApplyConfig(&body); err != nil {
return response.Error(http.StatusInternalServerError, "failed to apply Alertmanager configuration", err)
if err := srv.am.SaveAndApplyConfig(&body); err != nil {
return response.Error(http.StatusInternalServerError, "failed to save and apply Alertmanager configuration", err)
}
return response.JSON(http.StatusAccepted, util.DynMap{"message": "configuration created"})