forked from sapcc/mosquitto-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmosquitto_counter.go
51 lines (42 loc) · 1015 Bytes
/
mosquitto_counter.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
package main
import (
"errors"
"github.com/prometheus/client_golang/prometheus"
)
// MosquittoCounter exports all counter metrics are already added by mosquitto
type MosquittoCounter struct {
Desc *prometheus.Desc
counter
// ... many more fields
}
// NewMosquittoCounter get a new one
func NewMosquittoCounter(desc *prometheus.Desc) *MosquittoCounter {
return &MosquittoCounter{
Desc: desc,
}
}
// Set sets the value
func (c *MosquittoCounter) Set(v float64) {
c.counter.Set(v)
}
// Describe simply sends the two Descs in the struct to the channel.
func (c *MosquittoCounter) Describe(ch chan<- *prometheus.Desc) {
ch <- c.Desc
}
// Collect already added counter values
func (c *MosquittoCounter) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(
c.Desc,
prometheus.CounterValue,
c.counter.value,
)
}
type counter struct {
value float64
}
func (c *counter) Set(v float64) {
if v < 0 {
panic(errors.New("counter cannot decrease in value"))
}
c.value = v
}