|
| 1 | +// Copyright SAP SE |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package monitoring |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "log/slog" |
| 9 | + "path" |
| 10 | + "runtime" |
| 11 | + "sync" |
| 12 | + |
| 13 | + "github.com/prometheus/client_golang/prometheus" |
| 14 | + "go.uber.org/zap/zapcore" |
| 15 | +) |
| 16 | + |
| 17 | +// pcFileCache caches the resolved file path for each program counter. The set |
| 18 | +// of distinct PCs is bounded by the number of log call sites in the binary, so |
| 19 | +// this map grows to a fixed size and all subsequent lookups are lock-free reads. |
| 20 | +var pcFileCache sync.Map // uintptr -> string |
| 21 | + |
| 22 | +// LogMessagesTotal counts warn and error log messages emitted by both the slog |
| 23 | +// and zap loggers. Labels: "level" (warn|error), "file" (relative source path). |
| 24 | +var LogMessagesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 25 | + Namespace: "cortex", |
| 26 | + Name: "log_messages_total", |
| 27 | + Help: "Total number of log messages emitted at warn or error level.", |
| 28 | +}, []string{"level", "file"}) |
| 29 | + |
| 30 | +// shortFilePath returns "parent_dir/filename.go" from any absolute or |
| 31 | +// module-relative path. This is independent of the build environment (no |
| 32 | +// -trimpath needed) and keeps Prometheus label cardinality manageable. |
| 33 | +func shortFilePath(file string) string { |
| 34 | + dir, base := path.Split(file) |
| 35 | + parent := path.Base(dir) |
| 36 | + if parent == "." || parent == "/" { |
| 37 | + return base |
| 38 | + } |
| 39 | + return parent + "/" + base |
| 40 | +} |
| 41 | + |
| 42 | +// --- slog handler wrapper --- |
| 43 | + |
| 44 | +// MetricsSlogHandler wraps an slog.Handler and increments LogMessagesTotal for |
| 45 | +// every warn or error log record. |
| 46 | +type MetricsSlogHandler struct { |
| 47 | + next slog.Handler |
| 48 | +} |
| 49 | + |
| 50 | +// NewMetricsSlogHandler returns a new handler that counts warn/error logs and |
| 51 | +// delegates all calls to next. |
| 52 | +func NewMetricsSlogHandler(next slog.Handler) *MetricsSlogHandler { |
| 53 | + return &MetricsSlogHandler{next: next} |
| 54 | +} |
| 55 | + |
| 56 | +func (h *MetricsSlogHandler) Enabled(ctx context.Context, level slog.Level) bool { |
| 57 | + if h.next == nil { |
| 58 | + return false |
| 59 | + } |
| 60 | + return h.next.Enabled(ctx, level) |
| 61 | +} |
| 62 | + |
| 63 | +func (h *MetricsSlogHandler) Handle(ctx context.Context, r slog.Record) error { |
| 64 | + if r.Level >= slog.LevelWarn { |
| 65 | + level := "warn" |
| 66 | + if r.Level >= slog.LevelError { |
| 67 | + level = "error" |
| 68 | + } |
| 69 | + file := "unknown" |
| 70 | + if r.PC != 0 { |
| 71 | + if cached, ok := pcFileCache.Load(r.PC); ok { |
| 72 | + file = cached.(string) |
| 73 | + } else { |
| 74 | + frames := runtime.CallersFrames([]uintptr{r.PC}) |
| 75 | + f, _ := frames.Next() |
| 76 | + if f.File != "" { |
| 77 | + file = shortFilePath(f.File) |
| 78 | + } |
| 79 | + pcFileCache.Store(r.PC, file) |
| 80 | + } |
| 81 | + } |
| 82 | + LogMessagesTotal.WithLabelValues(level, file).Inc() |
| 83 | + } |
| 84 | + if h.next == nil { |
| 85 | + return nil |
| 86 | + } |
| 87 | + return h.next.Handle(ctx, r) |
| 88 | +} |
| 89 | + |
| 90 | +func (h *MetricsSlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler { |
| 91 | + if h.next == nil { |
| 92 | + return &MetricsSlogHandler{} |
| 93 | + } |
| 94 | + return &MetricsSlogHandler{next: h.next.WithAttrs(attrs)} |
| 95 | +} |
| 96 | + |
| 97 | +func (h *MetricsSlogHandler) WithGroup(name string) slog.Handler { |
| 98 | + if h.next == nil { |
| 99 | + return &MetricsSlogHandler{} |
| 100 | + } |
| 101 | + return &MetricsSlogHandler{next: h.next.WithGroup(name)} |
| 102 | +} |
| 103 | + |
| 104 | +// --- zap core wrapper --- |
| 105 | + |
| 106 | +// WrapCoreWithLogMetrics returns a zapcore.Core that hooks into every write to |
| 107 | +// increment LogMessagesTotal for warn and error entries. It uses |
| 108 | +// zapcore.RegisterHooks so no manual Check/Write plumbing is needed. |
| 109 | +func WrapCoreWithLogMetrics(core zapcore.Core) zapcore.Core { |
| 110 | + return zapcore.RegisterHooks(core, func(e zapcore.Entry) error { |
| 111 | + if e.Level >= zapcore.WarnLevel { |
| 112 | + level := "warn" |
| 113 | + if e.Level >= zapcore.ErrorLevel { |
| 114 | + level = "error" |
| 115 | + } |
| 116 | + file := "unknown" |
| 117 | + if e.Caller.Defined { |
| 118 | + file = shortFilePath(e.Caller.File) |
| 119 | + } |
| 120 | + LogMessagesTotal.WithLabelValues(level, file).Inc() |
| 121 | + } |
| 122 | + return nil |
| 123 | + }) |
| 124 | +} |
0 commit comments