diff options
Diffstat (limited to 'src/go/plugin/go.d/modules/rspamd')
-rw-r--r-- | src/go/plugin/go.d/modules/rspamd/collect.go | 31 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/rspamd/integrations/rspamd.md | 4 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/rspamd/rspamd.go | 23 | ||||
-rw-r--r-- | src/go/plugin/go.d/modules/rspamd/rspamd_test.go | 23 |
4 files changed, 19 insertions, 62 deletions
diff --git a/src/go/plugin/go.d/modules/rspamd/collect.go b/src/go/plugin/go.d/modules/rspamd/collect.go index ecbe4a03..caa785eb 100644 --- a/src/go/plugin/go.d/modules/rspamd/collect.go +++ b/src/go/plugin/go.d/modules/rspamd/collect.go @@ -3,10 +3,7 @@ package rspamd import ( - "encoding/json" "fmt" - "io" - "net/http" "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/stm" "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web" @@ -50,13 +47,13 @@ func (r *Rspamd) collect() (map[string]int64, error) { } func (r *Rspamd) queryRspamdStats() (*rspamdStats, error) { - req, err := web.NewHTTPRequestWithPath(r.Request, "/stat") + req, err := web.NewHTTPRequestWithPath(r.RequestConfig, "/stat") if err != nil { return nil, err } var stats rspamdStats - if err := r.doOKDecode(req, &stats); err != nil { + if err := web.DoHTTP(r.httpClient).RequestJSON(req, &stats); err != nil { return nil, err } @@ -66,27 +63,3 @@ func (r *Rspamd) queryRspamdStats() (*rspamdStats, error) { return &stats, nil } - -func (r *Rspamd) doOKDecode(req *http.Request, in interface{}) error { - resp, err := r.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) - } - - 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() - } -} diff --git a/src/go/plugin/go.d/modules/rspamd/integrations/rspamd.md b/src/go/plugin/go.d/modules/rspamd/integrations/rspamd.md index fe094942..c60fd2b4 100644 --- a/src/go/plugin/go.d/modules/rspamd/integrations/rspamd.md +++ b/src/go/plugin/go.d/modules/rspamd/integrations/rspamd.md @@ -93,8 +93,8 @@ No action required. The configuration file name for this integration is `go.d/rspamd.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/rspamd/rspamd.go b/src/go/plugin/go.d/modules/rspamd/rspamd.go index 0a5c4ffe..77131b3d 100644 --- a/src/go/plugin/go.d/modules/rspamd/rspamd.go +++ b/src/go/plugin/go.d/modules/rspamd/rspamd.go @@ -5,10 +5,12 @@ package rspamd 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() *Rspamd { return &Rspamd{ Config: Config{ - HTTP: web.HTTP{ - Request: web.Request{ + HTTPConfig: web.HTTPConfig{ + RequestConfig: web.RequestConfig{ URL: "http://127.0.0.1:11334", }, - Client: web.Client{ - Timeout: web.Duration(time.Second * 1), + ClientConfig: web.ClientConfig{ + Timeout: confopt.Duration(time.Second * 1), }, }, }, @@ -40,8 +42,8 @@ func New() *Rspamd { } 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 Rspamd struct { @@ -59,14 +61,12 @@ func (r *Rspamd) Configuration() any { func (r *Rspamd) Init() error { if r.URL == "" { - r.Error("URL not set") - return errors.New("url not set") + return errors.New("config: url not set") } - client, err := web.NewHTTPClient(r.Client) + client, err := web.NewHTTPClient(r.ClientConfig) if err != nil { - r.Error(err) - return err + return fmt.Errorf("create http client: %v", err) } r.httpClient = client @@ -79,7 +79,6 @@ func (r *Rspamd) Init() error { func (r *Rspamd) Check() error { mx, err := r.collect() if err != nil { - r.Error(err) return err } diff --git a/src/go/plugin/go.d/modules/rspamd/rspamd_test.go b/src/go/plugin/go.d/modules/rspamd/rspamd_test.go index 0c8cc8e5..a05658ea 100644 --- a/src/go/plugin/go.d/modules/rspamd/rspamd_test.go +++ b/src/go/plugin/go.d/modules/rspamd/rspamd_test.go @@ -48,8 +48,8 @@ func TestRspamd_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: ""}, }, }, }, @@ -156,29 +156,14 @@ func TestRspamd_Collect(t *testing.T) { mx := rsp.Collect() require.Equal(t, test.wantMetrics, mx) + if len(test.wantMetrics) > 0 { - testMetricsHasAllChartsDims(t, rsp, mx) + module.TestMetricsHasAllChartsDims(t, rsp.Charts(), mx) } }) } } -func testMetricsHasAllChartsDims(t *testing.T, rsp *Rspamd, mx map[string]int64) { - for _, chart := range *rsp.Charts() { - if chart.Obsolete { - continue - } - for _, dim := range chart.Dims { - _, ok := mx[dim.ID] - assert.Truef(t, ok, "collected metrics has no data for dim '%s' chart '%s'", dim.ID, chart.ID) - } - for _, v := range chart.Vars { - _, ok := mx[v.ID] - assert.Truef(t, ok, "collected metrics has no data for var '%s' chart '%s'", v.ID, chart.ID) - } - } -} - func prepareCaseOk(t *testing.T) (*Rspamd, func()) { t.Helper() srv := httptest.NewServer(http.HandlerFunc( |