add metrics that track how many error/warn/info logging is happening

This commit is contained in:
Mechiel Lukkien 2024-12-06 15:07:42 +01:00
parent 056b571fb6
commit b750668152
No known key found for this signature in database

View file

@ -25,10 +25,34 @@ import (
"strings" "strings"
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
) )
var noctx = context.Background() var noctx = context.Background()
var (
metricLogInfo = promauto.NewCounter(
prometheus.CounterOpts{
Name: "mox_logging_level_info_total",
Help: "Total number of logging events at level info.",
},
)
metricLogWarn = promauto.NewCounter(
prometheus.CounterOpts{
Name: "mox_logging_level_warn_total",
Help: "Total number of logging events at level warn.",
},
)
metricLogError = promauto.NewCounter(
prometheus.CounterOpts{
Name: "mox_logging_level_error_total",
Help: "Total number of logging events at level error.",
},
)
)
// Logfmt enabled output in logfmt, instead of output more suitable for // Logfmt enabled output in logfmt, instead of output more suitable for
// command-line tools. Must be set early in a program lifecycle. // command-line tools. Must be set early in a program lifecycle.
var Logfmt bool var Logfmt bool
@ -325,6 +349,15 @@ func (h *handler) Handle(ctx context.Context, r slog.Record) error {
if !ok { if !ok {
return nil return nil
} }
if r.Level >= LevelInfo {
if r.Level == LevelInfo {
metricLogInfo.Inc()
} else if r.Level <= LevelWarn {
metricLogWarn.Inc()
} else if r.Level <= LevelError {
metricLogError.Inc()
}
}
if hideData, hideAuth := traceLevel(l, r.Level); hideData { if hideData, hideAuth := traceLevel(l, r.Level); hideData {
r.Message = "..." r.Message = "..."
} else if hideAuth { } else if hideAuth {