@@ -36,6 +36,7 @@ import (
3636 "go.opentelemetry.io/otel"
3737 "go.opentelemetry.io/otel/codes"
3838
39+ "github.com/prometheus/alertmanager/alert"
3940 "github.com/prometheus/alertmanager/api/metrics"
4041 open_api_models "github.com/prometheus/alertmanager/api/v2/models"
4142 "github.com/prometheus/alertmanager/api/v2/restapi"
@@ -48,12 +49,12 @@ import (
4849 "github.com/prometheus/alertmanager/cluster"
4950 "github.com/prometheus/alertmanager/config"
5051 "github.com/prometheus/alertmanager/dispatch"
52+ "github.com/prometheus/alertmanager/marker"
5153 "github.com/prometheus/alertmanager/matcher/compat"
5254 "github.com/prometheus/alertmanager/pkg/labels"
5355 "github.com/prometheus/alertmanager/provider"
5456 "github.com/prometheus/alertmanager/silence"
5557 "github.com/prometheus/alertmanager/silence/silencepb"
56- "github.com/prometheus/alertmanager/types"
5758)
5859
5960var tracer = otel .Tracer ("github.com/prometheus/alertmanager/api/v2" )
@@ -64,7 +65,6 @@ type API struct {
6465 silences * silence.Silences
6566 alerts provider.Alerts
6667 alertGroups groupsFn
67- getAlertStatus getAlertStatusFn
6868 groupMutedFunc groupMutedFunc
6969 uptime time.Time
7070
@@ -83,17 +83,15 @@ type API struct {
8383}
8484
8585type (
86- groupsFn func (context.Context , func (* dispatch.Route ) bool , func (* types .Alert , time.Time ) bool ) (dispatch.AlertGroups , map [prometheus_model.Fingerprint ][]string , error )
86+ groupsFn func (context.Context , func (* dispatch.Route ) bool , func (* alert .Alert , time.Time ) bool ) (dispatch.AlertGroups , map [prometheus_model.Fingerprint ][]string , error )
8787 groupMutedFunc func (routeID , groupKey string ) ([]string , bool )
88- getAlertStatusFn func (prometheus_model.Fingerprint ) types.AlertStatus
8988 setAlertStatusFn func (ctx context.Context , labels prometheus_model.LabelSet )
9089)
9190
9291// NewAPI returns a new Alertmanager API v2.
9392func NewAPI (
9493 alerts provider.Alerts ,
9594 gf groupsFn ,
96- asf getAlertStatusFn ,
9795 gmf groupMutedFunc ,
9896 silences * silence.Silences ,
9997 peer cluster.ClusterPeer ,
@@ -102,7 +100,6 @@ func NewAPI(
102100) (* API , error ) {
103101 api := API {
104102 alerts : alerts ,
105- getAlertStatus : asf ,
106103 alertGroups : gf ,
107104 groupMutedFunc : gmf ,
108105 peer : peer ,
@@ -281,7 +278,8 @@ func (api *API) getAlertsHandler(params alert_ops.GetAlertsParams) middleware.Re
281278 alerts := api .alerts .GetPending ()
282279 defer alerts .Close ()
283280
284- alertFilter := api .alertFilter (matchers , * params .Silenced , * params .Inhibited , * params .Active )
281+ tempMarker := marker .NewAlertMarker ()
282+ alertFilter := api .alertFilter (matchers , * params .Silenced , * params .Inhibited , * params .Active , tempMarker )
285283 now := time .Now ()
286284
287285 api .mtx .RLock ()
@@ -308,7 +306,7 @@ func (api *API) getAlertsHandler(params alert_ops.GetAlertsParams) middleware.Re
308306 continue
309307 }
310308
311- openAlert := AlertToOpenAPIAlert (alert , api . getAlertStatus (alert .Fingerprint ()), receivers , nil )
309+ openAlert := AlertToOpenAPIAlert (alert , tempMarker . Status (alert .Fingerprint ()), receivers , nil )
312310
313311 res = append (res , openAlert )
314312 }
@@ -365,7 +363,7 @@ func (api *API) postAlertsHandler(params alert_ops.PostAlertsParams) middleware.
365363
366364 // Make a best effort to insert all alerts that are valid.
367365 var (
368- validAlerts = make ([]* types .Alert , 0 , len (alerts ))
366+ validAlerts = make ([]* alert .Alert , 0 , len (alerts ))
369367 validationErrs error
370368 )
371369 for _ , a := range alerts {
@@ -432,7 +430,7 @@ func (api *API) getAlertGroupsHandler(params alertgroup_ops.GetAlertGroupsParams
432430 }
433431 }(receiverFilter )
434432
435- af := api .alertFilter (matchers , * params .Silenced , * params .Inhibited , * params .Active )
433+ af := api .alertFilter (matchers , * params .Silenced , * params .Inhibited , * params .Active , marker . NewAlertMarker () )
436434 alertGroups , allReceivers , err := api .alertGroups (ctx , rf , af )
437435 if err != nil {
438436 message := "Failed to get alert groups"
@@ -459,7 +457,7 @@ func (api *API) getAlertGroupsHandler(params alertgroup_ops.GetAlertGroupsParams
459457 for _ , alert := range alertGroup .Alerts {
460458 fp := alert .Fingerprint ()
461459 receivers := allReceivers [fp ]
462- status := api . getAlertStatus ( fp )
460+ status := alertGroup . AlertStatuses [ fp ]
463461 apiAlert := AlertToOpenAPIAlert (alert , status , receivers , mutedBy )
464462 ag .Alerts = append (ag .Alerts , apiAlert )
465463 }
@@ -469,8 +467,8 @@ func (api *API) getAlertGroupsHandler(params alertgroup_ops.GetAlertGroupsParams
469467 return alertgroup_ops .NewGetAlertGroupsOK ().WithPayload (res )
470468}
471469
472- func (api * API ) alertFilter (matchers []* labels.Matcher , silenced , inhibited , active bool ) func (a * types .Alert , now time.Time ) bool {
473- return func (a * types .Alert , now time.Time ) bool {
470+ func (api * API ) alertFilter (matchers []* labels.Matcher , silenced , inhibited , active bool , m marker. AlertMarker ) func (a * alert .Alert , now time.Time ) bool {
471+ return func (a * alert .Alert , now time.Time ) bool {
474472 ctx , span := tracer .Start (context .Background (), "alertFilter" )
475473 defer span .End ()
476474
@@ -479,12 +477,14 @@ func (api *API) alertFilter(matchers []*labels.Matcher, silenced, inhibited, act
479477 }
480478
481479 // Set alert's current status based on its label set.
480+ // The inhibitor and silencer write to m via the context.
481+ ctx = marker .WithAlertMarker (ctx , m )
482482 api .setAlertStatus (ctx , a .Labels )
483483
484484 // Get alert's current status after seeing if it is suppressed.
485- status := api . getAlertStatus (a .Fingerprint ())
485+ status := m . Status (a .Fingerprint ())
486486
487- if ! active && status .State == types .AlertStateActive {
487+ if ! active && status .State == alert .AlertStateActive {
488488 return false
489489 }
490490
0 commit comments