From 7912fed16f0079c69b7db68bb6726438a8feda2d Mon Sep 17 00:00:00 2001 From: Milton Hultgren Date: Wed, 13 Sep 2023 21:42:31 +0200 Subject: [PATCH 1/2] [elasticsearch] Add the local query parameter when fetching cluster state --- metricbeat/module/elasticsearch/elasticsearch.go | 5 ++--- metricbeat/module/elasticsearch/shard/shard.go | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/metricbeat/module/elasticsearch/elasticsearch.go b/metricbeat/module/elasticsearch/elasticsearch.go index 00737472f2c..fab60bdde3b 100644 --- a/metricbeat/module/elasticsearch/elasticsearch.go +++ b/metricbeat/module/elasticsearch/elasticsearch.go @@ -180,8 +180,7 @@ func getNodeName(http *helper.HTTP, uri string) (string, error) { } func getMasterName(http *helper.HTTP, uri string) (string, error) { - // TODO: evaluate on why when run with ?local=true request does not contain master_node field - content, err := fetchPath(http, uri, "_cluster/state/master_node", "") + content, err := fetchPath(http, uri, "_cluster/state/master_node", "local=true") if err != nil { return "", err } @@ -287,7 +286,7 @@ func GetClusterState(http *helper.HTTP, resetURI string, metrics []string) (maps clusterStateURI += "/" + strings.Join(metrics, ",") } - content, err := fetchPath(http, resetURI, clusterStateURI, "") + content, err := fetchPath(http, resetURI, clusterStateURI, "local=true") if err != nil { return nil, err } diff --git a/metricbeat/module/elasticsearch/shard/shard.go b/metricbeat/module/elasticsearch/shard/shard.go index 63650d55596..fe0094ad8a5 100644 --- a/metricbeat/module/elasticsearch/shard/shard.go +++ b/metricbeat/module/elasticsearch/shard/shard.go @@ -31,7 +31,7 @@ func init() { } const ( - statePath = "/_cluster/state/version,nodes,master_node,routing_table" + statePath = "/_cluster/state/version,nodes,master_node,routing_table?local=true" ) // MetricSet type defines all fields of the MetricSet From 9fa8d2b03f2a7ea5e059ead71a93d3218e8fdc00 Mon Sep 17 00:00:00 2001 From: Milton Hultgren Date: Thu, 14 Sep 2023 10:09:12 +0200 Subject: [PATCH 2/2] Fix lint issues --- .../module/elasticsearch/elasticsearch.go | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/metricbeat/module/elasticsearch/elasticsearch.go b/metricbeat/module/elasticsearch/elasticsearch.go index fab60bdde3b..0bf7aa5b532 100644 --- a/metricbeat/module/elasticsearch/elasticsearch.go +++ b/metricbeat/module/elasticsearch/elasticsearch.go @@ -170,13 +170,16 @@ func getNodeName(http *helper.HTTP, uri string) (string, error) { Nodes map[string]interface{} `json:"nodes"` }{} - json.Unmarshal(content, &nodesStruct) + err = json.Unmarshal(content, &nodesStruct) + if err != nil { + return "", err + } // _local will only fetch one node info. First entry is node name for k := range nodesStruct.Nodes { return k, nil } - return "", fmt.Errorf("No local node found") + return "", fmt.Errorf("no local node found") } func getMasterName(http *helper.HTTP, uri string) (string, error) { @@ -189,7 +192,10 @@ func getMasterName(http *helper.HTTP, uri string) (string, error) { MasterNode string `json:"master_node"` }{} - json.Unmarshal(content, &clusterStruct) + err = json.Unmarshal(content, &clusterStruct) + if err != nil { + return "", err + } return clusterStruct.MasterNode, nil } @@ -235,7 +241,10 @@ func GetNodeInfo(http *helper.HTTP, uri string, nodeID string) (*NodeInfo, error Nodes map[string]*NodeInfo `json:"nodes"` }{} - json.Unmarshal(content, &nodesStruct) + err = json.Unmarshal(content, &nodesStruct) + if err != nil { + return nil, err + } // _local will only fetch one node info. First entry is node name for k, v := range nodesStruct.Nodes { @@ -282,7 +291,7 @@ func GetLicense(http *helper.HTTP, resetURI string) (*License, error) { // GetClusterState returns cluster state information. func GetClusterState(http *helper.HTTP, resetURI string, metrics []string) (mapstr.M, error) { clusterStateURI := "_cluster/state" - if metrics != nil && len(metrics) > 0 { + if len(metrics) > 0 { clusterStateURI += "/" + strings.Join(metrics, ",") } @@ -309,7 +318,7 @@ func GetClusterSettings(http *helper.HTTP, resetURI string, includeDefaults bool queryParams = append(queryParams, "include_defaults=true") } - if filterPaths != nil && len(filterPaths) > 0 { + if len(filterPaths) > 0 { filterPathQueryParam := "filter_path=" + strings.Join(filterPaths, ",") queryParams = append(queryParams, filterPathQueryParam) } @@ -450,7 +459,7 @@ func GetMasterNodeID(http *helper.HTTP, resetURI string) (string, error) { return "", err } - for nodeID, _ := range response.Nodes { + for nodeID := range response.Nodes { return nodeID, nil }