diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 11:19:16 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-07-24 09:53:24 +0000 |
commit | b5f8ee61a7f7e9bd291dd26b0585d03eb686c941 (patch) | |
tree | d4d31289c39fc00da064a825df13a0b98ce95b10 /src/go/collectors/go.d.plugin/modules/bind/json_client.go | |
parent | Adding upstream version 1.44.3. (diff) | |
download | netdata-b5f8ee61a7f7e9bd291dd26b0585d03eb686c941.tar.xz netdata-b5f8ee61a7f7e9bd291dd26b0585d03eb686c941.zip |
Adding upstream version 1.46.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/go/collectors/go.d.plugin/modules/bind/json_client.go | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/go/collectors/go.d.plugin/modules/bind/json_client.go b/src/go/collectors/go.d.plugin/modules/bind/json_client.go new file mode 100644 index 000000000..46a98de3a --- /dev/null +++ b/src/go/collectors/go.d.plugin/modules/bind/json_client.go @@ -0,0 +1,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() + } +} |