diff options
Diffstat (limited to 'src/go/plugin/go.d/modules/squid')
-rw-r--r-- | src/go/plugin/go.d/modules/squid/collect.go | 43 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/squid/integrations/squid.md | 4 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/squid/squid.go | 23 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/squid/squid_test.go | 8 |
4 files changed, 25 insertions, 53 deletions
diff --git a/src/go/plugin/go.d/modules/squid/collect.go b/src/go/plugin/go.d/modules/squid/collect.go index bb0cf1ab..889223a9 100644 --- a/src/go/plugin/go.d/modules/squid/collect.go +++ b/src/go/plugin/go.d/modules/squid/collect.go @@ -6,7 +6,6 @@ import ( "bufio" "fmt" "io" - "net/http" "strconv" "strings" @@ -42,12 +41,12 @@ func (s *Squid) collect() (map[string]int64, error) { } func (s *Squid) collectCounters(mx map[string]int64) error { - req, err := web.NewHTTPRequestWithPath(s.Request, urlPathServerStats) + req, err := web.NewHTTPRequestWithPath(s.RequestConfig, urlPathServerStats) if err != nil { - return err + return fmt.Errorf("failed to create '%s' request: %w", urlPathServerStats, err) } - if err := s.doOK(req, func(body io.Reader) error { + return web.DoHTTP(s.httpClient).Request(req, func(body io.Reader) error { sc := bufio.NewScanner(body) for sc.Scan() { @@ -70,36 +69,10 @@ func (s *Squid) collectCounters(mx map[string]int64) error { mx[key] = v } - return nil - }); err != nil { - return err - } - - if len(mx) == 0 { - return fmt.Errorf("unexpected response from '%s': no metrics found", req.URL) - } - - return nil -} - -func (s *Squid) doOK(req *http.Request, parse func(body io.Reader) error) error { - resp, err := s.httpClient.Do(req) - if err != nil { - return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err) - } - defer closeBody(resp) - - if resp.StatusCode != http.StatusOK { - return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode) - } - - return parse(resp.Body) -} - -func closeBody(resp *http.Response) { - if resp != nil && resp.Body != nil { - _, _ = io.Copy(io.Discard, resp.Body) - _ = resp.Body.Close() - } + if len(mx) == 0 { + return fmt.Errorf("unexpected response from '%s': no metrics found", req.URL) + } + return nil + }) } diff --git a/src/go/plugin/go.d/modules/squid/integrations/squid.md b/src/go/plugin/go.d/modules/squid/integrations/squid.md index 1a448de3..b2894664 100644 --- a/src/go/plugin/go.d/modules/squid/integrations/squid.md +++ b/src/go/plugin/go.d/modules/squid/integrations/squid.md @@ -93,8 +93,8 @@ No action required. The configuration file name for this integration is `go.d/squid.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 diff --git a/src/go/plugin/go.d/modules/squid/squid.go b/src/go/plugin/go.d/modules/squid/squid.go index fe9c15ec..a2fb178e 100644 --- a/src/go/plugin/go.d/modules/squid/squid.go +++ b/src/go/plugin/go.d/modules/squid/squid.go @@ -5,10 +5,12 @@ package squid import ( _ "embed" "errors" + "fmt" "net/http" "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/web" ) @@ -26,12 +28,12 @@ func init() { func New() *Squid { return &Squid{ Config: Config{ - HTTP: web.HTTP{ - Request: web.Request{ + HTTPConfig: web.HTTPConfig{ + RequestConfig: web.RequestConfig{ URL: "http://127.0.0.1:3128", }, - Client: web.Client{ - Timeout: web.Duration(time.Second * 1), + ClientConfig: web.ClientConfig{ + Timeout: confopt.Duration(time.Second * 1), }, }, }, @@ -40,8 +42,8 @@ func New() *Squid { } type Config struct { - UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` - web.HTTP `yaml:",inline" json:""` + UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` + web.HTTPConfig `yaml:",inline" json:""` } type Squid struct { @@ -59,14 +61,12 @@ func (s *Squid) Configuration() any { func (s *Squid) Init() error { if s.URL == "" { - s.Error("URL not set") - return errors.New("url not set") + return errors.New("config: url not set") } - client, err := web.NewHTTPClient(s.Client) + client, err := web.NewHTTPClient(s.ClientConfig) if err != nil { - s.Error(err) - return err + return fmt.Errorf("init http client: %w", err) } s.httpClient = client @@ -79,7 +79,6 @@ func (s *Squid) Init() error { func (s *Squid) Check() error { mx, err := s.collect() if err != nil { - s.Error(err) return err } diff --git a/src/go/plugin/go.d/modules/squid/squid_test.go b/src/go/plugin/go.d/modules/squid/squid_test.go index c0856f89..304ef3c2 100644 --- a/src/go/plugin/go.d/modules/squid/squid_test.go +++ b/src/go/plugin/go.d/modules/squid/squid_test.go @@ -47,8 +47,8 @@ func TestSquid_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: ""}, }, }, }, @@ -187,7 +187,7 @@ Fusce et felis pulvinar, posuere sem non, porttitor eros.`) srv := httptest.NewServer(http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { - _, _ = w.Write([]byte(resp)) + _, _ = w.Write(resp) })) squid := New() @@ -203,7 +203,7 @@ func prepareCaseEmptyResponse(t *testing.T) (*Squid, func()) { srv := httptest.NewServer(http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { - _, _ = w.Write([]byte(resp)) + _, _ = w.Write(resp) })) squid := New() |