| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package consul |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "net/http" |
| 8 | "net/url" |
| 9 | |
| 10 | "github.com/netdata/netdata/go/plugins/pkg/prometheus" |
| 11 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 12 | ) |
| 13 | |
| 14 | func (c *Collector) validateConfig() error { |
| 15 | if c.URL == "" { |
| 16 | return errors.New("'url' not set") |
| 17 | } |
| 18 | return nil |
| 19 | } |
| 20 | |
| 21 | func (c *Collector) initHTTPClient() (*http.Client, error) { |
| 22 | return web.NewHTTPClient(c.ClientConfig) |
| 23 | } |
| 24 | |
| 25 | const urlPathAgentMetrics = "/v1/agent/metrics" |
| 26 | |
| 27 | func (c *Collector) initPrometheusClient(httpClient *http.Client) (prometheus.Prometheus, error) { |
| 28 | r, err := web.NewHTTPRequest(c.RequestConfig.Copy()) |
| 29 | if err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | r.URL.Path = urlPathAgentMetrics |
| 33 | r.URL.RawQuery = url.Values{ |
| 34 | "format": []string{"prometheus"}, |
| 35 | }.Encode() |
| 36 | |
| 37 | req := c.RequestConfig.Copy() |
| 38 | req.URL = r.URL.String() |
| 39 | |
| 40 | if c.ACLToken != "" { |
| 41 | if req.Headers == nil { |
| 42 | req.Headers = make(map[string]string) |
| 43 | } |
| 44 | req.Headers["X-Consul-Token"] = c.ACLToken |
| 45 | } |
| 46 | |
| 47 | return prometheus.New(httpClient, req), nil |
| 48 | } |