Merge pull request #1736 from mxinden/combine-apis
api: Combine v1 and v2 into generic api
This commit is contained in:
commit
083639e31e
|
@ -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) error {
|
||||
if err := api.v1.Update(cfg, resolveTimeout); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return api.v2.Update(cfg, resolveTimeout)
|
||||
}
|
|
@ -40,8 +40,7 @@ import (
|
|||
"github.com/prometheus/common/version"
|
||||
"gopkg.in/alecthomas/kingpin.v2"
|
||||
|
||||
apiv1 "github.com/prometheus/alertmanager/api/v1"
|
||||
apiv2 "github.com/prometheus/alertmanager/api/v2"
|
||||
"github.com/prometheus/alertmanager/api"
|
||||
"github.com/prometheus/alertmanager/cluster"
|
||||
"github.com/prometheus/alertmanager/config"
|
||||
"github.com/prometheus/alertmanager/dispatch"
|
||||
|
@ -292,23 +291,16 @@ func run() int {
|
|||
)
|
||||
defer disp.Stop()
|
||||
|
||||
apiV1 := apiv1.New(
|
||||
api, err := api.New(
|
||||
alerts,
|
||||
silences,
|
||||
marker.Status,
|
||||
peer,
|
||||
log.With(logger, "component", "api/v1"),
|
||||
log.With(logger, "component", "api"),
|
||||
)
|
||||
|
||||
apiV2, err := apiv2.NewAPI(
|
||||
alerts,
|
||||
marker.Status,
|
||||
silences,
|
||||
peer,
|
||||
log.With(logger, "component", "api/v2"),
|
||||
)
|
||||
if err != nil {
|
||||
level.Error(logger).Log("err", fmt.Errorf("failed to create API v2: %v", err.Error()))
|
||||
level.Error(logger).Log("err", fmt.Errorf("failed to create API: %v", err.Error()))
|
||||
return 1
|
||||
}
|
||||
|
||||
|
@ -350,12 +342,7 @@ func run() int {
|
|||
|
||||
hash = md5HashAsMetricValue(plainCfg)
|
||||
|
||||
err = apiV1.Update(conf, time.Duration(conf.Global.ResolveTimeout))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = apiV2.Update(conf, time.Duration(conf.Global.ResolveTimeout))
|
||||
err = api.Update(conf, time.Duration(conf.Global.ResolveTimeout))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -410,16 +397,7 @@ func run() int {
|
|||
|
||||
ui.Register(router, webReload, logger)
|
||||
|
||||
apiV1.Register(router.WithPrefix("/api/v1"))
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/", router)
|
||||
|
||||
apiPrefix := ""
|
||||
if *routePrefix != "/" {
|
||||
apiPrefix = *routePrefix
|
||||
}
|
||||
mux.Handle(apiPrefix+"/api/v2/", http.StripPrefix(apiPrefix+"/api/v2", apiV2.Handler))
|
||||
mux := api.Register(router, *routePrefix)
|
||||
|
||||
srv := http.Server{Addr: *listenAddress, Handler: mux}
|
||||
srvc := make(chan struct{})
|
||||
|
|
Loading…
Reference in New Issue