-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (51 loc) · 1.64 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package logmetrics
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)
// Metrics is a logrus hook-compliant struct that records metrics about logging
// when added to a logrus.Logger
type Metrics map[logrus.Level]prometheus.Counter
// New creates a new hook for recording metrics.
// New takes a namespace parameter which defines the namespace
// for the prometheus metrics.
func New(namespace string) Metrics {
return Metrics{
logrus.DebugLevel: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace, Subsystem: "log", Name: "debug_total",
}),
logrus.InfoLevel: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace, Subsystem: "log", Name: "info_total",
}),
logrus.WarnLevel: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace, Subsystem: "log", Name: "warn_total",
}),
logrus.ErrorLevel: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace, Subsystem: "log", Name: "error_total",
}),
logrus.PanicLevel: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace, Subsystem: "log", Name: "panic_total",
}),
}
}
// Fire is triggered by logrus, in response to a logging event
func (m Metrics) Fire(e *logrus.Entry) error {
counter, ok := m[e.Level]
if ok {
counter.Inc()
return nil
}
return fmt.Errorf("level %v not supported", e.Level)
}
// Levels returns the logging levels that will trigger this hook to run. In
// this case, all of them.
func (m Metrics) Levels() []logrus.Level {
return []logrus.Level{
logrus.DebugLevel,
logrus.InfoLevel,
logrus.WarnLevel,
logrus.ErrorLevel,
logrus.PanicLevel,
}
}