2015-01-28 08:27:46 +01:00
|
|
|
package notifications
|
|
|
|
|
|
|
|
import (
|
|
|
|
"expvar"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
2018-02-13 22:30:56 +01:00
|
|
|
|
2020-08-24 13:18:39 +02:00
|
|
|
prometheus "github.com/distribution/distribution/v3/metrics"
|
2018-03-14 01:08:11 +01:00
|
|
|
events "github.com/docker/go-events"
|
2018-02-13 22:30:56 +01:00
|
|
|
"github.com/docker/go-metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// eventsCounter counts total events of incoming, success, failure, and errors
|
2019-02-17 05:46:01 +01:00
|
|
|
eventsCounter = prometheus.NotificationsNamespace.NewLabeledCounter("events", "The number of total events", "type", "endpoint")
|
2018-02-13 22:30:56 +01:00
|
|
|
// pendingGauge measures the pending queue size
|
2019-02-17 05:46:01 +01:00
|
|
|
pendingGauge = prometheus.NotificationsNamespace.NewLabeledGauge("pending", "The gauge of pending events in queue", metrics.Total, "endpoint")
|
2018-02-13 22:30:56 +01:00
|
|
|
// statusCounter counts the total notification call per each status code
|
2019-02-17 05:46:01 +01:00
|
|
|
statusCounter = prometheus.NotificationsNamespace.NewLabeledCounter("status", "The number of status code", "code", "endpoint")
|
2015-01-28 08:27:46 +01:00
|
|
|
)
|
|
|
|
|
2023-11-28 07:50:48 +01:00
|
|
|
// endpoints is global registry of endpoints used to report metrics to expvar
|
|
|
|
var endpoints struct {
|
|
|
|
registered []*Endpoint
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// NOTE(stevvooe): Setup registry metrics structure to report to expvar.
|
|
|
|
// Ideally, we do more metrics through logging but we need some nice
|
|
|
|
// realtime metrics for queue state for now.
|
|
|
|
|
|
|
|
registry := expvar.Get("registry")
|
|
|
|
|
|
|
|
if registry == nil {
|
|
|
|
registry = expvar.NewMap("registry")
|
|
|
|
}
|
|
|
|
|
|
|
|
var notifications expvar.Map
|
|
|
|
notifications.Init()
|
|
|
|
notifications.Set("endpoints", expvar.Func(func() interface{} {
|
|
|
|
endpoints.mu.Lock()
|
|
|
|
defer endpoints.mu.Unlock()
|
|
|
|
|
|
|
|
var names []interface{}
|
|
|
|
for _, v := range endpoints.registered {
|
|
|
|
var epjson struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
EndpointConfig
|
|
|
|
|
|
|
|
Metrics EndpointMetrics
|
|
|
|
}
|
|
|
|
|
|
|
|
epjson.Name = v.Name()
|
|
|
|
epjson.URL = v.URL()
|
|
|
|
epjson.EndpointConfig = v.EndpointConfig
|
|
|
|
|
|
|
|
v.ReadMetrics(&epjson.Metrics)
|
|
|
|
|
|
|
|
names = append(names, epjson)
|
|
|
|
}
|
|
|
|
|
|
|
|
return names
|
|
|
|
}))
|
|
|
|
|
|
|
|
registry.(*expvar.Map).Set("notifications", ¬ifications)
|
|
|
|
|
|
|
|
// register prometheus metrics
|
|
|
|
metrics.Register(prometheus.NotificationsNamespace)
|
|
|
|
}
|
|
|
|
|
2015-01-28 08:27:46 +01:00
|
|
|
// EndpointMetrics track various actions taken by the endpoint, typically by
|
|
|
|
// number of events. The goal of this to export it via expvar but we may find
|
|
|
|
// some other future solution to be better.
|
|
|
|
type EndpointMetrics struct {
|
|
|
|
Pending int // events pending in queue
|
|
|
|
Events int // total events incoming
|
|
|
|
Successes int // total events written successfully
|
|
|
|
Failures int // total events failed
|
|
|
|
Errors int // total events errored
|
|
|
|
Statuses map[string]int // status code histogram, per call event
|
|
|
|
}
|
|
|
|
|
|
|
|
// safeMetrics guards the metrics implementation with a lock and provides a
|
|
|
|
// safe update function.
|
|
|
|
type safeMetrics struct {
|
2018-10-11 15:39:02 +02:00
|
|
|
EndpointName string
|
2015-01-28 08:27:46 +01:00
|
|
|
EndpointMetrics
|
|
|
|
sync.Mutex // protects statuses map
|
|
|
|
}
|
|
|
|
|
|
|
|
// newSafeMetrics returns safeMetrics with map allocated.
|
2018-10-11 15:39:02 +02:00
|
|
|
func newSafeMetrics(name string) *safeMetrics {
|
2015-01-28 08:27:46 +01:00
|
|
|
var sm safeMetrics
|
|
|
|
sm.Statuses = make(map[string]int)
|
2018-10-11 15:39:02 +02:00
|
|
|
sm.EndpointName = name
|
2015-01-28 08:27:46 +01:00
|
|
|
return &sm
|
|
|
|
}
|
|
|
|
|
|
|
|
// httpStatusListener returns the listener for the http sink that updates the
|
2016-02-11 01:26:29 +01:00
|
|
|
// relevant counters.
|
2015-01-28 08:27:46 +01:00
|
|
|
func (sm *safeMetrics) httpStatusListener() httpStatusListener {
|
|
|
|
return &endpointMetricsHTTPStatusListener{
|
|
|
|
safeMetrics: sm,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// eventQueueListener returns a listener that maintains queue related counters.
|
|
|
|
func (sm *safeMetrics) eventQueueListener() eventQueueListener {
|
|
|
|
return &endpointMetricsEventQueueListener{
|
|
|
|
safeMetrics: sm,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// endpointMetricsHTTPStatusListener increments counters related to http sinks
|
2016-02-11 01:26:29 +01:00
|
|
|
// for the relevant events.
|
2015-01-28 08:27:46 +01:00
|
|
|
type endpointMetricsHTTPStatusListener struct {
|
|
|
|
*safeMetrics
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ httpStatusListener = &endpointMetricsHTTPStatusListener{}
|
|
|
|
|
2018-03-14 01:08:11 +01:00
|
|
|
func (emsl *endpointMetricsHTTPStatusListener) success(status int, event events.Event) {
|
2015-01-28 08:27:46 +01:00
|
|
|
emsl.safeMetrics.Lock()
|
|
|
|
defer emsl.safeMetrics.Unlock()
|
2018-03-14 01:08:11 +01:00
|
|
|
emsl.Statuses[fmt.Sprintf("%d %s", status, http.StatusText(status))]++
|
|
|
|
emsl.Successes++
|
2018-02-13 22:30:56 +01:00
|
|
|
|
2018-10-11 15:39:02 +02:00
|
|
|
statusCounter.WithValues(fmt.Sprintf("%d %s", status, http.StatusText(status)), emsl.EndpointName).Inc(1)
|
2018-03-14 01:08:11 +01:00
|
|
|
eventsCounter.WithValues("Successes", emsl.EndpointName).Inc(1)
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 01:08:11 +01:00
|
|
|
func (emsl *endpointMetricsHTTPStatusListener) failure(status int, event events.Event) {
|
2015-01-28 08:27:46 +01:00
|
|
|
emsl.safeMetrics.Lock()
|
|
|
|
defer emsl.safeMetrics.Unlock()
|
2018-03-14 01:08:11 +01:00
|
|
|
emsl.Statuses[fmt.Sprintf("%d %s", status, http.StatusText(status))]++
|
|
|
|
emsl.Failures++
|
2018-02-13 22:30:56 +01:00
|
|
|
|
2018-10-11 15:39:02 +02:00
|
|
|
statusCounter.WithValues(fmt.Sprintf("%d %s", status, http.StatusText(status)), emsl.EndpointName).Inc(1)
|
2018-03-14 01:08:11 +01:00
|
|
|
eventsCounter.WithValues("Failures", emsl.EndpointName).Inc(1)
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 01:08:11 +01:00
|
|
|
func (emsl *endpointMetricsHTTPStatusListener) err(err error, event events.Event) {
|
2015-01-28 08:27:46 +01:00
|
|
|
emsl.safeMetrics.Lock()
|
|
|
|
defer emsl.safeMetrics.Unlock()
|
2018-03-14 01:08:11 +01:00
|
|
|
emsl.Errors++
|
2018-02-13 22:30:56 +01:00
|
|
|
|
2018-03-14 01:08:11 +01:00
|
|
|
eventsCounter.WithValues("Errors", emsl.EndpointName).Inc(1)
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// endpointMetricsEventQueueListener maintains the incoming events counter and
|
|
|
|
// the queues pending count.
|
|
|
|
type endpointMetricsEventQueueListener struct {
|
|
|
|
*safeMetrics
|
|
|
|
}
|
|
|
|
|
2018-03-14 01:08:11 +01:00
|
|
|
func (eqc *endpointMetricsEventQueueListener) ingress(event events.Event) {
|
2015-01-28 08:27:46 +01:00
|
|
|
eqc.Lock()
|
|
|
|
defer eqc.Unlock()
|
2018-03-14 01:08:11 +01:00
|
|
|
eqc.Events++
|
|
|
|
eqc.Pending++
|
2018-02-13 22:30:56 +01:00
|
|
|
|
2018-10-11 15:39:02 +02:00
|
|
|
eventsCounter.WithValues("Events", eqc.EndpointName).Inc()
|
2018-03-14 01:08:11 +01:00
|
|
|
pendingGauge.WithValues(eqc.EndpointName).Inc(1)
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 01:08:11 +01:00
|
|
|
func (eqc *endpointMetricsEventQueueListener) egress(event events.Event) {
|
2015-01-28 08:27:46 +01:00
|
|
|
eqc.Lock()
|
|
|
|
defer eqc.Unlock()
|
2018-03-14 01:08:11 +01:00
|
|
|
eqc.Pending--
|
2018-02-13 22:30:56 +01:00
|
|
|
|
2018-10-11 15:39:02 +02:00
|
|
|
pendingGauge.WithValues(eqc.EndpointName).Dec(1)
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// register places the endpoint into expvar so that stats are tracked.
|
|
|
|
func register(e *Endpoint) {
|
|
|
|
endpoints.mu.Lock()
|
|
|
|
defer endpoints.mu.Unlock()
|
|
|
|
|
|
|
|
endpoints.registered = append(endpoints.registered, e)
|
|
|
|
}
|