-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathravendb_client.go
196 lines (152 loc) · 4.3 KB
/
ravendb_client.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
jp "github.com/buger/jsonparser"
)
var (
client http.Client
)
type stats struct {
cpu []byte
memory []byte
metrics []byte
nodeInfo []byte
dbStats []*dbStats
}
type dbStats struct {
database string
collectionStats []byte
metrics []byte
indexes []byte
databaseStats []byte
storage []byte
tasks []byte
}
func initializeClient() {
transport := &http.Transport{TLSClientConfig: prepareTLSConfig()}
client = http.Client{
Transport: transport,
Timeout: timeout,
}
}
func getStats() (*stats, error) {
databases, err := getDatabaseNames()
if err != nil {
return nil, err
}
paths := preparePaths(databases)
results := getAllPaths(paths, 16)
return organizeGetResults(results, databases)
}
func getDatabaseNames() ([]string, error) {
data, err := get("/databases")
if err != nil {
return nil, err
}
var databases []string
dbsNode, _, _, _ := jp.Get(data, "Databases")
jp.ArrayEach(dbsNode, func(value []byte, dataType jp.ValueType, offset int, err error) {
database, _ := jp.GetString(value, "Name")
databases = append(databases, database)
})
return databases, nil
}
func preparePaths(databases []string) []string {
paths := []string{
"/admin/debug/cpu/stats",
"/admin/debug/memory/stats",
"/admin/metrics",
"/cluster/node-info",
}
for _, database := range databases {
paths = append(paths, fmt.Sprintf("/databases/%s/collections/stats", database))
paths = append(paths, fmt.Sprintf("/databases/%s/indexes", database))
paths = append(paths, fmt.Sprintf("/databases/%s/metrics", database))
paths = append(paths, fmt.Sprintf("/databases/%s/stats", database))
paths = append(paths, fmt.Sprintf("/databases/%s/debug/storage/report", database))
paths = append(paths, fmt.Sprintf("/databases/%s/tasks", database))
}
return paths
}
func getAllPaths(paths []string, maxParallelism int) map[string]getResult {
pathsChan := make(chan string)
resultChan := make(chan getResult, len(paths))
var doneChans []<-chan bool
for i := 0; i < maxParallelism; i++ {
doneChans = append(doneChans, getWorker(pathsChan, resultChan))
}
for _, path := range paths {
pathsChan <- path
}
close(pathsChan)
for i := 0; i < maxParallelism; i++ {
<-doneChans[i]
}
allResults := make(map[string]getResult)
for i := 0; i < len(paths); i++ {
result := <-resultChan
allResults[result.path] = result
}
return allResults
}
func getWorker(paths <-chan string, results chan<- getResult) <-chan bool {
done := make(chan bool)
go func() {
for path := range paths {
result, err := get(path)
results <- getResult{path, result, err}
}
done <- true
}()
return done
}
type getResult struct {
path string
result []byte
err error
}
func get(path string) ([]byte, error) {
url := ravenDbURL + path
log.WithField("url", url).Debug("GET request to RavenDB")
response, err := client.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
buf, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
if response.StatusCode >= 400 {
return nil, fmt.Errorf("Server responded with HTTP %d and body: %s", response.StatusCode, string(buf))
}
return buf, nil
}
func organizeGetResults(results map[string]getResult, databases []string) (*stats, error) {
for _, result := range results {
if result.err != nil {
return nil, result.err
}
}
stats := stats{
cpu: results["/admin/debug/cpu/stats"].result,
memory: results["/admin/debug/memory/stats"].result,
metrics: results["/admin/metrics"].result,
nodeInfo: results["/cluster/node-info"].result,
}
for _, database := range databases {
dbs := &dbStats{
database: database,
collectionStats: results[fmt.Sprintf("/databases/%s/collections/stats", database)].result,
indexes: results[fmt.Sprintf("/databases/%s/indexes", database)].result,
metrics: results[fmt.Sprintf("/databases/%s/metrics", database)].result,
databaseStats: results[fmt.Sprintf("/databases/%s/stats", database)].result,
storage: results[fmt.Sprintf("/databases/%s//debug/storage/report", database)].result,
tasks: results[fmt.Sprintf("/databases/%s/tasks", database)].result,
}
stats.dbStats = append(stats.dbStats, dbs)
}
return &stats, nil
}