prysm-pulse/shared/prometheus/logrus_collector.go
Leandro Lugaresi d061c784e0 Added Prometheus Client and p2p Metrics (#673)
* Added prometheus client and p2p metrics

* Avoid run the adapter if the metrics are disabled

* fix visibility issue

* Fix invalid p2p.Message sent to Adapters

The middlewares (adapters) must receive the complete message to avoid
problems and the main Handler must get the values from middlewares

Also, added tests and comments for metrics package

* Added logrus hook collector

This collector is used to collect counters of log messages.
The main purpose of these metric is to know how many warnings and errors
the system are getting.

* Add hook when register the prometheus service

* update bazel builds

* fix emit tests and remove unused imports

* gazelle --fix

* remove unused logger

* move prometheus package to shared directory

* better metric names and fix metric paths

* improve metric tests and start to use promauto

* added prometheus initial documentation

* fix tests

* fix type differences with go get and bazel

* Fix service test
2018-11-15 07:54:45 -05:00

47 lines
1.3 KiB
Go

package prometheus
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/sirupsen/logrus"
)
// LogrusCollector is a logrus hook to collect log counters.
type LogrusCollector struct {
counterVec *prometheus.CounterVec
}
var (
supportedLevels = []logrus.Level{logrus.InfoLevel, logrus.WarnLevel, logrus.ErrorLevel}
counterVec = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "log_entries_total",
Help: "Total number of log messages.",
}, []string{"level", "prefix"})
)
const prefixKey = "prefix"
const defaultprefix = "global"
// NewLogrusCollector register internal metrics and return an logrus hook to collect log counters
// This function can be called only once, if more than one call is made an error will be returned.
func NewLogrusCollector() *LogrusCollector {
return &LogrusCollector{
counterVec: counterVec,
}
}
// Fire is called on every log call.
func (hook *LogrusCollector) Fire(entry *logrus.Entry) error {
prefix := defaultprefix
if prefixValue, ok := entry.Data[prefixKey]; ok {
prefix = prefixValue.(string)
}
hook.counterVec.WithLabelValues(entry.Level.String(), prefix).Inc()
return nil
}
// Levels return a slice of levels supported by this hook;
func (hook *LogrusCollector) Levels() []logrus.Level {
return supportedLevels
}