Skip to content

Commit

Permalink
Use setAlertStatusFn
Browse files Browse the repository at this point in the history
This encapsulates the actual logic being done
previously into a single closure that gets passed
to the api when loaded/reloaded.

Additionally, the naming is now a bit clearer. The
use of the Muter interface is a bit confusing as
it was previously being used for its side effect
of marking an alert as inhibited or not, and the
return value was being ignored.

Signed-off-by: stuart nelson <stuartnelson3@gmail.com>
  • Loading branch information
stuartnelson3 committed Feb 11, 2019
1 parent 5786f49 commit e4e404f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
35 changes: 8 additions & 27 deletions api/v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,38 +53,36 @@ type API struct {
peer *cluster.Peer
silences *silence.Silences
alerts provider.Alerts
muter types.Muter
marker types.Marker
getAlertStatus getAlertStatusFn
uptime time.Time

// mtx protects resolveTimeout, alertmanagerConfig and route.
// mtx protects resolveTimeout, alertmanagerConfig, setAlertStatus and route.
mtx sync.RWMutex
// resolveTimeout represents the default resolve timeout that an alert is
// assigned if no end time is specified.
resolveTimeout time.Duration
alertmanagerConfig *config.Config
route *dispatch.Route
setAlertStatus setAlertStatusFn

logger log.Logger

Handler http.Handler
}

type getAlertStatusFn func(prometheus_model.Fingerprint) types.AlertStatus
type setAlertStatusFn func(prometheus_model.LabelSet) error

// NewAPI returns a new Alertmanager API v2
func NewAPI(
alerts provider.Alerts,
marker types.Marker,
sf getAlertStatusFn,
silences *silence.Silences,
peer *cluster.Peer,
l log.Logger,
) (*API, error) {
api := API{
alerts: alerts,
marker: marker,
getAlertStatus: sf,
peer: peer,
silences: silences,
Expand Down Expand Up @@ -125,14 +123,14 @@ func NewAPI(
}

// Update sets the configuration string to a new value.
func (api *API) Update(cfg *config.Config, resolveTimeout time.Duration, muter types.Muter) error {
func (api *API) Update(cfg *config.Config, resolveTimeout time.Duration, setAlertStatus setAlertStatusFn) error {
api.mtx.Lock()
defer api.mtx.Unlock()

api.resolveTimeout = resolveTimeout
api.alertmanagerConfig = cfg
api.route = dispatch.NewRoute(cfg.Route, nil)
api.muter = muter
api.setAlertStatus = setAlertStatus
return nil
}

Expand Down Expand Up @@ -249,27 +247,10 @@ func (api *API) getAlertsHandler(params alert_ops.GetAlertsParams) middleware.Re
continue
}

// TODO: Add a timestamp now()+X, after which the Mutes() will
// be refreshed?
// Will check if the alert is inhibited, and update its status.
api.muter.Mutes(a.Labels)
// Will check if the alert is silenced, and update its status.
sils, err := api.silences.Query(
silence.QState(types.SilenceStateActive),
silence.QMatches(a.Labels),
)
if err != nil {
level.Error(api.logger).Log("msg", "Querying silences failed", "err", err)
}

if len(sils) > 0 {
ids := make([]string, len(sils))
for i, s := range sils {
ids[i] = s.Id
}
api.marker.SetSilenced(a.Labels.Fingerprint(), ids...)
// Set alert's current status based on its label set.
if err := api.setAlertStatus(a.Labels); err != nil {
level.Error(api.logger).Log("msg", "set alert status failed", "err", err)
}

// Get alert's current status after seeing if it is suppressed.
status := api.getAlertStatus(a.Fingerprint())

Expand Down
26 changes: 24 additions & 2 deletions cmd/alertmanager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/model"
"github.com/prometheus/common/promlog"
promlogflag "github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/route"
Expand Down Expand Up @@ -302,7 +303,6 @@ func run() int {

apiV2, err := apiv2.NewAPI(
alerts,
marker,
marker.Status,
silences,
peer,
Expand Down Expand Up @@ -378,7 +378,7 @@ func run() int {
return err
}

err = apiV2.Update(conf, time.Duration(conf.Global.ResolveTimeout), inhibitor)
err = apiV2.Update(conf, time.Duration(conf.Global.ResolveTimeout), setAlertStatus(inhibitor, marker, silences))
if err != nil {
return err
}
Expand Down Expand Up @@ -518,3 +518,25 @@ func md5HashAsMetricValue(data []byte) float64 {
copy(bytes, smallSum)
return float64(binary.LittleEndian.Uint64(bytes))
}

func setAlertStatus(inhibitor *inhibit.Inhibitor, marker types.Marker, silences *silence.Silences) func(model.LabelSet) error {
return func(labels model.LabelSet) error {
inhibitor.Mutes(labels)
sils, err := silences.Query(
silence.QState(types.SilenceStateActive),
silence.QMatches(labels),
)
if err != nil {
return fmt.Errorf("failed to query silences: %v", err)
}

if len(sils) > 0 {
ids := make([]string, len(sils))
for i, s := range sils {
ids[i] = s.Id
}
marker.SetSilenced(labels.Fingerprint(), ids...)
}
return nil
}
}

0 comments on commit e4e404f

Please sign in to comment.