Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/common-go/log/metrics.go
2498 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package log
6
7
import (
8
"github.com/prometheus/client_golang/prometheus"
9
"github.com/sirupsen/logrus"
10
)
11
12
var (
13
DefaultMetrics = NewMetrics()
14
)
15
16
type Metrics struct {
17
logEmitedCounter *prometheus.CounterVec
18
}
19
20
func (m *Metrics) ReportLog(level logrus.Level) {
21
m.logEmitedCounter.WithLabelValues(level.String()).Inc()
22
}
23
24
func NewMetrics() *Metrics {
25
return &Metrics{
26
logEmitedCounter: prometheus.NewCounterVec(prometheus.CounterOpts{
27
Name: "gitpod_logs_total",
28
Help: "Total number of logs produced by level",
29
}, []string{"level"}),
30
}
31
}
32
33
// Describe sends the super-set of all possible descriptors of metrics
34
// collected by this Collector to the provided channel and returns once
35
// the last descriptor has been sent.
36
func (m *Metrics) Describe(ch chan<- *prometheus.Desc) {
37
m.logEmitedCounter.Describe(ch)
38
}
39
40
// Collect is called by the Prometheus registry when collecting
41
// metrics. The implementation sends each collected metric via the
42
// provided channel and returns once the last metric has been sent.
43
func (m *Metrics) Collect(ch chan<- prometheus.Metric) {
44
m.logEmitedCounter.Collect(ch)
45
}
46
47
func NewLogHook(metrics *Metrics) *LogHook {
48
return &LogHook{metrics: metrics}
49
}
50
51
type LogHook struct {
52
metrics *Metrics
53
}
54
55
func (h *LogHook) Levels() []logrus.Level {
56
return logrus.AllLevels
57
}
58
59
func (h *LogHook) Fire(entry *logrus.Entry) error {
60
h.metrics.ReportLog(entry.Level)
61
return nil
62
}
63
64