Skip to content

Commit

Permalink
Merge branch 'master' into stn/correctly-mark-api-silences
Browse files Browse the repository at this point in the history
Signed-off-by: stuart nelson <stuartnelson3@gmail.com>
  • Loading branch information
stuartnelson3 committed Feb 11, 2019
2 parents 2e75c64 + 17e8cc0 commit a26e464
Show file tree
Hide file tree
Showing 629 changed files with 219,118 additions and 3,289 deletions.
1 change: 1 addition & 0 deletions .promu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ crossbuild:
- linux/ppc64le
- linux/mips64
- linux/mips64le
- linux/s390x
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## Next release

* [CHANGE] Make api/v2/status.cluster.{name,peers} properties optional for Alertmanager with disabled clustering (#1728)

## 0.16.1 / 2019-01-31

* [BUGFIX] Do not populate cluster info if clustering is disabled in API v2 (#1726)

## 0.16.0 / 2019-01-17

This release introduces a new API v2, fully generated via the OpenAPI project
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ PRECHECK_OPTIONS_bzr = version
build-all: assets apiv2 build

assets: ui/app/script.js ui/app/index.html ui/app/lib template/default.tmpl
cd $(PREFIX)/asset && $(GO) generate
GO111MODULE=$(GO111MODULE) $(GO) generate ./asset
@$(GOFMT) -w ./asset

ui/app/script.js: $(shell find ui/app/src -iname *.elm) api/v2/openapi.yaml
Expand Down
5 changes: 2 additions & 3 deletions Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ GO_VERSION ?= $(shell $(GO) version)
GO_VERSION_NUMBER ?= $(word 3, $(GO_VERSION))
PRE_GO_111 ?= $(shell echo $(GO_VERSION_NUMBER) | grep -E 'go1\.(10|[0-9])\.')

unexport GOVENDOR
GOVENDOR :=
GO111MODULE :=
ifeq (, $(PRE_GO_111))
ifneq (,$(wildcard go.mod))
# Enforce Go modules support just in case the directory is inside GOPATH (and for Travis CI).
Expand All @@ -57,8 +58,6 @@ $(warning Some recipes may not work as expected as the current Go runtime is '$(
# This repository isn't using Go modules (yet).
GOVENDOR := $(FIRST_GOPATH)/bin/govendor
endif

unexport GO111MODULE
endif
PROMU := $(FIRST_GOPATH)/bin/promu
STATICCHECK := $(FIRST_GOPATH)/bin/staticcheck
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ receiver: team-X-pager
### Routes
Amtool allows you to vizualize the routes of your configuration in form of text tree view.
Amtool allows you to visualize the routes of your configuration in form of text tree view.
Also you can use it to test the routing by passing it label set of an alert
and it prints out all receivers the alert would match ordered and separated by `,`.
(If you use `--verify.receivers` amtool returns error code 1 on mismatch)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.16.0
0.16.1
96 changes: 96 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2019 Prometheus Team
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
"net/http"
"time"

apiv1 "github.com/prometheus/alertmanager/api/v1"
apiv2 "github.com/prometheus/alertmanager/api/v2"
"github.com/prometheus/alertmanager/cluster"
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/provider"
"github.com/prometheus/alertmanager/silence"
"github.com/prometheus/alertmanager/types"
"github.com/prometheus/common/model"
"github.com/prometheus/common/route"

"github.com/go-kit/kit/log"
)

// API represents all APIs of Alertmanager.
type API struct {
v1 *apiv1.API
v2 *apiv2.API
}

// New creates a new API object combining all API versions.
func New(
alerts provider.Alerts,
silences *silence.Silences,
sf func(model.Fingerprint) types.AlertStatus,
peer *cluster.Peer,
l log.Logger,
) (*API, error) {
v1 := apiv1.New(
alerts,
silences,
sf,
peer,
log.With(l, "version", "v1"),
)

v2, err := apiv2.NewAPI(
alerts,
sf,
silences,
peer,
log.With(l, "version", "v2"),
)

if err != nil {
return nil, err
}

return &API{
v1: v1,
v2: v2,
}, nil
}

// Register all APIs with the given router and return a mux.
func (api *API) Register(r *route.Router, routePrefix string) *http.ServeMux {
api.v1.Register(r.WithPrefix("/api/v1"))

mux := http.NewServeMux()
mux.Handle("/", r)

apiPrefix := ""
if routePrefix != "/" {
apiPrefix = routePrefix
}
mux.Handle(apiPrefix+"/api/v2/", http.StripPrefix(apiPrefix+"/api/v2", api.v2.Handler))

return mux
}

// Update config and resolve timeout of each API.
func (api *API) Update(cfg *config.Config, resolveTimeout time.Duration, setAlertStatus func(model.LabelSet) error) error {
if err := api.v1.Update(cfg, resolveTimeout); err != nil {
return err
}

return api.v2.Update(cfg, resolveTimeout, setAlertStatus)
}
31 changes: 21 additions & 10 deletions api/v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ func (api *API) getStatusHandler(params general_ops.GetStatusParams) middleware.
api.mtx.RLock()
defer api.mtx.RUnlock()

name := api.peer.Name()
status := api.peer.Status()
original := api.alertmanagerConfig.String()
uptime := strfmt.DateTime(api.uptime)

status := open_api_models.ClusterStatusStatusDisabled

resp := open_api_models.AlertmanagerStatus{
Uptime: &uptime,
VersionInfo: &open_api_models.VersionInfo{
Expand All @@ -156,18 +157,28 @@ func (api *API) getStatusHandler(params general_ops.GetStatusParams) middleware.
Original: &original,
},
Cluster: &open_api_models.ClusterStatus{
Name: &name,
Status: &status,
Peers: []*open_api_models.PeerStatus{},
},
}

for _, n := range api.peer.Peers() {
address := n.Address()
resp.Cluster.Peers = append(resp.Cluster.Peers, &open_api_models.PeerStatus{
Name: &n.Name,
Address: &address,
})
// If alertmanager cluster feature is disabled, then api.peers == nil.
if api.peer != nil {
status := api.peer.Status()

peers := []*open_api_models.PeerStatus{}
for _, n := range api.peer.Peers() {
address := n.Address()
peers = append(peers, &open_api_models.PeerStatus{
Name: &n.Name,
Address: &address,
})
}

resp.Cluster = &open_api_models.ClusterStatus{
Name: api.peer.Name(),
Status: &status,
Peers: peers,
}
}

return general_ops.NewGetStatusOK().WithPayload(&resp)
Expand Down
50 changes: 50 additions & 0 deletions api/v2/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2019 Prometheus Team
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v2

import (
"testing"
"time"

general_ops "github.com/prometheus/alertmanager/api/v2/restapi/operations/general"
"github.com/prometheus/alertmanager/config"
)

// If api.peers == nil, Alertmanager cluster feature is disabled. Make sure to
// not try to access properties of peer, which would trigger a nil pointer
// dereference.
func TestGetStatusHandlerWithNilPeer(t *testing.T) {
api := API{
uptime: time.Now(),
peer: nil,
alertmanagerConfig: &config.Config{},
}

// Test ensures this method call does not panic.
status := api.getStatusHandler(general_ops.GetStatusParams{}).(*general_ops.GetStatusOK)

c := status.Payload.Cluster

if c == nil || c.Status == nil {
t.Fatal("expected cluster status not to be nil, violating the openapi specification")
}

if c.Peers != nil {
t.Fatal("expected cluster peers to be nil when api.peer is nil, violating the openapi specification")
}

if c.Name != "" {
t.Fatal("expected cluster name to be empty, violating the openapi specification")
}
}
60 changes: 42 additions & 18 deletions api/v2/models/cluster_status.go

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

3 changes: 1 addition & 2 deletions api/v2/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,14 @@ definitions:
type: string
status:
type: string
enum: ["ready", "settling", "disabled"]
peers:
type: array
minimum: 0
items:
$ref: '#/definitions/peerStatus'
required:
- name
- status
- peers
alertmanagerConfig:
type: object
properties:
Expand Down
Loading

0 comments on commit a26e464

Please sign in to comment.