2023-06-09 08:47:39 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/metrics"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-06-13 12:27:38 +00:00
|
|
|
logSubsystem = "logger"
|
2023-06-09 08:47:39 +00:00
|
|
|
logLevelLabel = "level"
|
|
|
|
logDroppedLabel = "dropped"
|
|
|
|
)
|
|
|
|
|
2023-10-08 09:32:00 +00:00
|
|
|
type LogMetrics interface {
|
|
|
|
Inc(level zapcore.Level, dropped bool)
|
|
|
|
GetSamplingHook() func(e zapcore.Entry, sd zapcore.SamplingDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
type logMetrics struct {
|
2023-06-09 08:47:39 +00:00
|
|
|
logCount *prometheus.CounterVec
|
|
|
|
}
|
|
|
|
|
2023-10-08 09:32:00 +00:00
|
|
|
func NewLogMetrics(namespace string) LogMetrics {
|
|
|
|
return &logMetrics{
|
2023-06-09 08:47:39 +00:00
|
|
|
logCount: metrics.NewCounterVec(prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: logSubsystem,
|
2023-06-13 12:27:38 +00:00
|
|
|
Name: "entry_count",
|
2023-06-09 08:47:39 +00:00
|
|
|
Help: "Total log entries emitted or dropped by severity level",
|
|
|
|
}, []string{logLevelLabel, logDroppedLabel}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-08 09:32:00 +00:00
|
|
|
func (m *logMetrics) Inc(level zapcore.Level, dropped bool) {
|
2023-06-09 08:47:39 +00:00
|
|
|
m.logCount.With(prometheus.Labels{
|
|
|
|
logLevelLabel: level.String(),
|
|
|
|
logDroppedLabel: strconv.FormatBool(dropped),
|
|
|
|
}).Inc()
|
|
|
|
}
|
2023-10-08 09:32:00 +00:00
|
|
|
|
|
|
|
func (m *logMetrics) GetSamplingHook() func(zapcore.Entry, zapcore.SamplingDecision) {
|
|
|
|
return func(e zapcore.Entry, sd zapcore.SamplingDecision) {
|
|
|
|
m.Inc(e.Level, sd == zapcore.LogDropped)
|
|
|
|
}
|
|
|
|
}
|