forked from buildsville/redash-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
256 lines (230 loc) · 6.48 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
)
const (
defaultMetricsInterval = 30
defaultAddr = ":9295"
defaultReashScheme = "http"
defaultRedashHost = "localhost"
defaultRedashPort = "5000"
)
const rootDoc = `<html>
<head><title>Redash Exporter</title></head>
<body>
<h1>Redash Exporter</h1>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>
`
var (
addr = flag.String("listen-address", defaultAddr, "The address to listen HTTP requests.")
metricsInterval = flag.Int("metricsInterval", defaultMetricsInterval, "Interval to scrape status.")
redashScheme = flag.String("redashScheme", defaultReashScheme, "target Redash scheme.")
redashHost = flag.String("redashHost", defaultRedashHost, "target Redash host.")
redashPort = flag.String("redashPort", defaultRedashPort, "target Redash port.")
)
var apiKey = os.Getenv("REDASH_API_KEY")
type redashStatus struct {
DashboardsCount float64 `json:"dashboards_count"`
DatabaseMetrics struct {
Metrics [][]interface{} `json:"metrics"`
} `json:"database_metrics"`
Manager struct {
OutdatedQueriesCount float64 `json:"outdated_queries_count,string"`
Queues struct {
Celery struct {
Size float64 `json:"size"`
} `json:"celery"`
Queries struct {
Size float64 `json:"size"`
} `json:"queries"`
ScheduledQueries struct {
Size float64 `json:"size"`
} `json:"scheduled_queries"`
} `json:"queues"`
} `json:"manager"`
QueriesCount float64 `json:"queries_count"`
QueryResultsCount float64 `json:"query_results_count"`
RedisUsedMemory float64 `json:"redis_used_memory"`
UnusedQueryResultsCount float64 `json:"unused_query_results_count"`
RedashVersion string `json:"version"`
WidgetsCount float64 `json:"widgets_count"`
}
var infoLabels = []string{
"redash_version",
}
var labels = []string{}
var (
info = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "redash_info",
Help: "Information of Redash.",
},
infoLabels,
)
dashboardsCount = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "redash_dashboards_count",
Help: "Number of dashboards in Redash.",
},
labels,
)
queryResultsSize = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "redash_query_results_size_bytes",
Help: "Size of Redash query results.",
},
labels,
)
dbSize = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "redash_db_size_bytes",
Help: "Size of Redash database.",
},
labels,
)
outdatedQueriesCount = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "redash_outdated_queries_count",
Help: "Number of outdated queries.",
},
labels,
)
queuesCelery = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "redash_queues_celery",
Help: "Number of celery queues.",
},
labels,
)
queuesQueries = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "redash_queues_queries",
Help: "Number of query queues.",
},
labels,
)
queuesScheduledQueries = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "redash_queues_scheduled_queries",
Help: "Number of scheduled query queues.",
},
labels,
)
queriesCount = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "redash_queries_count",
Help: "Number of queries stored in redash.",
},
labels,
)
queryResultsCount = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "redash_query_results_count",
Help: "Number of query results.",
},
labels,
)
redisUsedMemory = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "redash_redis_used_memory_bytes",
Help: "Memory size used by redis in Redash.",
},
labels,
)
unusedQueryResultsCount = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "redash_unused_query_results_count",
Help: "Number of unused query results.",
},
labels,
)
widgetsCount = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "redash_wigets_count",
Help: "Number of widgets.",
},
labels,
)
)
func getRedashStatus() (redashStatus, error) {
url := *redashScheme + "://" + *redashHost + ":" + *redashPort
endpoint := "/status.json"
resp, e := http.Get(url + endpoint + "?api_key=" + apiKey)
if e != nil {
return redashStatus{}, fmt.Errorf("httpGet error : %v", e)
}
body, e := ioutil.ReadAll(resp.Body)
if e != nil {
return redashStatus{}, fmt.Errorf("io read error : %v", e)
}
var jsonBody redashStatus
e = json.Unmarshal(body, &jsonBody)
if e != nil {
return redashStatus{}, fmt.Errorf("json parse error : %v. Is api key correct?", e)
}
return jsonBody, nil
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(rootDoc))
}
func main() {
flag.Parse()
log.Info("start Redash exporter.")
go func() {
for {
status, err := getRedashStatus()
if err != nil {
log.Error(err)
time.Sleep(time.Duration(*metricsInterval) * time.Second)
continue
}
label := prometheus.Labels{}
infoLabel := prometheus.Labels{
"redash_version": status.RedashVersion,
}
metrics := map[string]float64{}
for _, metric := range status.DatabaseMetrics.Metrics {
var key string
var val float64
for _, m := range metric {
switch m.(type) {
case string:
key = m.(string)
case float64:
val = m.(float64)
}
}
metrics[key] = val
}
info.With(infoLabel).Set(float64(1))
dashboardsCount.With(label).Set(status.DashboardsCount)
queryResultsSize.With(label).Set(metrics["Query Results Size"])
dbSize.With(label).Set(metrics["Redash DB Size"])
outdatedQueriesCount.With(label).Set(float64(status.Manager.OutdatedQueriesCount))
queuesCelery.With(label).Set(status.Manager.Queues.Celery.Size)
queuesQueries.With(label).Set(status.Manager.Queues.Queries.Size)
queuesScheduledQueries.With(label).Set(status.Manager.Queues.ScheduledQueries.Size)
queriesCount.With(label).Set(status.QueriesCount)
queryResultsCount.With(label).Set(status.QueryResultsCount)
redisUsedMemory.With(label).Set(status.RedisUsedMemory)
unusedQueryResultsCount.With(label).Set(status.UnusedQueryResultsCount)
widgetsCount.With(label).Set(status.WidgetsCount)
time.Sleep(time.Duration(*metricsInterval) * time.Second)
}
}()
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", rootHandler)
log.Fatal(http.ListenAndServe(*addr, nil))
}