-
Notifications
You must be signed in to change notification settings - Fork 6
/
prometheus.go
149 lines (137 loc) · 3.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"time"
"github.com/prometheus/client_golang/prometheus"
)
const (
NameSpace = "pepperbus"
)
var (
globalRegistry = prometheus.NewRegistry()
topicLen = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: NameSpace,
Name: "topic_length",
Help: "topic length",
},
[]string{"queue", "topic"},
)
topicRetryLen = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: NameSpace,
Name: "topic_retry_length",
Help: "topic retry length",
},
[]string{"queue", "topic"},
)
topicTimeoutLen = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: NameSpace,
Name: "topic_timeout_length",
Help: "topic timeout length",
},
[]string{"queue", "topic"},
)
cpuUsage = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: NameSpace,
Name: "cpu_usage",
Help: "cpu usage",
},
)
memUsage = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: NameSpace,
Name: "mem_usage",
Help: "memory usage",
},
)
loadAverage = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: NameSpace,
Name: "load_average",
Help: "machine load average",
},
)
queueAddCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: NameSpace,
Name: "queue_add_count",
Help: "queue add operation count",
},
[]string{"queue"},
)
jobSuccessCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: NameSpace,
Name: "job_success_count",
Help: "succeed job count",
},
[]string{"queue", "topic"},
)
jobFailCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: NameSpace,
Name: "job_fail_count",
Help: "failed jobs count",
},
[]string{"queue", "topic"},
)
jobConsumes = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: NameSpace,
Name: "job_consume_milliseconds",
Help: "Cgi job consume milliseconds",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
},
[]string{"queue", "topic"},
)
)
func init() {
globalRegistry.MustRegister(topicLen)
globalRegistry.MustRegister(topicRetryLen)
prometheus.MustRegister(cpuUsage)
prometheus.MustRegister(memUsage)
prometheus.MustRegister(loadAverage)
prometheus.MustRegister(queueAddCount)
prometheus.MustRegister(jobSuccessCount)
prometheus.MustRegister(jobFailCount)
prometheus.MustRegister(jobConsumes)
}
func updateTopicLength() {
for qn, queue := range QueueConfs() {
for tn, _ := range queue.Topic {
mux, err := MuxManager.GetMuxer(qn, tn)
if err != nil {
continue
}
normal, retry, timeout, err := mux.Storage().GetLength(qn, tn)
if err != nil {
continue
}
topicLen.WithLabelValues(string(qn), string(tn)).Set(float64(normal))
topicRetryLen.WithLabelValues(string(qn), string(tn)).Set(float64(retry))
topicTimeoutLen.WithLabelValues(string(qn), string(tn)).Set(float64(timeout))
}
}
}
func updateCurrentStats() {
MachineStat.RLock()
cpuUsage.Set(MachineStat.CPU)
memUsage.Set(MachineStat.Memory)
loadAverage.Set(MachineStat.Load)
MachineStat.RUnlock()
}
func addQueueStats(queue string, success bool) {
if success {
queueAddCount.WithLabelValues(queue).Inc()
}
}
func addJobStats(queue, topic string, duration time.Duration, success bool) {
if success {
jobConsumes.WithLabelValues(queue, topic).Observe(float64(duration.Nanoseconds() / 1e6))
jobSuccessCount.WithLabelValues(queue, topic).Inc()
} else {
jobFailCount.WithLabelValues(queue, topic).Inc()
}
}