2022-06-14 07:20:13 +00:00
package metrics
import (
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
// NewPrometheus returns a new Prometheus metrics recorder.
func NewPrometheus ( ) ( provider * Prometheus ) {
provider = & Prometheus { }
provider . register ( )
return provider
}
// Prometheus is a middleware for recording prometheus metrics.
type Prometheus struct {
2023-01-25 09:36:40 +00:00
authnDuration * prometheus . HistogramVec
reqDuration * prometheus . HistogramVec
reqCounter * prometheus . CounterVec
authzCounter * prometheus . CounterVec
authnCounter * prometheus . CounterVec
authn2FACounter * prometheus . CounterVec
2022-06-14 07:20:13 +00:00
}
// RecordRequest takes the statusCode string, requestMethod string, and the elapsed time.Duration to record the request and request duration metrics.
2022-06-14 11:51:33 +00:00
func ( r * Prometheus ) RecordRequest ( statusCode , requestMethod string , elapsed time . Duration ) {
r . reqCounter . WithLabelValues ( statusCode , requestMethod ) . Inc ( )
r . reqDuration . WithLabelValues ( statusCode ) . Observe ( elapsed . Seconds ( ) )
2022-06-14 07:20:13 +00:00
}
2023-01-25 09:36:40 +00:00
// RecordAuthz takes the statusCode string to record the verify endpoint request metrics.
func ( r * Prometheus ) RecordAuthz ( statusCode string ) {
r . authzCounter . WithLabelValues ( statusCode ) . Inc ( )
2022-06-14 07:20:13 +00:00
}
2023-01-25 09:36:40 +00:00
// RecordAuthn takes the success and regulated booleans and a method string to record the authentication metrics.
func ( r * Prometheus ) RecordAuthn ( success , banned bool , authType string ) {
2022-06-14 07:20:13 +00:00
switch authType {
case "1fa" , "" :
2023-01-25 09:36:40 +00:00
r . authnCounter . WithLabelValues ( strconv . FormatBool ( success ) , strconv . FormatBool ( banned ) ) . Inc ( )
2022-06-14 07:20:13 +00:00
default :
2023-01-25 09:36:40 +00:00
r . authn2FACounter . WithLabelValues ( strconv . FormatBool ( success ) , strconv . FormatBool ( banned ) , authType ) . Inc ( )
2022-06-14 07:20:13 +00:00
}
}
// RecordAuthenticationDuration takes the statusCode string, requestMethod string, and the elapsed time.Duration to record the request and request duration metrics.
2022-06-14 11:51:33 +00:00
func ( r * Prometheus ) RecordAuthenticationDuration ( success bool , elapsed time . Duration ) {
2023-01-25 09:36:40 +00:00
r . authnDuration . WithLabelValues ( strconv . FormatBool ( success ) ) . Observe ( elapsed . Seconds ( ) )
2022-06-14 07:20:13 +00:00
}
2022-06-14 11:51:33 +00:00
func ( r * Prometheus ) register ( ) {
2023-01-25 09:36:40 +00:00
r . authnDuration = promauto . NewHistogramVec (
2022-06-14 07:20:13 +00:00
prometheus . HistogramOpts {
Subsystem : "authelia" ,
2023-01-25 09:36:40 +00:00
Name : "authn_duration" ,
2022-06-14 07:20:13 +00:00
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 } ,
} ,
[ ] string { "success" } ,
)
2022-06-14 11:51:33 +00:00
r . reqDuration = promauto . NewHistogramVec (
2022-06-14 07:20:13 +00:00
prometheus . HistogramOpts {
Subsystem : "authelia" ,
2022-06-14 11:51:33 +00:00
Name : "request_duration" ,
2022-06-14 07:20:13 +00:00
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 } ,
} ,
[ ] string { "code" } ,
)
2022-06-14 11:51:33 +00:00
r . reqCounter = promauto . NewCounterVec (
2022-06-14 07:20:13 +00:00
prometheus . CounterOpts {
Subsystem : "authelia" ,
2022-06-14 11:51:33 +00:00
Name : "request" ,
2022-06-14 07:20:13 +00:00
Help : "The number of HTTP requests processed." ,
} ,
[ ] string { "code" , "method" } ,
)
2023-01-25 09:36:40 +00:00
r . authzCounter = promauto . NewCounterVec (
2022-06-14 07:20:13 +00:00
prometheus . CounterOpts {
Subsystem : "authelia" ,
2023-01-25 09:36:40 +00:00
Name : "authz" ,
Help : "The number of authz requests processed." ,
2022-06-14 07:20:13 +00:00
} ,
[ ] string { "code" } ,
)
2023-01-25 09:36:40 +00:00
r . authnCounter = promauto . NewCounterVec (
2022-06-14 07:20:13 +00:00
prometheus . CounterOpts {
Subsystem : "authelia" ,
2023-01-25 09:36:40 +00:00
Name : "authn" ,
2022-06-14 07:20:13 +00:00
Help : "The number of 1FA authentications processed." ,
} ,
[ ] string { "success" , "banned" } ,
)
2023-01-25 09:36:40 +00:00
r . authn2FACounter = promauto . NewCounterVec (
2022-06-14 07:20:13 +00:00
prometheus . CounterOpts {
Subsystem : "authelia" ,
2023-01-25 09:36:40 +00:00
Name : "authn_second_factor" ,
2022-06-14 07:20:13 +00:00
Help : "The number of 2FA authentications processed." ,
} ,
2022-06-14 11:51:33 +00:00
[ ] string { "success" , "banned" , "type" } ,
2022-06-14 07:20:13 +00:00
)
}