github.com/ethersphere/bee/v2@v2.2.0/pkg/log/metrics.go (about)

     1  // Copyright 2022 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package log
     6  
     7  import (
     8  	m "github.com/ethersphere/bee/v2/pkg/metrics"
     9  	"github.com/prometheus/client_golang/prometheus"
    10  )
    11  
    12  // metrics groups various metrics counters for statistical reasons.
    13  type metrics struct {
    14  	ErrorCount prometheus.Counter
    15  	WarnCount  prometheus.Counter
    16  	InfoCount  prometheus.Counter
    17  	DebugCount prometheus.Counter
    18  	TraceCount prometheus.Counter
    19  }
    20  
    21  // Fire implements Hook interface.
    22  func (m metrics) Fire(v Level) error {
    23  	switch v {
    24  	case VerbosityError:
    25  		m.ErrorCount.Inc()
    26  	case VerbosityWarning:
    27  		m.WarnCount.Inc()
    28  	case VerbosityInfo:
    29  		m.InfoCount.Inc()
    30  	case VerbosityDebug:
    31  		m.DebugCount.Inc()
    32  	default:
    33  		m.TraceCount.Inc()
    34  	}
    35  	return nil
    36  }
    37  
    38  // newLogMetrics returns pointer to a new metrics instance ready to use.
    39  func newLogMetrics() *metrics {
    40  	const subsystem = "log"
    41  
    42  	return &metrics{
    43  		ErrorCount: prometheus.NewCounter(prometheus.CounterOpts{
    44  			Namespace: m.Namespace,
    45  			Subsystem: subsystem,
    46  			Name:      "error_count",
    47  			Help:      "Number ERROR log messages.",
    48  		}),
    49  		WarnCount: prometheus.NewCounter(prometheus.CounterOpts{
    50  			Namespace: m.Namespace,
    51  			Subsystem: subsystem,
    52  			Name:      "warn_count",
    53  			Help:      "Number WARN log messages.",
    54  		}),
    55  		InfoCount: prometheus.NewCounter(prometheus.CounterOpts{
    56  			Namespace: m.Namespace,
    57  			Subsystem: subsystem,
    58  			Name:      "info_count",
    59  			Help:      "Number INFO log messages.",
    60  		}),
    61  		DebugCount: prometheus.NewCounter(prometheus.CounterOpts{
    62  			Namespace: m.Namespace,
    63  			Subsystem: subsystem,
    64  			Name:      "debug_count",
    65  			Help:      "Number DEBUG log messages.",
    66  		}),
    67  		TraceCount: prometheus.NewCounter(prometheus.CounterOpts{
    68  			Namespace: m.Namespace,
    69  			Subsystem: subsystem,
    70  			Name:      "trace_count",
    71  			Help:      "Number TRACE log messages.",
    72  		}),
    73  	}
    74  }