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
|
// SPDX-License-Identifier: GPL-3.0-or-later
package bind
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"path"
"github.com/netdata/netdata/go/go.d.plugin/pkg/web"
)
type serverStats = jsonServerStats
type jsonServerStats struct {
OpCodes map[string]int64
QTypes map[string]int64
NSStats map[string]int64
SockStats map[string]int64
Views map[string]jsonView
}
type jsonView struct {
Resolver jsonViewResolver
}
type jsonViewResolver struct {
Stats map[string]int64
QTypes map[string]int64
CacheStats map[string]int64
}
func newJSONClient(client *http.Client, request web.Request) *jsonClient {
return &jsonClient{httpClient: client, request: request}
}
type jsonClient struct {
httpClient *http.Client
request web.Request
}
func (c jsonClient) serverStats() (*serverStats, error) {
req := c.request.Copy()
u, err := url.Parse(req.URL)
if err != nil {
return nil, fmt.Errorf("error on parsing URL: %v", err)
}
u.Path = path.Join(u.Path, "/server")
req.URL = u.String()
httpReq, err := web.NewHTTPRequest(req)
if err != nil {
return nil, fmt.Errorf("error on creating HTTP request: %v", err)
}
resp, err := c.httpClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("error on request : %v", err)
}
defer closeBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s returned HTTP status %d", httpReq.URL, resp.StatusCode)
}
stats := &jsonServerStats{}
if err = json.NewDecoder(resp.Body).Decode(stats); err != nil {
return nil, fmt.Errorf("error on decoding response from %s : %v", httpReq.URL, err)
}
return stats, nil
}
func closeBody(resp *http.Response) {
if resp != nil && resp.Body != nil {
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
}
}
|