Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ script:
# test front-end
- make clean
- cd ui/app/ && make && cd ../..
- make assets
- make assets apiv2
- git diff --exit-code
# test back-end
- make
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ assets: ui/app/script.js ui/app/index.html ui/app/lib template/default.tmpl
cd $(PREFIX)/asset && $(GO) generate
@$(GOFMT) -w ./asset

ui/app/script.js: $(shell find ui/app/src -iname *.elm)
ui/app/script.js: $(shell find ui/app/src -iname *.elm) api/v2/openapi.yaml
cd $(FRONTEND_DIR) && $(MAKE) script.js

.PHONY: proto
Expand All @@ -56,6 +56,7 @@ test/with_api_v2/api_v2_client/models test/with_api_v2/api_v2_client/client: api
.PHONY: clean
clean:
rm -f asset/assets_vfsdata.go
rm -r api/v2/models api/v2/restapi test/with_api_v2/api_v2_client/models test/with_api_v2/api_v2_client/client
cd $(FRONTEND_DIR) && $(MAKE) clean

.PHONY: test
Expand Down
107 changes: 68 additions & 39 deletions api/v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/prometheus/alertmanager/types"

prometheus_model "github.com/prometheus/common/model"
"github.com/prometheus/common/version"
"github.com/prometheus/prometheus/pkg/labels"

"github.com/go-kit/kit/log"
Expand All @@ -54,6 +55,7 @@ type API struct {
silences *silence.Silences
alerts provider.Alerts
getAlertStatus getAlertStatusFn
uptime time.Time

// mtx protects resolveTimeout, alertmanagerConfig and route.
mtx sync.RWMutex
Expand All @@ -78,6 +80,7 @@ func NewAPI(alerts provider.Alerts, sf getAlertStatusFn, silences *silence.Silen
peer: peer,
silences: silences,
logger: l,
uptime: time.Now(),
}

// load embedded swagger file
Expand Down Expand Up @@ -122,16 +125,33 @@ func (api *API) getStatusHandler(params general_ops.GetStatusParams) middleware.

name := api.peer.Name()
status := api.peer.Status()
original := api.alertmanagerConfig.String()
uptime := strfmt.DateTime(api.uptime)
resp := open_api_models.AlertmanagerStatus{
Name: &name,
StatusInCluster: &status,
Peers: []*open_api_models.PeerStatus{},
Uptime: &uptime,
VersionInfo: &open_api_models.VersionInfo{
Version: &version.Version,
Revision: &version.Revision,
Branch: &version.Branch,
BuildUser: &version.BuildUser,
BuildDate: &version.BuildDate,
GoVersion: &version.GoVersion,
},
Config: &open_api_models.AlertmanagerConfig{
Original: &original,
},
Cluster: &open_api_models.ClusterStatus{
Name: &name,
Status: &status,
Peers: []*open_api_models.PeerStatus{},
},
}

for _, n := range api.peer.Peers() {
resp.Peers = append(resp.Peers, &open_api_models.PeerStatus{
Name: n.Name,
Address: n.Address(),
address := n.Address()
resp.Cluster.Peers = append(resp.Cluster.Peers, &open_api_models.PeerStatus{
Name: &n.Name,
Address: &address,
})
}

Expand All @@ -144,7 +164,7 @@ func (api *API) getReceiversHandler(params receiver_ops.GetReceiversParams) midd

receivers := make([]*open_api_models.Receiver, 0, len(api.alertmanagerConfig.Receivers))
for _, r := range api.alertmanagerConfig.Receivers {
receivers = append(receivers, &open_api_models.Receiver{Name: r.Name})
receivers = append(receivers, &open_api_models.Receiver{Name: &r.Name})
}

return receiver_ops.NewGetReceiversOK().WithPayload(receivers)
Expand Down Expand Up @@ -196,7 +216,7 @@ func (api *API) getAlertsHandler(params alert_ops.GetAlertsParams) middleware.Re
routes := api.route.Match(a.Labels)
receivers := make([]*open_api_models.Receiver, 0, len(routes))
for _, r := range routes {
receivers = append(receivers, &open_api_models.Receiver{Name: r.RouteOpts.Receiver})
receivers = append(receivers, &open_api_models.Receiver{Name: &r.RouteOpts.Receiver})
}

if receiverFilter != nil && !receiversMatchFilter(receivers, receiverFilter) {
Expand Down Expand Up @@ -230,6 +250,8 @@ func (api *API) getAlertsHandler(params alert_ops.GetAlertsParams) middleware.Re
continue
}

state := string(status.State)

alert := open_api_models.Alert{
Annotations: modelLabelSetToAPILabelSet(a.Annotations),
EndsAt: strfmt.DateTime(a.EndsAt),
Expand All @@ -239,7 +261,7 @@ func (api *API) getAlertsHandler(params alert_ops.GetAlertsParams) middleware.Re
Receivers: receivers,
StartsAt: strfmt.DateTime(a.StartsAt),
Status: &open_api_models.AlertStatus{
State: string(status.State),
State: &state,
SilencedBy: status.SilencedBy,
InhibitedBy: status.InhibitedBy,
},
Expand Down Expand Up @@ -368,7 +390,7 @@ func apiLabelSetToModelLabelSet(apiLabelSet open_api_models.LabelSet) prometheus

func receiversMatchFilter(receivers []*open_api_models.Receiver, filter *regexp.Regexp) bool {
for _, r := range receivers {
if filter.MatchString(string(r.Name)) {
if filter.MatchString(string(*r.Name)) {
return true
}
}
Expand Down Expand Up @@ -428,14 +450,14 @@ func (api *API) getSilencesHandler(params silence_ops.GetSilencesParams) middlew
return silence_ops.NewGetSilencesInternalServerError().WithPayload(err.Error())
}

sils := []*open_api_models.Silence{}
sils := open_api_models.GettableSilences{}
for _, ps := range psils {
silence, err := silenceFromProto(ps)
silence, err := gettableSilenceFromProto(ps)
if err != nil {
level.Error(api.logger).Log("msg", "failed to unmarshal silence from proto", "err", err)
return silence_ops.NewGetSilencesInternalServerError().WithPayload(err.Error())
}
if !silenceMatchesFilterLabels(silence, matchers) {
if !gettableSilenceMatchesFilterLabels(silence, matchers) {
continue
}
sils = append(sils, &silence)
Expand All @@ -444,10 +466,10 @@ func (api *API) getSilencesHandler(params silence_ops.GetSilencesParams) middlew
return silence_ops.NewGetSilencesOK().WithPayload(sils)
}

func silenceMatchesFilterLabels(s open_api_models.Silence, matchers []*labels.Matcher) bool {
func gettableSilenceMatchesFilterLabels(s open_api_models.GettableSilence, matchers []*labels.Matcher) bool {
sms := make(map[string]string)
for _, m := range s.Matchers {
sms[m.Name] = m.Value
sms[*m.Name] = *m.Value
}

return matchFilterLabels(matchers, sms)
Expand All @@ -465,7 +487,7 @@ func (api *API) getSilenceHandler(params silence_ops.GetSilenceParams) middlewar
return silence_ops.NewGetSilenceNotFound()
}

sil, err := silenceFromProto(sils[0])
sil, err := gettableSilenceFromProto(sils[0])
if err != nil {
level.Error(api.logger).Log("msg", "failed to convert unmarshal from proto", "err", err)
return silence_ops.NewGetSilenceInternalServerError().WithPayload(err.Error())
Expand All @@ -484,29 +506,37 @@ func (api *API) deleteSilenceHandler(params silence_ops.DeleteSilenceParams) mid
return silence_ops.NewDeleteSilenceOK()
}

func silenceFromProto(s *silencepb.Silence) (open_api_models.Silence, error) {
sil := open_api_models.Silence{
ID: s.Id,
StartsAt: strfmt.DateTime(s.StartsAt),
EndsAt: strfmt.DateTime(s.EndsAt),
UpdatedAt: strfmt.DateTime(s.UpdatedAt),
func gettableSilenceFromProto(s *silencepb.Silence) (open_api_models.GettableSilence, error) {
start := strfmt.DateTime(s.StartsAt)
end := strfmt.DateTime(s.EndsAt)
updated := strfmt.DateTime(s.UpdatedAt)
state := string(types.CalcSilenceState(s.StartsAt, s.EndsAt))
sil := open_api_models.GettableSilence{
Silence: open_api_models.Silence{
StartsAt: &start,
EndsAt: &end,
Comment: &s.Comment,
CreatedBy: &s.CreatedBy,
},
ID: &s.Id,
UpdatedAt: &updated,
Status: &open_api_models.SilenceStatus{
State: string(types.CalcSilenceState(s.StartsAt, s.EndsAt)),
State: &state,
},
Comment: s.Comment,
CreatedBy: s.CreatedBy,
}

for _, m := range s.Matchers {
matcher := &open_api_models.Matcher{
Name: m.Name,
Value: m.Pattern,
Regex: m.Pattern,
Name: &m.Name,
Value: &m.Pattern,
}
switch m.Type {
case silencepb.Matcher_EQUAL:
f := false
matcher.IsRegex = &f
case silencepb.Matcher_REGEXP:
matcher.IsRegex = true
t := true
matcher.IsRegex = &t
default:
return sil, fmt.Errorf(
"unknown matcher type for matcher '%v' in silence '%v'",
Expand All @@ -522,7 +552,7 @@ func silenceFromProto(s *silencepb.Silence) (open_api_models.Silence, error) {

func (api *API) postSilencesHandler(params silence_ops.PostSilencesParams) middleware.Responder {

sil, err := silenceToProto(params.Silence)
sil, err := postableSilenceToProto(params.Silence)
if err != nil {
level.Error(api.logger).Log("msg", "failed to marshal silence to proto", "err", err)
return silence_ops.NewPostSilencesBadRequest().WithPayload(
Expand Down Expand Up @@ -553,22 +583,21 @@ func (api *API) postSilencesHandler(params silence_ops.PostSilencesParams) middl
})
}

func silenceToProto(s *open_api_models.Silence) (*silencepb.Silence, error) {
func postableSilenceToProto(s *open_api_models.PostableSilence) (*silencepb.Silence, error) {
sil := &silencepb.Silence{
Id: s.ID,
StartsAt: time.Time(s.StartsAt),
EndsAt: time.Time(s.EndsAt),
UpdatedAt: time.Time(s.UpdatedAt),
Comment: s.Comment,
CreatedBy: s.CreatedBy,
StartsAt: time.Time(*s.StartsAt),
EndsAt: time.Time(*s.EndsAt),
Comment: *s.Comment,
CreatedBy: *s.CreatedBy,
}
for _, m := range s.Matchers {
matcher := &silencepb.Matcher{
Name: m.Name,
Pattern: m.Value,
Name: *m.Name,
Pattern: *m.Value,
Type: silencepb.Matcher_EQUAL,
}
if m.IsRegex {
if *m.IsRegex {
matcher.Type = silencepb.Matcher_REGEXP
}
sil.Matchers = append(sil.Matchers, matcher)
Expand Down
101 changes: 2 additions & 99 deletions api/v2/models/alert.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading