mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Use Registerer to Register All Metrics
* Made Metric a Gauge so that it can be registered.
This commit is contained in:
parent
7ba0a9e81a
commit
f27ce34a13
|
@ -184,7 +184,6 @@ func Main() int {
|
||||||
if instrumentedStorage, ok := localStorage.(prometheus.Collector); ok {
|
if instrumentedStorage, ok := localStorage.(prometheus.Collector); ok {
|
||||||
prometheus.MustRegister(instrumentedStorage)
|
prometheus.MustRegister(instrumentedStorage)
|
||||||
}
|
}
|
||||||
prometheus.MustRegister(notifier)
|
|
||||||
prometheus.MustRegister(configSuccess)
|
prometheus.MustRegister(configSuccess)
|
||||||
prometheus.MustRegister(configSuccessTime)
|
prometheus.MustRegister(configSuccessTime)
|
||||||
|
|
||||||
|
|
|
@ -83,11 +83,11 @@ type alertMetrics struct {
|
||||||
errors *prometheus.CounterVec
|
errors *prometheus.CounterVec
|
||||||
sent *prometheus.CounterVec
|
sent *prometheus.CounterVec
|
||||||
dropped prometheus.Counter
|
dropped prometheus.Counter
|
||||||
queueLength prometheus.Gauge
|
queueLength prometheus.GaugeFunc
|
||||||
queueCapacity prometheus.Metric
|
queueCapacity prometheus.Gauge
|
||||||
}
|
}
|
||||||
|
|
||||||
func newAlertMetrics(r prometheus.Registerer, o *Options) *alertMetrics {
|
func newAlertMetrics(r prometheus.Registerer, queueCap int, queueLen func() float64) *alertMetrics {
|
||||||
m := &alertMetrics{
|
m := &alertMetrics{
|
||||||
latency: prometheus.NewSummaryVec(prometheus.SummaryOpts{
|
latency: prometheus.NewSummaryVec(prometheus.SummaryOpts{
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
|
@ -119,29 +119,30 @@ func newAlertMetrics(r prometheus.Registerer, o *Options) *alertMetrics {
|
||||||
Name: "dropped_total",
|
Name: "dropped_total",
|
||||||
Help: "Total number of alerts dropped due to errors when sending to Alertmanager.",
|
Help: "Total number of alerts dropped due to errors when sending to Alertmanager.",
|
||||||
}),
|
}),
|
||||||
queueLength: prometheus.NewGauge(prometheus.GaugeOpts{
|
queueLength: prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
Subsystem: subsystem,
|
Subsystem: subsystem,
|
||||||
Name: "queue_length",
|
Name: "queue_length",
|
||||||
Help: "The number of alert notifications in the queue.",
|
Help: "The number of alert notifications in the queue.",
|
||||||
|
}, queueLen),
|
||||||
|
queueCapacity: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||||
|
Namespace: namespace,
|
||||||
|
Subsystem: subsystem,
|
||||||
|
Name: "queue_capacity",
|
||||||
|
Help: "The capacity of the alert notifications queue.",
|
||||||
}),
|
}),
|
||||||
queueCapacity: prometheus.MustNewConstMetric(
|
|
||||||
prometheus.NewDesc(
|
|
||||||
prometheus.BuildFQName(namespace, subsystem, "queue_capacity"),
|
|
||||||
"The capacity of the alert notifications queue.",
|
|
||||||
nil, nil,
|
|
||||||
),
|
|
||||||
prometheus.GaugeValue,
|
|
||||||
float64(o.QueueCapacity),
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m.queueCapacity.Set(float64(queueCap))
|
||||||
|
|
||||||
if r != nil {
|
if r != nil {
|
||||||
r.MustRegister(
|
r.MustRegister(
|
||||||
m.latency,
|
m.latency,
|
||||||
m.errors,
|
m.errors,
|
||||||
m.sent,
|
m.sent,
|
||||||
m.dropped,
|
m.dropped,
|
||||||
|
m.queueLength,
|
||||||
|
m.queueCapacity,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,14 +157,17 @@ func New(o *Options) *Notifier {
|
||||||
o.Do = ctxhttp.Do
|
o.Do = ctxhttp.Do
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Notifier{
|
n := &Notifier{
|
||||||
queue: make(model.Alerts, 0, o.QueueCapacity),
|
queue: make(model.Alerts, 0, o.QueueCapacity),
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
more: make(chan struct{}, 1),
|
more: make(chan struct{}, 1),
|
||||||
opts: o,
|
opts: o,
|
||||||
metrics: newAlertMetrics(o.Registerer, o),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
queueLenFunc := func() float64 { return float64(n.queueLen()) }
|
||||||
|
n.metrics = newAlertMetrics(o.Registerer, o.QueueCapacity, queueLenFunc)
|
||||||
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyConfig updates the status state as the new config requires.
|
// ApplyConfig updates the status state as the new config requires.
|
||||||
|
@ -406,20 +410,6 @@ func (n *Notifier) Stop() {
|
||||||
n.cancel()
|
n.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Describe implements prometheus.Collector.
|
|
||||||
func (n *Notifier) Describe(ch chan<- *prometheus.Desc) {
|
|
||||||
ch <- n.metrics.queueCapacity.Desc()
|
|
||||||
ch <- n.metrics.queueLength.Desc()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Collect implements prometheus.Collector.
|
|
||||||
func (n *Notifier) Collect(ch chan<- prometheus.Metric) {
|
|
||||||
n.metrics.queueLength.Set(float64(n.queueLen()))
|
|
||||||
|
|
||||||
ch <- n.metrics.queueLength
|
|
||||||
ch <- n.metrics.queueCapacity
|
|
||||||
}
|
|
||||||
|
|
||||||
// alertmanager holds Alertmanager endpoint information.
|
// alertmanager holds Alertmanager endpoint information.
|
||||||
type alertmanager interface {
|
type alertmanager interface {
|
||||||
url() string
|
url() string
|
||||||
|
|
|
@ -25,7 +25,6 @@ import (
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
"github.com/prometheus/common/model"
|
"github.com/prometheus/common/model"
|
||||||
"github.com/prometheus/prometheus/config"
|
"github.com/prometheus/prometheus/config"
|
||||||
)
|
)
|
||||||
|
@ -64,7 +63,6 @@ func TestPostPath(t *testing.T) {
|
||||||
|
|
||||||
func TestHandlerNextBatch(t *testing.T) {
|
func TestHandlerNextBatch(t *testing.T) {
|
||||||
h := New(&Options{})
|
h := New(&Options{})
|
||||||
defer unregisterMetrics()
|
|
||||||
|
|
||||||
for i := range make([]struct{}, 2*maxBatchSize+1) {
|
for i := range make([]struct{}, 2*maxBatchSize+1) {
|
||||||
h.queue = append(h.queue, &model.Alert{
|
h.queue = append(h.queue, &model.Alert{
|
||||||
|
@ -120,14 +118,6 @@ func alertsEqual(a, b model.Alerts) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func unregisterMetrics() {
|
|
||||||
m := newAlertMetrics(nil, &Options{})
|
|
||||||
prometheus.DefaultRegisterer.Unregister(m.latency)
|
|
||||||
prometheus.DefaultRegisterer.Unregister(m.errors)
|
|
||||||
prometheus.DefaultRegisterer.Unregister(m.sent)
|
|
||||||
prometheus.DefaultRegisterer.Unregister(m.dropped)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandlerSendAll(t *testing.T) {
|
func TestHandlerSendAll(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
expected model.Alerts
|
expected model.Alerts
|
||||||
|
@ -160,7 +150,6 @@ func TestHandlerSendAll(t *testing.T) {
|
||||||
defer server2.Close()
|
defer server2.Close()
|
||||||
|
|
||||||
h := New(&Options{})
|
h := New(&Options{})
|
||||||
defer unregisterMetrics()
|
|
||||||
h.alertmanagers = append(h.alertmanagers, &alertmanagerSet{
|
h.alertmanagers = append(h.alertmanagers, &alertmanagerSet{
|
||||||
ams: []alertmanager{
|
ams: []alertmanager{
|
||||||
alertmanagerMock{
|
alertmanagerMock{
|
||||||
|
@ -228,7 +217,6 @@ func TestCustomDo(t *testing.T) {
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
defer unregisterMetrics()
|
|
||||||
|
|
||||||
h.sendOne(context.Background(), nil, testURL, []byte(testBody))
|
h.sendOne(context.Background(), nil, testURL, []byte(testBody))
|
||||||
|
|
||||||
|
@ -251,7 +239,6 @@ func TestExternalLabels(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
defer unregisterMetrics()
|
|
||||||
|
|
||||||
// This alert should get the external label attached.
|
// This alert should get the external label attached.
|
||||||
h.Send(&model.Alert{
|
h.Send(&model.Alert{
|
||||||
|
@ -306,7 +293,6 @@ func TestHandlerRelabel(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
defer unregisterMetrics()
|
|
||||||
|
|
||||||
// This alert should be dropped due to the configuration
|
// This alert should be dropped due to the configuration
|
||||||
h.Send(&model.Alert{
|
h.Send(&model.Alert{
|
||||||
|
@ -361,7 +347,6 @@ func TestHandlerQueueing(t *testing.T) {
|
||||||
h := New(&Options{
|
h := New(&Options{
|
||||||
QueueCapacity: 3 * maxBatchSize,
|
QueueCapacity: 3 * maxBatchSize,
|
||||||
})
|
})
|
||||||
defer unregisterMetrics()
|
|
||||||
h.alertmanagers = append(h.alertmanagers, &alertmanagerSet{
|
h.alertmanagers = append(h.alertmanagers, &alertmanagerSet{
|
||||||
ams: []alertmanager{
|
ams: []alertmanager{
|
||||||
alertmanagerMock{
|
alertmanagerMock{
|
||||||
|
|
Loading…
Reference in a new issue