summaryrefslogtreecommitdiffstats
path: root/src/go/plugin/go.d/modules/consul
diff options
context:
space:
mode:
Diffstat (limited to 'src/go/plugin/go.d/modules/consul')
-rw-r--r--src/go/plugin/go.d/modules/consul/collect.go44
-rw-r--r--src/go/plugin/go.d/modules/consul/collect_autopilot.go7
-rw-r--r--src/go/plugin/go.d/modules/consul/collect_checks.go7
-rw-r--r--src/go/plugin/go.d/modules/consul/collect_config.go7
-rw-r--r--src/go/plugin/go.d/modules/consul/collect_net_rtt.go7
-rw-r--r--src/go/plugin/go.d/modules/consul/consul.go26
-rw-r--r--src/go/plugin/go.d/modules/consul/consul_test.go4
-rw-r--r--src/go/plugin/go.d/modules/consul/init.go6
-rw-r--r--src/go/plugin/go.d/modules/consul/integrations/consul.md6
9 files changed, 55 insertions, 59 deletions
diff --git a/src/go/plugin/go.d/modules/consul/collect.go b/src/go/plugin/go.d/modules/consul/collect.go
index 3033e046..628bde1b 100644
--- a/src/go/plugin/go.d/modules/consul/collect.go
+++ b/src/go/plugin/go.d/modules/consul/collect.go
@@ -3,10 +3,9 @@
package consul
import (
- "encoding/json"
"fmt"
- "io"
"net/http"
+ "slices"
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web"
)
@@ -68,44 +67,23 @@ func (c *Consul) isServer() bool {
return c.cfg.Config.Server
}
-func (c *Consul) doOKDecode(urlPath string, in interface{}, statusCodes ...int) error {
- req, err := web.NewHTTPRequestWithPath(c.Request, urlPath)
+func (c *Consul) client(statusCodes ...int) *web.Client {
+ return web.DoHTTP(c.httpClient).OnNokCode(func(resp *http.Response) (bool, error) {
+ return slices.Contains(statusCodes, resp.StatusCode), nil
+ })
+}
+
+func (c *Consul) createRequest(urlPath string) (*http.Request, error) {
+ req, err := web.NewHTTPRequestWithPath(c.RequestConfig, urlPath)
if err != nil {
- return fmt.Errorf("error on creating request: %v", err)
+ return nil, fmt.Errorf("failed to create '%s' request: %w", urlPath, err)
}
if c.ACLToken != "" {
req.Header.Set("X-Consul-Token", c.ACLToken)
}
- resp, err := c.httpClient.Do(req)
- if err != nil {
- return fmt.Errorf("error on request to %s : %v", req.URL, err)
- }
-
- defer closeBody(resp)
-
- codes := map[int]bool{http.StatusOK: true}
- for _, v := range statusCodes {
- codes[v] = true
- }
-
- if !codes[resp.StatusCode] {
- return fmt.Errorf("%s returned HTTP status %d", req.URL, resp.StatusCode)
- }
-
- if err = json.NewDecoder(resp.Body).Decode(&in); err != nil {
- return fmt.Errorf("error on decoding response from %s : %v", req.URL, err)
- }
-
- return nil
-}
-
-func closeBody(resp *http.Response) {
- if resp != nil && resp.Body != nil {
- _, _ = io.Copy(io.Discard, resp.Body)
- _ = resp.Body.Close()
- }
+ return req, nil
}
func boolToInt(v bool) int64 {
diff --git a/src/go/plugin/go.d/modules/consul/collect_autopilot.go b/src/go/plugin/go.d/modules/consul/collect_autopilot.go
index e73ce9b2..7ae98e43 100644
--- a/src/go/plugin/go.d/modules/consul/collect_autopilot.go
+++ b/src/go/plugin/go.d/modules/consul/collect_autopilot.go
@@ -25,11 +25,16 @@ type autopilotHealth struct {
}
func (c *Consul) collectAutopilotHealth(mx map[string]int64) error {
+ req, err := c.createRequest(urlPathOperationAutopilotHealth)
+ if err != nil {
+ return err
+ }
+
var health autopilotHealth
// The HTTP status code will indicate the health of the cluster: 200 is healthy, 429 is unhealthy.
// https://github.com/hashicorp/consul/blob/c7ef04c5979dbc311ff3c67b7bf3028a93e8b0f1/agent/operator_endpoint.go#L325
- if err := c.doOKDecode(urlPathOperationAutopilotHealth, &health, http.StatusTooManyRequests); err != nil {
+ if err := c.client(http.StatusTooManyRequests).RequestJSON(req, &health); err != nil {
return err
}
diff --git a/src/go/plugin/go.d/modules/consul/collect_checks.go b/src/go/plugin/go.d/modules/consul/collect_checks.go
index 88ea4612..fd9e7026 100644
--- a/src/go/plugin/go.d/modules/consul/collect_checks.go
+++ b/src/go/plugin/go.d/modules/consul/collect_checks.go
@@ -18,9 +18,14 @@ type agentCheck struct {
}
func (c *Consul) collectChecks(mx map[string]int64) error {
+ req, err := c.createRequest(urlPathAgentChecks)
+ if err != nil {
+ return err
+ }
+
var checks map[string]*agentCheck
- if err := c.doOKDecode(urlPathAgentChecks, &checks); err != nil {
+ if err := c.client().RequestJSON(req, &checks); err != nil {
return err
}
diff --git a/src/go/plugin/go.d/modules/consul/collect_config.go b/src/go/plugin/go.d/modules/consul/collect_config.go
index 14c77067..493a7a6e 100644
--- a/src/go/plugin/go.d/modules/consul/collect_config.go
+++ b/src/go/plugin/go.d/modules/consul/collect_config.go
@@ -46,9 +46,14 @@ type consulConfig struct {
}
func (c *Consul) collectConfiguration() error {
+ req, err := c.createRequest(urlPathAgentSelf)
+ if err != nil {
+ return err
+ }
+
var cfg consulConfig
- if err := c.doOKDecode(urlPathAgentSelf, &cfg); err != nil {
+ if err := c.client().RequestJSON(req, &cfg); err != nil {
return err
}
diff --git a/src/go/plugin/go.d/modules/consul/collect_net_rtt.go b/src/go/plugin/go.d/modules/consul/collect_net_rtt.go
index 80330d23..1dce8d37 100644
--- a/src/go/plugin/go.d/modules/consul/collect_net_rtt.go
+++ b/src/go/plugin/go.d/modules/consul/collect_net_rtt.go
@@ -23,9 +23,14 @@ type nodeCoordinates struct {
}
func (c *Consul) collectNetworkRTT(mx map[string]int64) error {
+ req, err := c.createRequest(urlPathCoordinateNodes)
+ if err != nil {
+ return err
+ }
+
var coords []nodeCoordinates
- if err := c.doOKDecode(urlPathCoordinateNodes, &coords); err != nil {
+ if err := c.client().RequestJSON(req, &coords); err != nil {
return err
}
diff --git a/src/go/plugin/go.d/modules/consul/consul.go b/src/go/plugin/go.d/modules/consul/consul.go
index 6389d065..13ab25d5 100644
--- a/src/go/plugin/go.d/modules/consul/consul.go
+++ b/src/go/plugin/go.d/modules/consul/consul.go
@@ -5,11 +5,13 @@ package consul
import (
_ "embed"
"errors"
+ "fmt"
"net/http"
"sync"
"time"
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
+ "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/confopt"
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/prometheus"
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web"
@@ -33,12 +35,12 @@ func init() {
func New() *Consul {
return &Consul{
Config: Config{
- HTTP: web.HTTP{
- Request: web.Request{
+ HTTPConfig: web.HTTPConfig{
+ RequestConfig: web.RequestConfig{
URL: "http://127.0.0.1:8500",
},
- Client: web.Client{
- Timeout: web.Duration(time.Second),
+ ClientConfig: web.ClientConfig{
+ Timeout: confopt.Duration(time.Second),
},
},
},
@@ -50,9 +52,9 @@ func New() *Consul {
}
type Config struct {
- UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
- web.HTTP `yaml:",inline" json:""`
- ACLToken string `yaml:"acl_token,omitempty" json:"acl_token"`
+ UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
+ web.HTTPConfig `yaml:",inline" json:""`
+ ACLToken string `yaml:"acl_token,omitempty" json:"acl_token"`
}
type Consul struct {
@@ -79,21 +81,18 @@ func (c *Consul) Configuration() any {
func (c *Consul) Init() error {
if err := c.validateConfig(); err != nil {
- c.Errorf("config validation: %v", err)
- return err
+ return fmt.Errorf("config validation: %v", err)
}
httpClient, err := c.initHTTPClient()
if err != nil {
- c.Errorf("init HTTP client: %v", err)
- return err
+ return fmt.Errorf("init HTTP client: %v", err)
}
c.httpClient = httpClient
prom, err := c.initPrometheusClient(httpClient)
if err != nil {
- c.Errorf("init Prometheus client: %v", err)
- return err
+ return fmt.Errorf("init Prometheus client: %v", err)
}
c.prom = prom
@@ -103,7 +102,6 @@ func (c *Consul) Init() error {
func (c *Consul) Check() error {
mx, err := c.collect()
if err != nil {
- c.Error(err)
return err
}
if len(mx) == 0 {
diff --git a/src/go/plugin/go.d/modules/consul/consul_test.go b/src/go/plugin/go.d/modules/consul/consul_test.go
index ccc9f99b..ded6bde4 100644
--- a/src/go/plugin/go.d/modules/consul/consul_test.go
+++ b/src/go/plugin/go.d/modules/consul/consul_test.go
@@ -75,8 +75,8 @@ func TestConsul_Init(t *testing.T) {
"fail when URL not set": {
wantFail: true,
config: Config{
- HTTP: web.HTTP{
- Request: web.Request{URL: ""},
+ HTTPConfig: web.HTTPConfig{
+ RequestConfig: web.RequestConfig{URL: ""},
},
},
},
diff --git a/src/go/plugin/go.d/modules/consul/init.go b/src/go/plugin/go.d/modules/consul/init.go
index 4ba5b86e..9f19decd 100644
--- a/src/go/plugin/go.d/modules/consul/init.go
+++ b/src/go/plugin/go.d/modules/consul/init.go
@@ -19,13 +19,13 @@ func (c *Consul) validateConfig() error {
}
func (c *Consul) initHTTPClient() (*http.Client, error) {
- return web.NewHTTPClient(c.Client)
+ return web.NewHTTPClient(c.ClientConfig)
}
const urlPathAgentMetrics = "/v1/agent/metrics"
func (c *Consul) initPrometheusClient(httpClient *http.Client) (prometheus.Prometheus, error) {
- r, err := web.NewHTTPRequest(c.Request.Copy())
+ r, err := web.NewHTTPRequest(c.RequestConfig.Copy())
if err != nil {
return nil, err
}
@@ -34,7 +34,7 @@ func (c *Consul) initPrometheusClient(httpClient *http.Client) (prometheus.Prome
"format": []string{"prometheus"},
}.Encode()
- req := c.Request.Copy()
+ req := c.RequestConfig.Copy()
req.URL = r.URL.String()
if c.ACLToken != "" {
diff --git a/src/go/plugin/go.d/modules/consul/integrations/consul.md b/src/go/plugin/go.d/modules/consul/integrations/consul.md
index 3a364bfd..55a1bbf5 100644
--- a/src/go/plugin/go.d/modules/consul/integrations/consul.md
+++ b/src/go/plugin/go.d/modules/consul/integrations/consul.md
@@ -202,8 +202,8 @@ Required **only if authentication is enabled**.
The configuration file name for this integration is `go.d/consul.conf`.
-You can edit the configuration file using the `edit-config` script from the
-Netdata [config directory](/docs/netdata-agent/configuration/README.md#the-netdata-config-directory).
+You can edit the configuration file using the [`edit-config`](https://github.com/netdata/netdata/blob/master/docs/netdata-agent/configuration/README.md#edit-a-configuration-file-using-edit-config) script from the
+Netdata [config directory](https://github.com/netdata/netdata/blob/master/docs/netdata-agent/configuration/README.md#the-netdata-config-directory).
```bash
cd /etc/netdata 2>/dev/null || cd /opt/netdata/etc/netdata
@@ -223,7 +223,7 @@ The following options can be defined globally: update_every, autodetection_retry
| url | Server URL. | http://localhost:8500 | yes |
| acl_token | ACL token used in every request. | | no |
| max_checks | Checks processing/charting limit. | | no |
-| max_filter | Checks processing/charting filter. Uses [simple patterns](/src/libnetdata/simple_pattern/README.md). | | no |
+| max_filter | Checks processing/charting filter. Uses [simple patterns](https://github.com/netdata/netdata/blob/master/src/libnetdata/simple_pattern/README.md). | | no |
| username | Username for basic HTTP authentication. | | no |
| password | Password for basic HTTP authentication. | | no |
| proxy_url | Proxy URL. | | no |