refactor(metrics): simplify names (#3515)

pull/3513/head^2
James Elliott 2022-06-14 21:51:33 +10:00 committed by GitHub
parent 001589cd6d
commit 0eb6e31252
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 48 deletions

View File

@ -28,105 +28,85 @@ type Prometheus struct {
} }
// RecordRequest takes the statusCode string, requestMethod string, and the elapsed time.Duration to record the request and request duration metrics. // RecordRequest takes the statusCode string, requestMethod string, and the elapsed time.Duration to record the request and request duration metrics.
func (p *Prometheus) RecordRequest(statusCode, requestMethod string, elapsed time.Duration) { func (r *Prometheus) RecordRequest(statusCode, requestMethod string, elapsed time.Duration) {
if p.reqCounter == nil || p.reqDuration == nil { r.reqCounter.WithLabelValues(statusCode, requestMethod).Inc()
return r.reqDuration.WithLabelValues(statusCode).Observe(elapsed.Seconds())
}
p.reqCounter.WithLabelValues(statusCode, requestMethod).Inc()
p.reqDuration.WithLabelValues(statusCode).Observe(elapsed.Seconds())
} }
// RecordVerifyRequest takes the statusCode string to record the verify endpoint request metrics. // RecordVerifyRequest takes the statusCode string to record the verify endpoint request metrics.
func (p *Prometheus) RecordVerifyRequest(statusCode string) { func (r *Prometheus) RecordVerifyRequest(statusCode string) {
if p.reqVerifyCounter == nil { r.reqVerifyCounter.WithLabelValues(statusCode).Inc()
return
}
p.reqVerifyCounter.WithLabelValues(statusCode).Inc()
} }
// RecordAuthentication takes the success and regulated booleans and a method string to record the authentication metrics. // RecordAuthentication takes the success and regulated booleans and a method string to record the authentication metrics.
func (p *Prometheus) RecordAuthentication(success, banned bool, authType string) { func (r *Prometheus) RecordAuthentication(success, banned bool, authType string) {
switch authType { switch authType {
case "1fa", "": case "1fa", "":
if p.auth1FACounter == nil { r.auth1FACounter.WithLabelValues(strconv.FormatBool(success), strconv.FormatBool(banned)).Inc()
return
}
p.auth1FACounter.WithLabelValues(strconv.FormatBool(success), strconv.FormatBool(banned)).Inc()
default: default:
if p.auth2FACounter == nil { r.auth2FACounter.WithLabelValues(strconv.FormatBool(success), strconv.FormatBool(banned), authType).Inc()
return
}
p.auth2FACounter.WithLabelValues(strconv.FormatBool(success), strconv.FormatBool(banned), authType).Inc()
} }
} }
// RecordAuthenticationDuration takes the statusCode string, requestMethod string, and the elapsed time.Duration to record the request and request duration metrics. // RecordAuthenticationDuration takes the statusCode string, requestMethod string, and the elapsed time.Duration to record the request and request duration metrics.
func (p *Prometheus) RecordAuthenticationDuration(success bool, elapsed time.Duration) { func (r *Prometheus) RecordAuthenticationDuration(success bool, elapsed time.Duration) {
if p.authDuration == nil { r.authDuration.WithLabelValues(strconv.FormatBool(success)).Observe(elapsed.Seconds())
return
}
p.authDuration.WithLabelValues(strconv.FormatBool(success)).Observe(elapsed.Seconds())
} }
func (p *Prometheus) register() { func (r *Prometheus) register() {
p.authDuration = promauto.NewHistogramVec( r.authDuration = promauto.NewHistogramVec(
prometheus.HistogramOpts{ prometheus.HistogramOpts{
Subsystem: "authelia", Subsystem: "authelia",
Name: "authentication_duration_seconds", Name: "authentication_duration",
Help: "The time an authentication attempt takes in seconds.", Help: "The time an authentication attempt takes in seconds.",
Buckets: []float64{.0005, .00075, .001, .005, .01, .025, .05, .075, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.8, 0.9, 1, 5, 10, 15, 30, 60}, Buckets: []float64{.0005, .00075, .001, .005, .01, .025, .05, .075, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.8, 0.9, 1, 5, 10, 15, 30, 60},
}, },
[]string{"success"}, []string{"success"},
) )
p.reqDuration = promauto.NewHistogramVec( r.reqDuration = promauto.NewHistogramVec(
prometheus.HistogramOpts{ prometheus.HistogramOpts{
Subsystem: "authelia", Subsystem: "authelia",
Name: "request_duration_seconds", Name: "request_duration",
Help: "The time a HTTP request takes to process in seconds.", Help: "The time a HTTP request takes to process in seconds.",
Buckets: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10, 15, 20, 30, 40, 50, 60}, Buckets: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10, 15, 20, 30, 40, 50, 60},
}, },
[]string{"code"}, []string{"code"},
) )
p.reqCounter = promauto.NewCounterVec( r.reqCounter = promauto.NewCounterVec(
prometheus.CounterOpts{ prometheus.CounterOpts{
Subsystem: "authelia", Subsystem: "authelia",
Name: "requests_total", Name: "request",
Help: "The number of HTTP requests processed.", Help: "The number of HTTP requests processed.",
}, },
[]string{"code", "method"}, []string{"code", "method"},
) )
p.reqVerifyCounter = promauto.NewCounterVec( r.reqVerifyCounter = promauto.NewCounterVec(
prometheus.CounterOpts{ prometheus.CounterOpts{
Subsystem: "authelia", Subsystem: "authelia",
Name: "verify_requests_total", Name: "verify_request",
Help: "The number of verify requests processed.", Help: "The number of verify requests processed.",
}, },
[]string{"code"}, []string{"code"},
) )
p.auth1FACounter = promauto.NewCounterVec( r.auth1FACounter = promauto.NewCounterVec(
prometheus.CounterOpts{ prometheus.CounterOpts{
Subsystem: "authelia", Subsystem: "authelia",
Name: "authentication_first_factor_total", Name: "authentication_first_factor",
Help: "The number of 1FA authentications processed.", Help: "The number of 1FA authentications processed.",
}, },
[]string{"success", "banned"}, []string{"success", "banned"},
) )
p.auth2FACounter = promauto.NewCounterVec( r.auth2FACounter = promauto.NewCounterVec(
prometheus.CounterOpts{ prometheus.CounterOpts{
Subsystem: "authelia", Subsystem: "authelia",
Name: "authentication_second_factor_total", Name: "authentication_second_factor",
Help: "The number of 2FA authentications processed.", Help: "The number of 2FA authentications processed.",
}, },
[]string{"success", "banned", "method"}, []string{"success", "banned", "type"},
) )
} }

View File

@ -285,10 +285,8 @@ func (s *StandaloneSuite) TestShouldRecordMetrics() {
metrics := string(body) metrics := string(body)
s.Assert().Contains(metrics, "authelia_request_duration_seconds_bucket{") s.Assert().Contains(metrics, "authelia_request_duration_bucket{")
s.Assert().Contains(metrics, "authelia_request_duration_seconds_sum{") s.Assert().Contains(metrics, "authelia_request_duration_sum{")
s.Assert().Contains(metrics, "go_gc_cycles_forced_gc_cycles_total")
s.Assert().Contains(metrics, "go_gc_cycles_total_gc_cycles_total")
} }
func (s *StandaloneSuite) TestStandaloneWebDriverScenario() { func (s *StandaloneSuite) TestStandaloneWebDriverScenario() {