-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexporter.go
327 lines (312 loc) · 11 KB
/
exporter.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"github.com/prometheus/client_golang/prometheus"
"log"
"strconv"
"sync"
)
type Exporter struct {
ExporterMetrics
TypeSense *Client
}
type ExporterMetrics struct {
Up *prometheus.Desc
SystemCPUxActivePercentage *prometheus.Desc
SystemDiskTotalBytes *prometheus.Desc
SystemDiskUsedBytes *prometheus.Desc
SystemMemoryTotalBytes *prometheus.Desc
SystemMemoryUsedBytes *prometheus.Desc
SystemNetworkReceived *prometheus.Desc
SystemNetworkSent *prometheus.Desc
TypesenseMemoryActive *prometheus.Desc
TypesenseMemoryAllocated *prometheus.Desc
TypesenseMemoryFragment *prometheus.Desc
TypesenseMemoryMapped *prometheus.Desc
TypesenseMemoryMetadata *prometheus.Desc
TypesenseMemoryResident *prometheus.Desc
TypesenseMemoryRetained *prometheus.Desc
ApiStatsOperationLatency *prometheus.Desc
ApiStatsOperationRequests *prometheus.Desc
ApiStatsEndpointLatency *prometheus.Desc
ApiStatsEndpointRequests *prometheus.Desc
ApiStatsPendingWrite *prometheus.Desc
}
func (em *ExporterMetrics) Describe(ch chan<- *prometheus.Desc) {
ch <- em.Up
ch <- em.SystemCPUxActivePercentage
ch <- em.SystemDiskTotalBytes
ch <- em.SystemDiskUsedBytes
ch <- em.SystemMemoryTotalBytes
ch <- em.SystemMemoryUsedBytes
ch <- em.SystemNetworkReceived
ch <- em.SystemNetworkSent
ch <- em.TypesenseMemoryActive
ch <- em.TypesenseMemoryAllocated
ch <- em.TypesenseMemoryFragment
ch <- em.TypesenseMemoryMapped
ch <- em.TypesenseMemoryMetadata
ch <- em.TypesenseMemoryResident
ch <- em.TypesenseMemoryRetained
ch <- em.ApiStatsOperationLatency
ch <- em.ApiStatsOperationRequests
ch <- em.ApiStatsEndpointLatency
ch <- em.ApiStatsEndpointRequests
ch <- em.ApiStatsPendingWrite
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.collectUp(ch)
var wg sync.WaitGroup
wg.Add(2)
go func(wg *sync.WaitGroup) {
e.collectMetrics(ch)
wg.Done()
}(&wg)
go func(wg *sync.WaitGroup) {
e.collectStats(ch)
wg.Done()
}(&wg)
wg.Wait()
}
func (e *Exporter) collectMetrics(ch chan<- prometheus.Metric) {
v, err := e.TypeSense.GetMetrics()
if err != nil {
log.Println(err)
return
}
i := 0
for _, value := range v.SystemCPUxActivePercentage {
i++
float, err := value.Float64()
if err != nil {
continue
}
label := strconv.Itoa(i)
if i == len(v.SystemCPUxActivePercentage) {
label = "all"
}
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.SystemCPUxActivePercentage, prometheus.GaugeValue, percentageToRatio(float),
label,
)
}
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.SystemDiskTotalBytes, prometheus.GaugeValue, v.SystemDiskTotalBytes,
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.SystemDiskUsedBytes, prometheus.GaugeValue, v.SystemDiskUsedBytes,
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.SystemMemoryTotalBytes, prometheus.GaugeValue, v.SystemMemoryTotalBytes,
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.SystemMemoryUsedBytes, prometheus.GaugeValue, v.SystemMemoryUsedBytes,
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.SystemNetworkReceived, prometheus.GaugeValue, v.SystemNetworkReceivedBytes,
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.SystemNetworkSent, prometheus.GaugeValue, v.SystemNetworkSentBytes,
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.TypesenseMemoryActive, prometheus.GaugeValue, v.TypesenseMemoryActiveBytes,
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.TypesenseMemoryAllocated, prometheus.GaugeValue, v.TypesenseMemoryAllocated,
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.TypesenseMemoryFragment, prometheus.GaugeValue, v.TypesenseMemoryFragment,
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.TypesenseMemoryMapped, prometheus.GaugeValue, v.TypesenseMemoryMapped,
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.TypesenseMemoryMetadata, prometheus.GaugeValue, v.TypesenseMemoryMetadata,
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.TypesenseMemoryResident, prometheus.GaugeValue, v.TypesenseMemoryResident,
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.TypesenseMemoryRetained, prometheus.GaugeValue, v.TypesenseMemoryRetained,
)
}
func (e *Exporter) collectStats(ch chan<- prometheus.Metric) {
s, err := e.TypeSense.GetStats()
if err != nil {
log.Println(err)
return
}
for name, value := range s.Requests {
float, err := value.Float64()
if err != nil {
continue
}
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.ApiStatsEndpointRequests, prometheus.GaugeValue, float, name,
)
}
for name, value := range s.Latency {
float, err := value.Float64()
if err != nil {
continue
}
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.ApiStatsEndpointLatency, prometheus.GaugeValue, msToSeconds(float), name,
)
}
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.ApiStatsOperationLatency, prometheus.GaugeValue, msToSeconds(s.DeleteLatency), "delete",
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.ApiStatsOperationRequests, prometheus.GaugeValue, s.DeleteRequests, "delete",
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.ApiStatsOperationLatency, prometheus.GaugeValue, msToSeconds(s.ImportLatency), "import",
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.ApiStatsOperationRequests, prometheus.GaugeValue, s.ImportRequests, "import",
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.ApiStatsOperationLatency, prometheus.GaugeValue, msToSeconds(s.SearchLatency), "search",
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.ApiStatsOperationRequests, prometheus.GaugeValue, s.SearchRequests, "search",
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.ApiStatsOperationLatency, prometheus.GaugeValue, msToSeconds(s.WriteLatency), "write",
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.ApiStatsOperationRequests, prometheus.GaugeValue, s.WriteRequests, "write",
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.ApiStatsOperationRequests, prometheus.GaugeValue, s.TotalRequests, "all",
)
ch <- prometheus.MustNewConstMetric(
e.ExporterMetrics.ApiStatsPendingWrite, prometheus.GaugeValue, s.PendingWrite,
)
}
func (e *Exporter) collectUp(ch chan<- prometheus.Metric) {
healthy, err := e.TypeSense.GetHealth()
if err != nil {
ch <- prometheus.MustNewConstMetric(e.ExporterMetrics.Up, prometheus.GaugeValue, 0)
return
}
if healthy {
ch <- prometheus.MustNewConstMetric(e.ExporterMetrics.Up, prometheus.GaugeValue, 1)
return
}
ch <- prometheus.MustNewConstMetric(e.ExporterMetrics.Up, prometheus.GaugeValue, 0)
}
func (em *ExporterMetrics) initializeDescriptors() {
const namespace = "typesense"
const subsystemSystem = "system"
const subsystemApplication = "application"
const subsystemApi = "api"
em.Up = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"Was the last scrape of the Typesense exporter successful.",
nil, nil,
)
em.SystemCPUxActivePercentage = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemSystem, "cpu_x_active"),
"Ratio of CPU core time spent in user mode.",
[]string{"cpu"}, nil,
)
em.SystemDiskTotalBytes = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemSystem, "disk_total_bytes"),
"Total disk space in bytes.",
nil, prometheus.Labels{"unit": "bytes"},
)
em.SystemDiskUsedBytes = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemSystem, "disk_used_bytes"),
"Used disk space in bytes.",
nil, prometheus.Labels{"unit": "bytes"},
)
em.SystemMemoryTotalBytes = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemSystem, "memory_total_bytes"),
"Total memory in bytes.",
nil, prometheus.Labels{"unit": "bytes"},
)
em.SystemMemoryUsedBytes = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemSystem, "memory_used_bytes"),
"Used memory in bytes.",
nil, prometheus.Labels{"unit": "bytes"},
)
em.SystemNetworkReceived = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemSystem, "network_received_bytes"),
"Total bytes received by the network interface.",
nil, prometheus.Labels{"unit": "bytes"},
)
em.SystemNetworkSent = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemSystem, "network_sent_bytes"),
"Total bytes sent by the network interface.",
nil, prometheus.Labels{"unit": "bytes"},
)
em.TypesenseMemoryActive = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemApplication, "memory_active_bytes"),
"Total active memory in bytes.",
nil, prometheus.Labels{"unit": "bytes"},
)
em.TypesenseMemoryAllocated = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemApplication, "memory_allocated_bytes"),
"Total allocated memory in bytes.",
nil, prometheus.Labels{"unit": "bytes"},
)
em.TypesenseMemoryFragment = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemApplication, "memory_fragment_bytes"),
"Total memory fragmentation in bytes.",
nil, prometheus.Labels{"unit": "bytes"},
)
em.TypesenseMemoryMapped = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemApplication, "memory_mapped_bytes"),
"Total mapped memory in bytes.",
nil, prometheus.Labels{"unit": "bytes"},
)
em.TypesenseMemoryMetadata = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemApplication, "memory_metadata_bytes"),
"Total metadata memory in bytes.",
nil, prometheus.Labels{"unit": "bytes"},
)
em.TypesenseMemoryResident = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemApplication, "memory_resident_bytes"),
"Total resident memory in bytes.",
nil, prometheus.Labels{"unit": "bytes"},
)
em.TypesenseMemoryRetained = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemApplication, "memory_retained_bytes"),
"Total retained memory in bytes.",
nil, prometheus.Labels{"unit": "bytes"},
)
em.ApiStatsPendingWrite = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemApi, "pending_write_batches"),
"Number of pending write batches.",
nil, nil,
)
em.ApiStatsOperationLatency = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemApi, "operation_latency_seconds"),
"Latency of the operation in seconds.",
[]string{"operation"}, nil,
)
em.ApiStatsOperationRequests = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemApi, "operation_requests_per_second"),
"Number of requests per second for the operation.",
[]string{"operation"}, nil,
)
em.ApiStatsEndpointLatency = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemApi, "endpoint_latency_seconds"),
"Latency of the endpoint in seconds.",
[]string{"endpoint"}, nil,
)
em.ApiStatsEndpointRequests = prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystemApi, "endpoint_requests_per_second"),
"Number of requests per second for the endpoint.",
[]string{"endpoint"}, nil,
)
}
func NewExporter(typesenseClient *Client) *Exporter {
em := ExporterMetrics{}
em.initializeDescriptors()
return &Exporter{
ExporterMetrics: em,
TypeSense: typesenseClient,
}
}