-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
88 lines (82 loc) · 2.19 KB
/
metrics.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package log
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)
type metrics struct {
// all metrics fields must be exported
// to be able to return them by Metrics()
// using reflection
ErrorCount prometheus.Counter
WarnCount prometheus.Counter
InfoCount prometheus.Counter
DebugCount prometheus.Counter
TraceCount prometheus.Counter
}
//func newMetrics() metrics {
// subsystem := "log"
//
// return metrics{
// ErrorCount: prometheus.NewCounter(prometheus.CounterOpts{
// Namespace: m.Namespace,
// Subsystem: subsystem,
// Name: "error_count",
// Help: "Number ERROR log messages.",
// }),
// WarnCount: prometheus.NewCounter(prometheus.CounterOpts{
// Namespace: m.Namespace,
// Subsystem: subsystem,
// Name: "warn_count",
// Help: "Number WARN log messages.",
// }),
// InfoCount: prometheus.NewCounter(prometheus.CounterOpts{
// Namespace: m.Namespace,
// Subsystem: subsystem,
// Name: "info_count",
// Help: "Number INFO log messages.",
// }),
// DebugCount: prometheus.NewCounter(prometheus.CounterOpts{
// Namespace: m.Namespace,
// Subsystem: subsystem,
// Name: "debug_count",
// Help: "Number DEBUG log messages.",
// }),
// TraceCount: prometheus.NewCounter(prometheus.CounterOpts{
// Namespace: m.Namespace,
// Subsystem: subsystem,
// Name: "trace_count",
// Help: "Number TRACE log messages.",
// }),
// }
//}
//
//func (l *logger) Metrics() []prometheus.Collector {
// return m.PrometheusCollectorsFromFields(l.metrics)
//}
func (m metrics) Levels() []logrus.Level {
return []logrus.Level{
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
logrus.TraceLevel,
}
}
func (m metrics) Fire(e *logrus.Entry) error {
switch e.Level {
case logrus.ErrorLevel:
m.ErrorCount.Inc()
case logrus.WarnLevel:
m.WarnCount.Inc()
case logrus.InfoLevel:
m.InfoCount.Inc()
case logrus.DebugLevel:
m.DebugCount.Inc()
case logrus.TraceLevel:
m.TraceCount.Inc()
}
return nil
}