-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcollector.go
243 lines (205 loc) · 9.64 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"encoding/json"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"log"
"net/http"
)
type clusterMetricsResponse struct {
ClusterMetrics clusterMetrics `json:"clusterMetrics"`
}
type clusterMetrics struct {
AppsSubmitted int `json:"appsSubmitted"`
AppsCompleted int `json:"appsCompleted"`
AppsPending int `json:"appsPending"`
AppsRunning int `json:"appsRunning"`
AppsFailed int `json:"appsFailed"`
AppsKilled int `json:"appsKilled"`
ReservedMB int `json:"reservedMB"`
AvailableMB int `json:"availableMB"`
AllocatedMB int `json:"allocatedMB"`
ReservedVirtualCores int `json:"reservedVirtualCores"`
AvailableVirtualCores int `json:"availableVirtualCores"`
AllocatedVirtualCores int `json:"allocatedVirtualCores"`
ContainersAllocated int `json:"containersAllocated"`
ContainersReserved int `json:"containersReserved"`
ContainersPending int `json:"containersPending"`
TotalMB int `json:"totalMB"`
TotalVirtualCores int `json:"totalVirtualCores"`
TotalNodes int `json:"totalNodes"`
LostNodes int `json:"lostNodes"`
UnhealthyNodes int `json:"unhealthyNodes"`
DecommissioningNodes int `json:"decommissioningNodes"`
DecommissionedNodes int `json:"decommissionedNodes"`
RebootedNodes int `json:"rebootedNodes"`
ActiveNodes int `json:"activeNodes"`
ShutdownNodes int `json:"shutdownNodes"`
}
type schedulerResponse struct {
Scheduler scheduler `json:"scheduler"`
}
type scheduler struct {
SchedulerInfo schedulerInfo `json:"schedulerInfo"`
}
type schedulerInfo struct {
Queues schedulerQueues `json:"queues"`
}
type schedulerQueues struct {
Queue []schedulerQueue `json:"queue"`
}
type schedulerQueue struct {
Capacity float64 `json:"capacity"`
UsedCapacity float64 `json:"usedCapacity"`
MaxCapacity float64 `json:"maxCapacity"`
NumApplications int `json:"numApplications"`
QueueName string `json:"queueName"`
State string `json:"state"`
ResourcesUsed SchedulerQueueResourcesUsed `json:"resourcesUsed"`
}
type SchedulerQueueResourcesUsed struct {
Memory int `json:"memory,omitempty"`
VCores int `json:"vCores,omitempty"`
}
type collector struct {
endpoint string
up *prometheus.Desc
scrapeFailures *prometheus.Desc
failureCount int
applications *prometheus.Desc
memory *prometheus.Desc
cores *prometheus.Desc
containers *prometheus.Desc
nodes *prometheus.Desc
queueCapacity *prometheus.Desc
queueUsedCapacity *prometheus.Desc
queueMaxCapacity *prometheus.Desc
queueApplicationCount *prometheus.Desc
queueUsedMemoryBytes *prometheus.Desc
queueUsedVCoresCount *prometheus.Desc
}
const metricsNamespace = "yarn"
func newFuncMetric(metricName string, docString string, labels []string) *prometheus.Desc {
return prometheus.NewDesc(prometheus.BuildFQName(metricsNamespace, "", metricName), docString, labels, nil)
}
func newCollector(endpoint string) *collector {
return &collector{
endpoint: endpoint,
up: newFuncMetric("up", "Able to contact YARN", nil),
scrapeFailures: newFuncMetric("scrape_failures_total", "Number of errors while scraping YARN metrics", nil),
applications: newFuncMetric("applications_total", "Applications stats", []string{"status"}),
memory: newFuncMetric("memory_bytes", "Memory allocation stats", []string{"status"}),
cores: newFuncMetric("cores_total", "Cpu allocation stats", []string{"status"}),
containers: newFuncMetric("containers_total", "Container stats", []string{"status"}),
nodes: newFuncMetric("nodes_total", "Node stats", []string{"status"}),
queueCapacity: newFuncMetric("queue_capacity", "Queue capacity", []string{"queue"}),
queueUsedCapacity: newFuncMetric("queue_used_capacity", "Queue used capacity", []string{"queue"}),
queueMaxCapacity: newFuncMetric("queue_max_capacity", "Queue max capacity", []string{"queue"}),
queueApplicationCount: newFuncMetric("queue_application_count", "Queue application count", []string{"queue"}),
queueUsedMemoryBytes: newFuncMetric("queue_used_memory_bytes", "Queue used memory bytes", []string{"queue"}),
queueUsedVCoresCount: newFuncMetric("queue_used_vcores_count", "Queue used vcores count", []string{"queue"}),
}
}
func (c *collector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.up
ch <- c.scrapeFailures
ch <- c.applications
ch <- c.memory
ch <- c.cores
ch <- c.containers
ch <- c.nodes
ch <- c.queueCapacity
ch <- c.queueUsedCapacity
ch <- c.queueMaxCapacity
ch <- c.queueApplicationCount
ch <- c.queueUsedMemoryBytes
ch <- c.queueUsedVCoresCount
}
func (c *collector) Collect(ch chan<- prometheus.Metric) {
up := 1.0
m, err := c.fetchClusterMetrics()
if err != nil {
up = 0.0
c.failureCount++
log.Println("Error while collecting data from YARN: " + err.Error())
}
s, err := c.fetchSchedulerMetrics()
if err != nil {
up = 0.0
c.failureCount++
log.Println("Error while collecting data from YARN: " + err.Error())
}
ch <- prometheus.MustNewConstMetric(c.up, prometheus.GaugeValue, up)
ch <- prometheus.MustNewConstMetric(c.scrapeFailures, prometheus.CounterValue, float64(c.failureCount))
if up == 0.0 {
return
}
ch <- prometheus.MustNewConstMetric(c.applications, prometheus.GaugeValue, float64(m.AppsSubmitted), "submitted")
ch <- prometheus.MustNewConstMetric(c.applications, prometheus.GaugeValue, float64(m.AppsCompleted), "completed")
ch <- prometheus.MustNewConstMetric(c.applications, prometheus.GaugeValue, float64(m.AppsPending), "pending")
ch <- prometheus.MustNewConstMetric(c.applications, prometheus.GaugeValue, float64(m.AppsRunning), "running")
ch <- prometheus.MustNewConstMetric(c.applications, prometheus.GaugeValue, float64(m.AppsFailed), "failed")
ch <- prometheus.MustNewConstMetric(c.applications, prometheus.GaugeValue, float64(m.AppsKilled), "killed")
ch <- prometheus.MustNewConstMetric(c.memory, prometheus.GaugeValue, float64(m.ReservedMB)*1024*1024, "submitted")
ch <- prometheus.MustNewConstMetric(c.memory, prometheus.GaugeValue, float64(m.AvailableMB)*1024*1024, "available")
ch <- prometheus.MustNewConstMetric(c.memory, prometheus.GaugeValue, float64(m.AllocatedMB)*1024*1024, "allocated")
ch <- prometheus.MustNewConstMetric(c.memory, prometheus.GaugeValue, float64(m.TotalMB)*1024*1024, "total")
ch <- prometheus.MustNewConstMetric(c.cores, prometheus.GaugeValue, float64(m.ReservedVirtualCores), "reserved")
ch <- prometheus.MustNewConstMetric(c.cores, prometheus.GaugeValue, float64(m.AvailableVirtualCores), "available")
ch <- prometheus.MustNewConstMetric(c.cores, prometheus.GaugeValue, float64(m.AllocatedVirtualCores), "allocated")
ch <- prometheus.MustNewConstMetric(c.cores, prometheus.GaugeValue, float64(m.TotalVirtualCores), "total")
ch <- prometheus.MustNewConstMetric(c.containers, prometheus.GaugeValue, float64(m.ContainersAllocated), "allocated")
ch <- prometheus.MustNewConstMetric(c.containers, prometheus.GaugeValue, float64(m.ContainersReserved), "reserved")
ch <- prometheus.MustNewConstMetric(c.containers, prometheus.GaugeValue, float64(m.ContainersPending), "pending")
ch <- prometheus.MustNewConstMetric(c.nodes, prometheus.GaugeValue, float64(m.TotalNodes), "total")
ch <- prometheus.MustNewConstMetric(c.nodes, prometheus.GaugeValue, float64(m.LostNodes), "lost")
ch <- prometheus.MustNewConstMetric(c.nodes, prometheus.GaugeValue, float64(m.UnhealthyNodes), "unhealthy")
ch <- prometheus.MustNewConstMetric(c.nodes, prometheus.GaugeValue, float64(m.DecommissionedNodes), "decommissioned")
ch <- prometheus.MustNewConstMetric(c.nodes, prometheus.GaugeValue, float64(m.DecommissioningNodes), "decommissioning")
ch <- prometheus.MustNewConstMetric(c.nodes, prometheus.GaugeValue, float64(m.RebootedNodes), "rebooted")
ch <- prometheus.MustNewConstMetric(c.nodes, prometheus.GaugeValue, float64(m.ActiveNodes), "active")
ch <- prometheus.MustNewConstMetric(c.nodes, prometheus.GaugeValue, float64(m.ShutdownNodes), "shutdown")
for _, q := range s.SchedulerInfo.Queues.Queue {
ch <- prometheus.MustNewConstMetric(c.queueCapacity, prometheus.GaugeValue, q.Capacity, q.QueueName)
ch <- prometheus.MustNewConstMetric(c.queueUsedCapacity, prometheus.GaugeValue, q.UsedCapacity, q.QueueName)
ch <- prometheus.MustNewConstMetric(c.queueMaxCapacity, prometheus.GaugeValue, q.MaxCapacity, q.QueueName)
ch <- prometheus.MustNewConstMetric(c.queueApplicationCount, prometheus.GaugeValue, float64(q.NumApplications), q.QueueName)
ch <- prometheus.MustNewConstMetric(c.queueUsedMemoryBytes, prometheus.GaugeValue, float64(q.ResourcesUsed.Memory)*1024*1024, q.QueueName)
ch <- prometheus.MustNewConstMetric(c.queueUsedVCoresCount, prometheus.GaugeValue, float64(q.ResourcesUsed.VCores), q.QueueName)
}
}
func (c *collector) fetchClusterMetrics() (*clusterMetrics, error) {
var r clusterMetricsResponse
err := c.fetch("ws/v1/cluster/metrics", &r)
if err != nil {
return nil, err
}
return &r.ClusterMetrics, nil
}
func (c *collector) fetchSchedulerMetrics() (*scheduler, error) {
var r schedulerResponse
err := c.fetch("ws/v1/cluster/scheduler", &r)
if err != nil {
return nil, err
}
return &r.Scheduler, nil
}
func (c *collector) fetch(path string, v any) error {
req, err := http.NewRequest(http.MethodGet, c.endpoint+path, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected HTTP status: %v", resp.StatusCode)
}
err = json.NewDecoder(resp.Body).Decode(v)
if err != nil {
return err
}
return nil
}