-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
46 lines (38 loc) · 1.25 KB
/
worker.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
package prometheus_utils
import "github.com/prometheus/client_golang/prometheus"
const (
labelCalled = "called"
labelFailed = "failed"
labelSucceed = "succeed"
)
// workerMetrics stores any worker metrics: number of worker being called and failed
// succeed can be derived as: called - failed or counted with its own label
type workerMetrics struct {
*prometheus.CounterVec
}
// NewWorkerMetrics creates a new workerMetrics with the given app and worker name
// The only label is 'status' i.e. called/failed
func NewWorkerMetrics(appName, workerName string) *workerMetrics {
c := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "worker_metric",
Help: "Number of times worker has the specified in label status",
ConstLabels: prometheus.Labels{"app": appName, "worker": workerName},
},
[]string{"status"},
)
prometheus.MustRegister(c)
return &workerMetrics{c}
}
// Called increments the called counter by 1
func (m *workerMetrics) Called() {
m.WithLabelValues(labelCalled).Inc()
}
// Failed increments the failed counter by 1
func (m *workerMetrics) Failed() {
m.WithLabelValues(labelFailed).Inc()
}
// Succeed increments the succeed counter by 1
func (m *workerMetrics) Succeed() {
m.WithLabelValues(labelSucceed).Inc()
}