/
prometheus.go
65 lines (57 loc) · 1.51 KB
/
prometheus.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
// Copyright (c) 2022 Janoš Guljaš <janos@resenje.org>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package logging
import (
"log/slog"
"github.com/prometheus/client_golang/prometheus"
)
// Metrics holds Prometheus counters.
type Metrics struct {
vector *prometheus.CounterVec
}
// MetricsOptions holds options for NewCounter constructor.
type MetricsOptions struct {
Namespace string
Subsystem string
Name string
Help string
}
// NewMetrics creates new Counter instance. Options value can be nil.
func NewMetrics(options *MetricsOptions) (c *Metrics) {
if options == nil {
options = new(MetricsOptions)
}
if options.Subsystem == "" {
options.Subsystem = "logger"
}
if options.Name == "" {
options.Name = "messages_total"
}
if options.Help == "" {
options.Help = "Number of log messages processed, partitioned by log level."
}
vector := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: options.Namespace,
Subsystem: options.Subsystem,
Name: options.Name,
Help: options.Help,
},
[]string{"logger", "level"},
)
return &Metrics{
vector: vector,
}
}
func (c *Metrics) Inc(loggerName string, level slog.Level) {
c.vector.With(prometheus.Labels{
"logger": loggerName,
"level": level.String(),
}).Inc()
}
// Metrics returns all Prometheus metrics that should be registered.
func (c *Metrics) Metrics() (cs []prometheus.Collector) {
return []prometheus.Collector{c.vector}
}