summaryrefslogtreecommitdiffstats
path: root/src/go/plugin/go.d/modules/elasticsearch
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-11-09 08:36:07 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-11-09 08:36:07 +0000
commite8c44275b9a1937b5948010a042294d580d36d7c (patch)
treee87c73e25556c3c9d5442f5ca4ba0cf46c64ec70 /src/go/plugin/go.d/modules/elasticsearch
parentAdding upstream version 1.47.5. (diff)
downloadnetdata-upstream.tar.xz
netdata-upstream.zip
Adding upstream version 2.0.0.upstream/2.0.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/go/plugin/go.d/modules/elasticsearch')
-rw-r--r--src/go/plugin/go.d/modules/elasticsearch/collect.go61
-rw-r--r--src/go/plugin/go.d/modules/elasticsearch/elasticsearch.go19
-rw-r--r--src/go/plugin/go.d/modules/elasticsearch/elasticsearch_test.go51
-rw-r--r--src/go/plugin/go.d/modules/elasticsearch/init.go4
-rw-r--r--src/go/plugin/go.d/modules/elasticsearch/integrations/elasticsearch.md4
-rw-r--r--src/go/plugin/go.d/modules/elasticsearch/integrations/opensearch.md4
6 files changed, 41 insertions, 102 deletions
diff --git a/src/go/plugin/go.d/modules/elasticsearch/collect.go b/src/go/plugin/go.d/modules/elasticsearch/collect.go
index 4f46f108..561ba83b 100644
--- a/src/go/plugin/go.d/modules/elasticsearch/collect.go
+++ b/src/go/plugin/go.d/modules/elasticsearch/collect.go
@@ -3,12 +3,10 @@
package elasticsearch
import (
- "encoding/json"
"errors"
"fmt"
- "io"
"math"
- "net/http"
+ "slices"
"strconv"
"strings"
"sync"
@@ -165,10 +163,10 @@ func (es *Elasticsearch) scrapeNodesStats(ms *esMetrics) {
p = urlPathLocalNodeStats
}
- req, _ := web.NewHTTPRequestWithPath(es.Request, p)
+ req, _ := web.NewHTTPRequestWithPath(es.RequestConfig, p)
var stats esNodesStats
- if err := es.doOKDecode(req, &stats); err != nil {
+ if err := web.DoHTTP(es.httpClient).RequestJSON(req, &stats); err != nil {
es.Warning(err)
return
}
@@ -177,10 +175,10 @@ func (es *Elasticsearch) scrapeNodesStats(ms *esMetrics) {
}
func (es *Elasticsearch) scrapeClusterHealth(ms *esMetrics) {
- req, _ := web.NewHTTPRequestWithPath(es.Request, urlPathClusterHealth)
+ req, _ := web.NewHTTPRequestWithPath(es.RequestConfig, urlPathClusterHealth)
var health esClusterHealth
- if err := es.doOKDecode(req, &health); err != nil {
+ if err := web.DoHTTP(es.httpClient).RequestJSON(req, &health); err != nil {
es.Warning(err)
return
}
@@ -189,10 +187,10 @@ func (es *Elasticsearch) scrapeClusterHealth(ms *esMetrics) {
}
func (es *Elasticsearch) scrapeClusterStats(ms *esMetrics) {
- req, _ := web.NewHTTPRequestWithPath(es.Request, urlPathClusterStats)
+ req, _ := web.NewHTTPRequestWithPath(es.RequestConfig, urlPathClusterStats)
var stats esClusterStats
- if err := es.doOKDecode(req, &stats); err != nil {
+ if err := web.DoHTTP(es.httpClient).RequestJSON(req, &stats); err != nil {
es.Warning(err)
return
}
@@ -201,11 +199,11 @@ func (es *Elasticsearch) scrapeClusterStats(ms *esMetrics) {
}
func (es *Elasticsearch) scrapeLocalIndicesStats(ms *esMetrics) {
- req, _ := web.NewHTTPRequestWithPath(es.Request, urlPathIndicesStats)
+ req, _ := web.NewHTTPRequestWithPath(es.RequestConfig, urlPathIndicesStats)
req.URL.RawQuery = "local=true&format=json"
var stats []esIndexStats
- if err := es.doOKDecode(req, &stats); err != nil {
+ if err := web.DoHTTP(es.httpClient).RequestJSON(req, &stats); err != nil {
es.Warning(err)
return
}
@@ -214,13 +212,12 @@ func (es *Elasticsearch) scrapeLocalIndicesStats(ms *esMetrics) {
}
func (es *Elasticsearch) getClusterName() (string, error) {
- req, _ := web.NewHTTPRequest(es.Request)
+ req, _ := web.NewHTTPRequest(es.RequestConfig)
var info struct {
ClusterName string `json:"cluster_name"`
}
-
- if err := es.doOKDecode(req, &info); err != nil {
+ if err := web.DoHTTP(es.httpClient).RequestJSON(req, &info); err != nil {
return "", err
}
@@ -231,30 +228,6 @@ func (es *Elasticsearch) getClusterName() (string, error) {
return info.ClusterName, nil
}
-func (es *Elasticsearch) doOKDecode(req *http.Request, in interface{}) error {
- resp, err := es.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()
- }
-}
-
func convertIndexStoreSizeToBytes(size string) int64 {
var num float64
switch {
@@ -289,15 +262,9 @@ func boolToInt(v bool) int64 {
}
func removeSystemIndices(indices []esIndexStats) []esIndexStats {
- var i int
- for _, index := range indices {
- if strings.HasPrefix(index.Index, ".") {
- continue
- }
- indices[i] = index
- i++
- }
- return indices[:i]
+ return slices.DeleteFunc(indices, func(stats esIndexStats) bool {
+ return strings.HasPrefix(stats.Index, ".")
+ })
}
func merge(dst, src map[string]int64, prefix string) {
diff --git a/src/go/plugin/go.d/modules/elasticsearch/elasticsearch.go b/src/go/plugin/go.d/modules/elasticsearch/elasticsearch.go
index 22280f2d..7ff1cfcf 100644
--- a/src/go/plugin/go.d/modules/elasticsearch/elasticsearch.go
+++ b/src/go/plugin/go.d/modules/elasticsearch/elasticsearch.go
@@ -5,11 +5,13 @@ package elasticsearch
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/web"
)
@@ -30,12 +32,12 @@ func init() {
func New() *Elasticsearch {
return &Elasticsearch{
Config: Config{
- HTTP: web.HTTP{
- Request: web.Request{
+ HTTPConfig: web.HTTPConfig{
+ RequestConfig: web.RequestConfig{
URL: "http://127.0.0.1:9200",
},
- Client: web.Client{
- Timeout: web.Duration(time.Second * 2),
+ ClientConfig: web.ClientConfig{
+ Timeout: confopt.Duration(time.Second * 2),
},
},
ClusterMode: false,
@@ -56,7 +58,7 @@ func New() *Elasticsearch {
type Config struct {
UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
- web.HTTP `yaml:",inline" json:""`
+ web.HTTPConfig `yaml:",inline" json:""`
ClusterMode bool `yaml:"cluster_mode" json:"cluster_mode"`
DoNodeStats bool `yaml:"collect_node_stats" json:"collect_node_stats"`
DoClusterHealth bool `yaml:"collect_cluster_health" json:"collect_cluster_health"`
@@ -86,14 +88,12 @@ func (es *Elasticsearch) Configuration() any {
func (es *Elasticsearch) Init() error {
err := es.validateConfig()
if err != nil {
- es.Errorf("check configuration: %v", err)
- return err
+ return fmt.Errorf("check configuration: %v", err)
}
httpClient, err := es.initHTTPClient()
if err != nil {
- es.Errorf("init HTTP client: %v", err)
- return err
+ return fmt.Errorf("init HTTP client: %v", err)
}
es.httpClient = httpClient
@@ -103,7 +103,6 @@ func (es *Elasticsearch) Init() error {
func (es *Elasticsearch) Check() error {
mx, err := es.collect()
if err != nil {
- es.Error(err)
return err
}
if len(mx) == 0 {
diff --git a/src/go/plugin/go.d/modules/elasticsearch/elasticsearch_test.go b/src/go/plugin/go.d/modules/elasticsearch/elasticsearch_test.go
index ca3aa526..315b46db 100644
--- a/src/go/plugin/go.d/modules/elasticsearch/elasticsearch_test.go
+++ b/src/go/plugin/go.d/modules/elasticsearch/elasticsearch_test.go
@@ -3,12 +3,12 @@
package elasticsearch
import (
- "github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
"net/http"
"net/http/httptest"
"os"
"testing"
+ "github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/tlscfg"
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web"
@@ -57,8 +57,8 @@ func TestElasticsearch_Init(t *testing.T) {
},
"all stats": {
config: Config{
- HTTP: web.HTTP{
- Request: web.Request{URL: "http://127.0.0.1:38001"},
+ HTTPConfig: web.HTTPConfig{
+ RequestConfig: web.RequestConfig{URL: "http://127.0.0.1:38001"},
},
DoNodeStats: true,
DoClusterHealth: true,
@@ -68,8 +68,8 @@ func TestElasticsearch_Init(t *testing.T) {
},
"only node_stats": {
config: Config{
- HTTP: web.HTTP{
- Request: web.Request{URL: "http://127.0.0.1:38001"},
+ HTTPConfig: web.HTTPConfig{
+ RequestConfig: web.RequestConfig{URL: "http://127.0.0.1:38001"},
},
DoNodeStats: true,
DoClusterHealth: false,
@@ -80,15 +80,15 @@ func TestElasticsearch_Init(t *testing.T) {
"URL not set": {
wantFail: true,
config: Config{
- HTTP: web.HTTP{
- Request: web.Request{URL: ""},
+ HTTPConfig: web.HTTPConfig{
+ RequestConfig: web.RequestConfig{URL: ""},
}},
},
"invalid TLSCA": {
wantFail: true,
config: Config{
- HTTP: web.HTTP{
- Client: web.Client{
+ HTTPConfig: web.HTTPConfig{
+ ClientConfig: web.ClientConfig{
TLSConfig: tlscfg.TLSConfig{TLSCA: "testdata/tls"},
},
}},
@@ -96,8 +96,8 @@ func TestElasticsearch_Init(t *testing.T) {
"all API calls are disabled": {
wantFail: true,
config: Config{
- HTTP: web.HTTP{
- Request: web.Request{URL: "http://127.0.0.1:38001"},
+ HTTPConfig: web.HTTPConfig{
+ RequestConfig: web.RequestConfig{URL: "http://127.0.0.1:38001"},
},
DoNodeStats: false,
DoClusterHealth: false,
@@ -636,40 +636,13 @@ func TestElasticsearch_Collect(t *testing.T) {
mx = es.Collect()
}
- //m := mx
- //l := make([]string, 0)
- //for k := range m {
- // l = append(l, k)
- //}
- //sort.Strings(l)
- //for _, value := range l {
- // fmt.Println(fmt.Sprintf("\"%s\": %d,", value, m[value]))
- //}
- //return
-
assert.Equal(t, test.wantCollected, mx)
assert.Len(t, *es.Charts(), test.wantCharts)
- ensureCollectedHasAllChartsDimsVarsIDs(t, es, mx)
+ module.TestMetricsHasAllChartsDims(t, es.Charts(), mx)
})
}
}
-func ensureCollectedHasAllChartsDimsVarsIDs(t *testing.T, es *Elasticsearch, collected map[string]int64) {
- for _, chart := range *es.Charts() {
- if chart.Obsolete {
- continue
- }
- for _, dim := range chart.Dims {
- _, ok := collected[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 := collected[v.ID]
- assert.Truef(t, ok, "collected metrics has no data for var '%s' chart '%s'", v.ID, chart.ID)
- }
- }
-}
-
func prepareElasticsearch(t *testing.T, createES func() *Elasticsearch) (es *Elasticsearch, cleanup func()) {
t.Helper()
srv := prepareElasticsearchEndpoint()
diff --git a/src/go/plugin/go.d/modules/elasticsearch/init.go b/src/go/plugin/go.d/modules/elasticsearch/init.go
index f87b594f..6de37551 100644
--- a/src/go/plugin/go.d/modules/elasticsearch/init.go
+++ b/src/go/plugin/go.d/modules/elasticsearch/init.go
@@ -16,12 +16,12 @@ func (es *Elasticsearch) validateConfig() error {
if !(es.DoNodeStats || es.DoClusterHealth || es.DoClusterStats || es.DoIndicesStats) {
return errors.New("all API calls are disabled")
}
- if _, err := web.NewHTTPRequest(es.Request); err != nil {
+ if _, err := web.NewHTTPRequest(es.RequestConfig); err != nil {
return err
}
return nil
}
func (es *Elasticsearch) initHTTPClient() (*http.Client, error) {
- return web.NewHTTPClient(es.Client)
+ return web.NewHTTPClient(es.ClientConfig)
}
diff --git a/src/go/plugin/go.d/modules/elasticsearch/integrations/elasticsearch.md b/src/go/plugin/go.d/modules/elasticsearch/integrations/elasticsearch.md
index ab6f7d00..79dde297 100644
--- a/src/go/plugin/go.d/modules/elasticsearch/integrations/elasticsearch.md
+++ b/src/go/plugin/go.d/modules/elasticsearch/integrations/elasticsearch.md
@@ -193,8 +193,8 @@ No action required.
The configuration file name for this integration is `go.d/elasticsearch.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/elasticsearch/integrations/opensearch.md b/src/go/plugin/go.d/modules/elasticsearch/integrations/opensearch.md
index 9426ada7..46e3ddf1 100644
--- a/src/go/plugin/go.d/modules/elasticsearch/integrations/opensearch.md
+++ b/src/go/plugin/go.d/modules/elasticsearch/integrations/opensearch.md
@@ -193,8 +193,8 @@ No action required.
The configuration file name for this integration is `go.d/elasticsearch.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