| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package dnsdist |
| 4 | |
| 5 | import ( |
| 6 | "maps" |
| 7 | "net/url" |
| 8 | |
| 9 | "github.com/netdata/netdata/go/plugins/pkg/stm" |
| 10 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 11 | ) |
| 12 | |
| 13 | const ( |
| 14 | urlPathJSONStat = "/jsonstat" |
| 15 | ) |
| 16 | |
| 17 | func (c *Collector) collect() (map[string]int64, error) { |
| 18 | statistics, err := c.scrapeStatistics() |
| 19 | if err != nil { |
| 20 | return nil, err |
| 21 | } |
| 22 | |
| 23 | collected := make(map[string]int64) |
| 24 | c.collectStatistic(collected, statistics) |
| 25 | |
| 26 | return collected, nil |
| 27 | } |
| 28 | |
| 29 | func (c *Collector) collectStatistic(collected map[string]int64, statistics *statisticMetrics) { |
| 30 | maps.Copy(collected, stm.ToMap(statistics)) |
| 31 | } |
| 32 | |
| 33 | func (c *Collector) scrapeStatistics() (*statisticMetrics, error) { |
| 34 | req, err := web.NewHTTPRequestWithPath(c.RequestConfig, urlPathJSONStat) |
| 35 | if err != nil { |
| 36 | return nil, err |
| 37 | } |
| 38 | req.URL.RawQuery = url.Values{"command": []string{"stats"}}.Encode() |
| 39 | |
| 40 | var stats statisticMetrics |
| 41 | if err := web.DoHTTP(c.httpClient).RequestJSON(req, &stats); err != nil { |
| 42 | return nil, err |
| 43 | } |
| 44 | |
| 45 | return &stats, nil |
| 46 | } |