This repository has been archived by the owner on Aug 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
collector.go
126 lines (103 loc) · 2.89 KB
/
collector.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
package main
import (
"encoding/json"
"log"
"net/http"
"net/url"
"time"
"github.com/prometheus/client_golang/prometheus"
)
type natsMetrics struct {
Connections float64 `json:"connections"`
Routes float64 `json:"routes"`
MessagesIn float64 `json:"in_msgs"`
MessagesOut float64 `json:"out_msgs"`
BytesIn float64 `json:"in_bytes"`
BytesOut float64 `json:"out_bytes"`
SlowConsumers float64 `json:"slow_consumers"`
}
var (
connections = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "gnatsd",
Name: "connections",
Help: "Active connections to gnatsd",
})
routes = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "gnatsd",
Name: "routes",
Help: "Active routes to gnatsd",
})
messageCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "gnatsd",
Name: "msg_total",
Help: "Count of transferred messages",
},
[]string{"direction"},
)
bytesCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "gnatsd",
Name: "bytes_total",
Help: "Amount of transmitted data",
},
[]string{"direction"},
)
slowConsumers = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "gnatsd",
Name: "slow_consumers",
Help: "Number of slow consumers",
})
)
// Exporter implements the prometheus.Collector interface. It exposes the metrics
// of a NATS node.
type Exporter struct {
NatsURL string
}
// NewExporter instantiates a new NATS Exporter.
func NewExporter(natsURL *url.URL) *Exporter {
natsURL.Path = "/varz"
return &Exporter{
NatsURL: natsURL.String(),
}
}
// Describe describes all the registered stats metrics from the NATS node.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
connections.Describe(ch)
routes.Describe(ch)
messageCounter.Describe(ch)
bytesCounter.Describe(ch)
slowConsumers.Describe(ch)
}
// Collect collects all the registered stats metrics from the NATS node.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
if err := e.collect(); err != nil {
return
}
connections.Collect(ch)
routes.Collect(ch)
messageCounter.Collect(ch)
bytesCounter.Collect(ch)
slowConsumers.Collect(ch)
}
func (e *Exporter) collect() error {
var metrics natsMetrics
httpClient := http.DefaultClient
httpClient.Timeout = 1 * time.Second
resp, err := httpClient.Get(e.NatsURL)
if err != nil {
log.Printf("could not retrieve NATS metrics: %v", err)
return err
}
err = json.NewDecoder(resp.Body).Decode(&metrics)
if err != nil {
log.Printf("could not decode NATS metrics: %v", err)
return err
}
connections.Set(metrics.Connections)
routes.Set(metrics.Routes)
messageCounter.WithLabelValues("in").Set(metrics.MessagesIn)
messageCounter.WithLabelValues("out").Set(metrics.MessagesOut)
bytesCounter.WithLabelValues("in").Set(metrics.BytesIn)
bytesCounter.WithLabelValues("out").Set(metrics.BytesOut)
slowConsumers.Set(metrics.SlowConsumers)
return nil
}