summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/consul/init.go
blob: 944609a169c97f97d8ff1a38a6bdb41efe6ea7f7 (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package consul

import (
	"errors"
	"net/http"
	"net/url"

	"github.com/netdata/netdata/go/go.d.plugin/pkg/prometheus"
	"github.com/netdata/netdata/go/go.d.plugin/pkg/web"
)

func (c *Consul) validateConfig() error {
	if c.URL == "" {
		return errors.New("'url' not set")
	}
	return nil
}

func (c *Consul) initHTTPClient() (*http.Client, error) {
	return web.NewHTTPClient(c.Client)
}

const urlPathAgentMetrics = "/v1/agent/metrics"

func (c *Consul) initPrometheusClient(httpClient *http.Client) (prometheus.Prometheus, error) {
	r, err := web.NewHTTPRequest(c.Request.Copy())
	if err != nil {
		return nil, err
	}
	r.URL.Path = urlPathAgentMetrics
	r.URL.RawQuery = url.Values{
		"format": []string{"prometheus"},
	}.Encode()

	req := c.Request.Copy()
	req.URL = r.URL.String()

	if c.ACLToken != "" {
		if req.Headers == nil {
			req.Headers = make(map[string]string)
		}
		req.Headers["X-Consul-Token"] = c.ACLToken
	}

	return prometheus.New(httpClient, req), nil
}