github.com/netdata/go.d.plugin@v0.58.1/modules/consul/init.go (about)

     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/go.d.plugin/pkg/prometheus"
    11  	"github.com/netdata/go.d.plugin/pkg/web"
    12  )
    13  
    14  func (c *Consul) validateConfig() error {
    15  	if c.URL == "" {
    16  		return errors.New("'url' not set")
    17  	}
    18  	return nil
    19  }
    20  
    21  func (c *Consul) initHTTPClient() (*http.Client, error) {
    22  	return web.NewHTTPClient(c.Client)
    23  }
    24  
    25  const urlPathAgentMetrics = "/v1/agent/metrics"
    26  
    27  func (c *Consul) initPrometheusClient(httpClient *http.Client) (prometheus.Prometheus, error) {
    28  	r, err := web.NewHTTPRequest(c.Request.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.Request.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  }