-
Notifications
You must be signed in to change notification settings - Fork 0
/
example2.go
53 lines (42 loc) · 1.07 KB
/
example2.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
package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net/http"
)
type ChanCollector struct{
JobChannel chan int
Name string
}
func NewChanCollector(jobChannel chan int, name string) *ChanCollector{
c := ChanCollector{}
c.JobChannel = jobChannel
c.Name = name
return &c
}
func (c *ChanCollector) Describe(ch chan<- *prometheus.Desc) {
desc := prometheus.NewDesc(c.Name, "length of channel", nil, nil)
ch <- desc
}
func (c *ChanCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(c.Name, "length of channel", nil, nil),
prometheus.GaugeValue,
float64(len(c.JobChannel)),
)
}
var jobChannel chan int
func init() {
jobChannel = make(chan int, 10)
}
func main() {
for i := 0; i < 4; i++ {
jobChannel <- i
}
collector := NewChanCollector(jobChannel, "job_channel_length")
prometheus.MustRegister(collector)
http.Handle("/metrics", promhttp.Handler())
log.Println("starting...")
log.Fatal(http.ListenAndServe(":9090", nil))
}