api: add missing metrics for API v2

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Simon Pasquier 2019-05-24 14:42:52 +02:00
parent 588b4dd17b
commit adcf283d4c
4 changed files with 78 additions and 38 deletions

View File

@ -126,6 +126,7 @@ func New(opts Options) (*API, error) {
opts.Silences,
opts.Peer,
log.With(l, "version", "v2"),
opts.Registry,
)
if err != nil {

54
api/metrics/metrics.go Normal file
View File

@ -0,0 +1,54 @@
// 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 metrics
import "github.com/prometheus/client_golang/prometheus"
// Alerts stores metrics for alerts which are common across all API versions.
type Alerts struct {
firing prometheus.Counter
resolved prometheus.Counter
invalid prometheus.Counter
}
// NewAlerts returns an *Alerts struct for the given API version.
func NewAlerts(version string, r prometheus.Registerer) *Alerts {
numReceivedAlerts := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "alertmanager_alerts_received_total",
Help: "The total number of received alerts.",
ConstLabels: prometheus.Labels{"version": version},
}, []string{"status"})
numInvalidAlerts := prometheus.NewCounter(prometheus.CounterOpts{
Name: "alertmanager_alerts_invalid_total",
Help: "The total number of received alerts that were invalid.",
ConstLabels: prometheus.Labels{"version": version},
})
if r != nil {
r.MustRegister(numReceivedAlerts, numInvalidAlerts)
}
return &Alerts{
firing: numReceivedAlerts.WithLabelValues("firing"),
resolved: numReceivedAlerts.WithLabelValues("resolved"),
invalid: numInvalidAlerts,
}
}
// Firing returns a counter of firing alerts.
func (a *Alerts) Firing() prometheus.Counter { return a.firing }
// Resolved returns a counter of resolved alerts.
func (a *Alerts) Resolved() prometheus.Counter { return a.resolved }
// Invalid returns a counter of invalid alerts.
func (a *Alerts) Invalid() prometheus.Counter { return a.firing }

View File

@ -31,6 +31,7 @@ import (
"github.com/prometheus/common/version"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/alertmanager/api/metrics"
"github.com/prometheus/alertmanager/cluster"
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/dispatch"
@ -74,9 +75,7 @@ type API struct {
uptime time.Time
peer *cluster.Peer
logger log.Logger
numReceivedAlerts *prometheus.CounterVec
numInvalidAlerts prometheus.Counter
m *metrics.Alerts
getAlertStatus getAlertStatusFn
@ -98,32 +97,14 @@ func New(
l = log.NewNopLogger()
}
numReceivedAlerts := prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "alertmanager",
Name: "alerts_received_total",
Help: "The total number of received alerts.",
}, []string{"status"})
numInvalidAlerts := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "alertmanager",
Name: "alerts_invalid_total",
Help: "The total number of received alerts that were invalid.",
})
numReceivedAlerts.WithLabelValues("firing")
numReceivedAlerts.WithLabelValues("resolved")
if r != nil {
r.MustRegister(numReceivedAlerts, numInvalidAlerts)
}
return &API{
alerts: alerts,
silences: silences,
getAlertStatus: sf,
uptime: time.Now(),
peer: peer,
logger: l,
numReceivedAlerts: numReceivedAlerts,
numInvalidAlerts: numInvalidAlerts,
alerts: alerts,
silences: silences,
getAlertStatus: sf,
uptime: time.Now(),
peer: peer,
logger: l,
m: metrics.NewAlerts("v1", r),
}
}
@ -450,9 +431,9 @@ func (api *API) insertAlerts(w http.ResponseWriter, r *http.Request, alerts ...*
alert.EndsAt = now.Add(resolveTimeout)
}
if alert.EndsAt.After(time.Now()) {
api.numReceivedAlerts.WithLabelValues("firing").Inc()
api.m.Firing().Inc()
} else {
api.numReceivedAlerts.WithLabelValues("resolved").Inc()
api.m.Resolved().Inc()
}
}
@ -466,7 +447,7 @@ func (api *API) insertAlerts(w http.ResponseWriter, r *http.Request, alerts ...*
if err := a.Validate(); err != nil {
validationErrs.Add(err)
api.numInvalidAlerts.Inc()
api.m.Invalid().Inc()
continue
}
validAlerts = append(validAlerts, a)

View File

@ -26,11 +26,13 @@ import (
"github.com/go-openapi/loads"
"github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/strfmt"
"github.com/prometheus/client_golang/prometheus"
prometheus_model "github.com/prometheus/common/model"
"github.com/prometheus/common/version"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/rs/cors"
"github.com/prometheus/alertmanager/api/metrics"
open_api_models "github.com/prometheus/alertmanager/api/v2/models"
"github.com/prometheus/alertmanager/api/v2/restapi"
"github.com/prometheus/alertmanager/api/v2/restapi/operations"
@ -67,6 +69,7 @@ type API struct {
setAlertStatus setAlertStatusFn
logger log.Logger
m *metrics.Alerts
Handler http.Handler
}
@ -83,6 +86,7 @@ func NewAPI(
silences *silence.Silences,
peer *cluster.Peer,
l log.Logger,
r prometheus.Registerer,
) (*API, error) {
api := API{
alerts: alerts,
@ -91,6 +95,7 @@ func NewAPI(
peer: peer,
silences: silences,
logger: l,
m: metrics.NewAlerts("v2", r),
uptime: time.Now(),
}
@ -302,12 +307,11 @@ func (api *API) postAlertsHandler(params alert_ops.PostAlertsParams) middleware.
alert.Timeout = true
alert.EndsAt = now.Add(resolveTimeout)
}
// TODO: Take care of the metrics endpoint
// if alert.EndsAt.After(time.Now()) {
// numReceivedAlerts.WithLabelValues("firing").Inc()
// } else {
// numReceivedAlerts.WithLabelValues("resolved").Inc()
// }
if alert.EndsAt.After(time.Now()) {
api.m.Firing().Inc()
} else {
api.m.Resolved().Inc()
}
}
// Make a best effort to insert all alerts that are valid.
@ -320,7 +324,7 @@ func (api *API) postAlertsHandler(params alert_ops.PostAlertsParams) middleware.
if err := a.Validate(); err != nil {
validationErrs.Add(err)
// numInvalidAlerts.Inc()
api.m.Invalid().Inc()
continue
}
validAlerts = append(validAlerts, a)