From 0c40d98f7a7ad6f39755c4c0a7e298e419d2aafd Mon Sep 17 00:00:00 2001 From: Alejandro Lopez Date: Fri, 9 Jun 2023 11:47:39 +0300 Subject: [PATCH] [#375] Add log metrics Signed-off-by: Alejandro Lopez --- cmd/frostfs-ir/main.go | 1 + cmd/frostfs-node/config.go | 2 ++ pkg/util/logger/logger.go | 8 ++++++++ pkg/util/logger/metrics.go | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 pkg/util/logger/metrics.go diff --git a/cmd/frostfs-ir/main.go b/cmd/frostfs-ir/main.go index 1718a46ab..81fcaa489 100644 --- a/cmd/frostfs-ir/main.go +++ b/cmd/frostfs-ir/main.go @@ -61,6 +61,7 @@ func main() { cfg, err = newConfig() exitErr(err) + logPrm.MetricsNamespace = "frostfs-ir" err = logPrm.SetLevelString( cfg.GetString("logger.level"), ) diff --git a/cmd/frostfs-node/config.go b/cmd/frostfs-node/config.go index f8605c21e..e4547364d 100644 --- a/cmd/frostfs-node/config.go +++ b/cmd/frostfs-node/config.go @@ -542,6 +542,8 @@ func initCfg(appCfg *config.Config) *cfg { logPrm, err := c.loggerPrm() fatalOnErr(err) + logPrm.MetricsNamespace = "frostfs-node" + log, err := logger.NewLogger(logPrm) fatalOnErr(err) diff --git a/pkg/util/logger/logger.go b/pkg/util/logger/logger.go index 4a536368a..fcac09321 100644 --- a/pkg/util/logger/logger.go +++ b/pkg/util/logger/logger.go @@ -31,6 +31,9 @@ type Prm struct { // support runtime rereading level zapcore.Level + // MetricsNamespace is the namespace string used for log counter metrics + MetricsNamespace string + // do not support runtime rereading } @@ -79,10 +82,15 @@ func NewLogger(prm *Prm) (*Logger, error) { lvl := zap.NewAtomicLevelAt(prm.level) + m := newLogMetrics(prm.MetricsNamespace) + c := zap.NewProductionConfig() c.Level = lvl c.Encoding = "console" c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder + c.Sampling.Hook = func(e zapcore.Entry, sd zapcore.SamplingDecision) { + m.Inc(e.Level, sd == zapcore.LogDropped) + } lZap, err := c.Build( zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)), diff --git a/pkg/util/logger/metrics.go b/pkg/util/logger/metrics.go new file mode 100644 index 000000000..10be4a8f0 --- /dev/null +++ b/pkg/util/logger/metrics.go @@ -0,0 +1,37 @@ +package logger + +import ( + "strconv" + + "git.frostfs.info/TrueCloudLab/frostfs-observability/metrics" + "github.com/prometheus/client_golang/prometheus" + "go.uber.org/zap/zapcore" +) + +const ( + logSubsystem = "log" + logLevelLabel = "level" + logDroppedLabel = "dropped" +) + +type logMetrics struct { + logCount *prometheus.CounterVec +} + +func newLogMetrics(namespace string) *logMetrics { + return &logMetrics{ + logCount: metrics.NewCounterVec(prometheus.CounterOpts{ + Namespace: namespace, + Subsystem: logSubsystem, + Name: "log_count", + Help: "Total log entries emitted or dropped by severity level", + }, []string{logLevelLabel, logDroppedLabel}), + } +} + +func (m *logMetrics) Inc(level zapcore.Level, dropped bool) { + m.logCount.With(prometheus.Labels{ + logLevelLabel: level.String(), + logDroppedLabel: strconv.FormatBool(dropped), + }).Inc() +}