[#680] metrics: Initialize log metrics together with services

Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
This commit is contained in:
Anton Nikiforov 2023-10-08 12:32:00 +03:00
parent bf082348d4
commit 3a997d1207
8 changed files with 58 additions and 38 deletions

View file

@ -14,12 +14,17 @@ const (
logDroppedLabel = "dropped"
)
type LogMetrics struct {
type LogMetrics interface {
Inc(level zapcore.Level, dropped bool)
GetSamplingHook() func(e zapcore.Entry, sd zapcore.SamplingDecision)
}
type logMetrics struct {
logCount *prometheus.CounterVec
}
func NewLogMetrics(namespace string) *LogMetrics {
return &LogMetrics{
func NewLogMetrics(namespace string) LogMetrics {
return &logMetrics{
logCount: metrics.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: logSubsystem,
@ -29,9 +34,15 @@ func NewLogMetrics(namespace string) *LogMetrics {
}
}
func (m *LogMetrics) Inc(level zapcore.Level, dropped bool) {
func (m *logMetrics) Inc(level zapcore.Level, dropped bool) {
m.logCount.With(prometheus.Labels{
logLevelLabel: level.String(),
logDroppedLabel: strconv.FormatBool(dropped),
}).Inc()
}
func (m *logMetrics) GetSamplingHook() func(zapcore.Entry, zapcore.SamplingDecision) {
return func(e zapcore.Entry, sd zapcore.SamplingDecision) {
m.Inc(e.Level, sd == zapcore.LogDropped)
}
}