Alerting: API to create extra Alertmanager configurations (#106904)

What is this feature?

Implements the POST endpoint for deleting imported Mimir Alertmanager configurations:

    POST /api/convert/api/v1/alerts

The API endpoint creates the extra Alertmanager configuration with the provided identifier and matchers.
This commit is contained in:
Alexander Akhmetov
2025-06-18 15:05:06 +02:00
committed by GitHub
parent 3ab54a5562
commit 426c334973
6 changed files with 625 additions and 4 deletions
@@ -128,6 +128,7 @@ type ConvertPrometheusSrv struct {
type Alertmanager interface {
DeleteExtraConfiguration(ctx context.Context, org int64, identifier string) error
SaveAndApplyExtraConfiguration(ctx context.Context, org int64, extraConfig apimodels.ExtraConfiguration) error
GetAlertmanagerConfiguration(ctx context.Context, org int64, withAutogen bool) (apimodels.GettableUserConfig, error)
}
@@ -532,7 +533,44 @@ func (srv *ConvertPrometheusSrv) convertToGrafanaRuleGroup(
}
func (srv *ConvertPrometheusSrv) RouteConvertPrometheusPostAlertmanagerConfig(c *contextmodel.ReqContext, amCfg apimodels.AlertmanagerUserConfig) response.Response {
return response.Error(http.StatusNotImplemented, "Not Implemented", nil)
if !srv.featureToggles.IsEnabledGlobally(featuremgmt.FlagAlertingImportAlertmanagerAPI) {
return response.Error(http.StatusNotImplemented, "Not Implemented", nil)
}
logger := srv.logger.FromContext(c.Req.Context())
identifier, err := parseConfigIdentifierHeader(c)
if err != nil {
logger.Error("Failed to parse config identifier header", "error", err, "identifier", identifier)
return errorToResponse(err)
}
mergeMatchers, err := parseMergeMatchersHeader(c)
if err != nil {
logger.Error("Failed to parse merge matchers header", "error", err, "identifier", identifier)
return errorToResponse(err)
}
ec := apimodels.ExtraConfiguration{
Identifier: identifier,
MergeMatchers: mergeMatchers,
TemplateFiles: amCfg.TemplateFiles,
AlertmanagerConfig: amCfg.AlertmanagerConfig,
}
err = ec.Validate()
if err != nil {
logger.Error("Invalid alertmanager configuration", "error", err, "identifier", identifier)
return errorToResponse(err)
}
err = srv.am.SaveAndApplyExtraConfiguration(c.Req.Context(), c.GetOrgID(), ec)
if err != nil {
logger.Error("Failed to save alertmanager configuration", "error", err, "identifier", identifier)
return errorToResponse(fmt.Errorf("failed to save alertmanager configuration: %w", err))
}
logger.Info("Successfully updated alertmanager configuration with imported Prometheus config", "identifier", identifier)
return successfulResponse()
}
func (srv *ConvertPrometheusSrv) RouteConvertPrometheusGetAlertmanagerConfig(c *contextmodel.ReqContext) response.Response {