metrics: fix global state reset on policy reload

This commit is contained in:
WeebDataHoarder
2025-04-25 22:09:02 +02:00
parent bca5b25f28
commit a1f97adde8
3 changed files with 16 additions and 10 deletions

View File

@@ -41,7 +41,7 @@ func (state *State) ChallengeFailed(r *http.Request, reg *challenge.Registration
} }
logger.Warn("challenge failed", "challenge", reg.Name, "err", err, "redirect", redirect) logger.Warn("challenge failed", "challenge", reg.Name, "err", err, "redirect", redirect)
state.metrics.Challenge(reg.Name, "fail") metrics.Challenge(reg.Name, "fail")
} }
func (state *State) ChallengePassed(r *http.Request, reg *challenge.Registration, redirect string, logger *slog.Logger) { func (state *State) ChallengePassed(r *http.Request, reg *challenge.Registration, redirect string, logger *slog.Logger) {
@@ -50,7 +50,7 @@ func (state *State) ChallengePassed(r *http.Request, reg *challenge.Registration
} }
logger.Warn("challenge passed", "challenge", reg.Name, "redirect", redirect) logger.Warn("challenge passed", "challenge", reg.Name, "redirect", redirect)
state.metrics.Challenge(reg.Name, "pass") metrics.Challenge(reg.Name, "pass")
} }
func (state *State) ChallengeIssued(r *http.Request, reg *challenge.Registration, redirect string, logger *slog.Logger) { func (state *State) ChallengeIssued(r *http.Request, reg *challenge.Registration, redirect string, logger *slog.Logger) {
@@ -59,23 +59,23 @@ func (state *State) ChallengeIssued(r *http.Request, reg *challenge.Registration
} }
logger.Info("challenge issued", "challenge", reg.Name, "redirect", redirect) logger.Info("challenge issued", "challenge", reg.Name, "redirect", redirect)
state.metrics.Challenge(reg.Name, "issue") metrics.Challenge(reg.Name, "issue")
} }
func (state *State) ChallengeChecked(r *http.Request, reg *challenge.Registration, redirect string, logger *slog.Logger) { func (state *State) ChallengeChecked(r *http.Request, reg *challenge.Registration, redirect string, logger *slog.Logger) {
state.metrics.Challenge(reg.Name, "check") metrics.Challenge(reg.Name, "check")
} }
func (state *State) RuleHit(r *http.Request, name string, logger *slog.Logger) { func (state *State) RuleHit(r *http.Request, name string, logger *slog.Logger) {
state.metrics.Rule(name, "hit") metrics.Rule(name, "hit")
} }
func (state *State) RuleMiss(r *http.Request, name string, logger *slog.Logger) { func (state *State) RuleMiss(r *http.Request, name string, logger *slog.Logger) {
state.metrics.Rule(name, "miss") metrics.Rule(name, "miss")
} }
func (state *State) ActionHit(r *http.Request, name policy.RuleAction, logger *slog.Logger) { func (state *State) ActionHit(r *http.Request, name policy.RuleAction, logger *slog.Logger) {
state.metrics.Action(name) metrics.Action(name)
} }
func (state *State) Logger(r *http.Request) *slog.Logger { func (state *State) Logger(r *http.Request) *slog.Logger {

View File

@@ -40,3 +40,11 @@ func (metrics *stateMetrics) Action(action policy.RuleAction) {
func (metrics *stateMetrics) Challenge(name, result string) { func (metrics *stateMetrics) Challenge(name, result string) {
metrics.challenges.With(prometheus.Labels{"challenge": name, "action": result}).Inc() metrics.challenges.With(prometheus.Labels{"challenge": name, "action": result}).Inc()
} }
func (metrics *stateMetrics) Reset() {
metrics.rules.Reset()
metrics.actions.Reset()
metrics.challenges.Reset()
}
var metrics = newMetrics()

View File

@@ -44,8 +44,6 @@ type State struct {
close chan struct{} close chan struct{}
Mux *http.ServeMux Mux *http.ServeMux
metrics *stateMetrics
} }
func NewState(p policy.Policy, opt settings.Settings, settings policy.StateSettings) (handler http.Handler, err error) { func NewState(p policy.Policy, opt settings.Settings, settings policy.StateSettings) (handler http.Handler, err error) {
@@ -53,7 +51,7 @@ func NewState(p policy.Policy, opt settings.Settings, settings policy.StateSetti
state.close = make(chan struct{}) state.close = make(chan struct{})
state.settings = settings state.settings = settings
state.opt = opt state.opt = opt
state.metrics = newMetrics() metrics.Reset()
state.client = &http.Client{ state.client = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error { CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse return http.ErrUseLastResponse