From b75066815299e720a8f3d07637056b023f202805 Mon Sep 17 00:00:00 2001 From: Mechiel Lukkien Date: Fri, 6 Dec 2024 15:07:42 +0100 Subject: [PATCH] add metrics that track how many error/warn/info logging is happening --- mlog/log.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/mlog/log.go b/mlog/log.go index d041c98..122599a 100644 --- a/mlog/log.go +++ b/mlog/log.go @@ -25,10 +25,34 @@ import ( "strings" "sync/atomic" "time" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" ) 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 // command-line tools. Must be set early in a program lifecycle. var Logfmt bool @@ -325,6 +349,15 @@ func (h *handler) Handle(ctx context.Context, r slog.Record) error { if !ok { 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 { r.Message = "..." } else if hideAuth {