diff options
Diffstat (limited to 'src/go/plugin/go.d/modules/clickhouse')
11 files changed, 35 insertions, 60 deletions
diff --git a/src/go/plugin/go.d/modules/clickhouse/clickhouse.go b/src/go/plugin/go.d/modules/clickhouse/clickhouse.go index 3e34f726..ab37d826 100644 --- a/src/go/plugin/go.d/modules/clickhouse/clickhouse.go +++ b/src/go/plugin/go.d/modules/clickhouse/clickhouse.go @@ -5,10 +5,12 @@ package clickhouse 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() *ClickHouse { return &ClickHouse{ Config: Config{ - HTTP: web.HTTP{ - Request: web.Request{ + HTTPConfig: web.HTTPConfig{ + RequestConfig: web.RequestConfig{ URL: "http://127.0.0.1:8123", }, - Client: web.Client{ - Timeout: web.Duration(time.Second), + ClientConfig: web.ClientConfig{ + Timeout: confopt.Duration(time.Second), }, }, }, @@ -42,8 +44,8 @@ func New() *ClickHouse { } 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 ( @@ -68,14 +70,12 @@ func (c *ClickHouse) Configuration() any { func (c *ClickHouse) 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 @@ -88,7 +88,6 @@ func (c *ClickHouse) Init() error { func (c *ClickHouse) Check() error { mx, err := c.collect() if err != nil { - c.Error(err) return err } diff --git a/src/go/plugin/go.d/modules/clickhouse/clickhouse_test.go b/src/go/plugin/go.d/modules/clickhouse/clickhouse_test.go index c3defbda..29dfdacb 100644 --- a/src/go/plugin/go.d/modules/clickhouse/clickhouse_test.go +++ b/src/go/plugin/go.d/modules/clickhouse/clickhouse_test.go @@ -58,8 +58,8 @@ func TestClickHouse_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: ""}, }, }, }, @@ -243,25 +243,14 @@ func TestClickHouse_Collect(t *testing.T) { mx := click.Collect() require.Equal(t, test.wantMetrics, mx) + if len(test.wantMetrics) > 0 { - testMetricsHasAllChartsDims(t, click, mx) + module.TestMetricsHasAllChartsDims(t, click.Charts(), mx) } }) } } -func testMetricsHasAllChartsDims(t *testing.T, click *ClickHouse, mx map[string]int64) { - for _, chart := range *click.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) - } - } -} - func prepareCaseOk(t *testing.T) (*ClickHouse, func()) { t.Helper() srv := httptest.NewServer(http.HandlerFunc( diff --git a/src/go/plugin/go.d/modules/clickhouse/collect.go b/src/go/plugin/go.d/modules/clickhouse/collect.go index 8bb75652..2000f9e2 100644 --- a/src/go/plugin/go.d/modules/clickhouse/collect.go +++ b/src/go/plugin/go.d/modules/clickhouse/collect.go @@ -9,6 +9,8 @@ import ( "net/http" "net/url" "slices" + + "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web" ) const precision = 1000 @@ -38,18 +40,10 @@ func (c *ClickHouse) collect() (map[string]int64, error) { return mx, nil } -func (c *ClickHouse) doOKDecodeCSV(req *http.Request, assign func(column, value string, lineEnd bool)) error { - resp, err := c.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 readCSVResponseData(resp.Body, assign) +func (c *ClickHouse) doHTTP(req *http.Request, assign func(column, value string, lineEnd bool)) error { + return web.DoHTTP(c.httpClient).Request(req, func(body io.Reader) error { + return readCSVResponseData(body, assign) + }) } func readCSVResponseData(reader io.Reader, assign func(column, value string, lineEnd bool)) error { @@ -87,10 +81,3 @@ func readCSVResponseData(reader io.Reader, assign func(column, value string, lin func makeURLQuery(q string) string { return url.Values{"query": {q}}.Encode() } - -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/clickhouse/collect_system_async_metrics.go b/src/go/plugin/go.d/modules/clickhouse/collect_system_async_metrics.go index 79b7e0ff..4106e98f 100644 --- a/src/go/plugin/go.d/modules/clickhouse/collect_system_async_metrics.go +++ b/src/go/plugin/go.d/modules/clickhouse/collect_system_async_metrics.go @@ -22,7 +22,7 @@ where ` func (c *ClickHouse) collectSystemAsyncMetrics(mx map[string]int64) error { - req, _ := web.NewHTTPRequest(c.Request) + req, _ := web.NewHTTPRequest(c.RequestConfig) req.URL.RawQuery = makeURLQuery(querySystemAsyncMetrics) want := map[string]float64{ @@ -35,7 +35,7 @@ func (c *ClickHouse) collectSystemAsyncMetrics(mx map[string]int64) error { var metric string var n int - err := c.doOKDecodeCSV(req, func(column, value string, lineEnd bool) { + err := c.doHTTP(req, func(column, value string, lineEnd bool) { switch column { case "metric": metric = value diff --git a/src/go/plugin/go.d/modules/clickhouse/collect_system_disks.go b/src/go/plugin/go.d/modules/clickhouse/collect_system_disks.go index 4b9829bf..5c6c5ffe 100644 --- a/src/go/plugin/go.d/modules/clickhouse/collect_system_disks.go +++ b/src/go/plugin/go.d/modules/clickhouse/collect_system_disks.go @@ -26,7 +26,7 @@ type diskStats struct { } func (c *ClickHouse) collectSystemDisks(mx map[string]int64) error { - req, _ := web.NewHTTPRequest(c.Request) + req, _ := web.NewHTTPRequest(c.RequestConfig) req.URL.RawQuery = makeURLQuery(querySystemDisks) seen := make(map[string]*diskStats) @@ -42,7 +42,7 @@ func (c *ClickHouse) collectSystemDisks(mx map[string]int64) error { var name string - err := c.doOKDecodeCSV(req, func(column, value string, lineEnd bool) { + err := c.doHTTP(req, func(column, value string, lineEnd bool) { switch column { case "name": name = value diff --git a/src/go/plugin/go.d/modules/clickhouse/collect_system_events.go b/src/go/plugin/go.d/modules/clickhouse/collect_system_events.go index de3c33a1..6b103eac 100644 --- a/src/go/plugin/go.d/modules/clickhouse/collect_system_events.go +++ b/src/go/plugin/go.d/modules/clickhouse/collect_system_events.go @@ -18,14 +18,14 @@ FROM ` func (c *ClickHouse) collectSystemEvents(mx map[string]int64) error { - req, _ := web.NewHTTPRequest(c.Request) + req, _ := web.NewHTTPRequest(c.RequestConfig) req.URL.RawQuery = makeURLQuery(querySystemEvents) px := "events_" var event string var n int - err := c.doOKDecodeCSV(req, func(column, value string, lineEnd bool) { + err := c.doHTTP(req, func(column, value string, lineEnd bool) { switch column { case "event": event = value diff --git a/src/go/plugin/go.d/modules/clickhouse/collect_system_metrics.go b/src/go/plugin/go.d/modules/clickhouse/collect_system_metrics.go index 26891f80..8bfafe3d 100644 --- a/src/go/plugin/go.d/modules/clickhouse/collect_system_metrics.go +++ b/src/go/plugin/go.d/modules/clickhouse/collect_system_metrics.go @@ -18,14 +18,14 @@ FROM ` func (c *ClickHouse) collectSystemMetrics(mx map[string]int64) error { - req, _ := web.NewHTTPRequest(c.Request) + req, _ := web.NewHTTPRequest(c.RequestConfig) req.URL.RawQuery = makeURLQuery(querySystemMetrics) px := "metrics_" var metric string var n int - err := c.doOKDecodeCSV(req, func(column, value string, lineEnd bool) { + err := c.doHTTP(req, func(column, value string, lineEnd bool) { switch column { case "metric": metric = value diff --git a/src/go/plugin/go.d/modules/clickhouse/collect_system_parts.go b/src/go/plugin/go.d/modules/clickhouse/collect_system_parts.go index 3e9dc6ac..e6058927 100644 --- a/src/go/plugin/go.d/modules/clickhouse/collect_system_parts.go +++ b/src/go/plugin/go.d/modules/clickhouse/collect_system_parts.go @@ -34,7 +34,7 @@ type tableStats struct { } func (c *ClickHouse) collectSystemParts(mx map[string]int64) error { - req, _ := web.NewHTTPRequest(c.Request) + req, _ := web.NewHTTPRequest(c.RequestConfig) req.URL.RawQuery = makeURLQuery(querySystemParts) seen := make(map[string]*tableStats) @@ -51,7 +51,7 @@ func (c *ClickHouse) collectSystemParts(mx map[string]int64) error { var database, table string - err := c.doOKDecodeCSV(req, func(column, value string, lineEnd bool) { + err := c.doHTTP(req, func(column, value string, lineEnd bool) { switch column { case "database": database = value diff --git a/src/go/plugin/go.d/modules/clickhouse/collect_system_processes.go b/src/go/plugin/go.d/modules/clickhouse/collect_system_processes.go index 53698ea6..cf3ec185 100644 --- a/src/go/plugin/go.d/modules/clickhouse/collect_system_processes.go +++ b/src/go/plugin/go.d/modules/clickhouse/collect_system_processes.go @@ -16,10 +16,10 @@ FROM ` func (c *ClickHouse) collectLongestRunningQueryTime(mx map[string]int64) error { - req, _ := web.NewHTTPRequest(c.Request) + req, _ := web.NewHTTPRequest(c.RequestConfig) req.URL.RawQuery = makeURLQuery(queryLongestQueryTime) - return c.doOKDecodeCSV(req, func(column, value string, lineEnd bool) { + return c.doHTTP(req, func(column, value string, lineEnd bool) { if column == "value" { if v, err := strconv.ParseFloat(value, 64); err == nil { mx["LongestRunningQueryTime"] = int64(v * precision) diff --git a/src/go/plugin/go.d/modules/clickhouse/init.go b/src/go/plugin/go.d/modules/clickhouse/init.go index 4b8ce3e4..4d73129b 100644 --- a/src/go/plugin/go.d/modules/clickhouse/init.go +++ b/src/go/plugin/go.d/modules/clickhouse/init.go @@ -17,5 +17,5 @@ func (c *ClickHouse) validateConfig() error { } func (c *ClickHouse) initHTTPClient() (*http.Client, error) { - return web.NewHTTPClient(c.Client) + return web.NewHTTPClient(c.ClientConfig) } diff --git a/src/go/plugin/go.d/modules/clickhouse/integrations/clickhouse.md b/src/go/plugin/go.d/modules/clickhouse/integrations/clickhouse.md index c4f1384c..3e0ada21 100644 --- a/src/go/plugin/go.d/modules/clickhouse/integrations/clickhouse.md +++ b/src/go/plugin/go.d/modules/clickhouse/integrations/clickhouse.md @@ -203,8 +203,8 @@ No action required. The configuration file name for this integration is `go.d/clickhouse.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 |