Skip to content

Commit

Permalink
[elasticsearch] Add the local query parameter when fetching cluster s…
Browse files Browse the repository at this point in the history
…tate (#36586)

* [elasticsearch] Add the local query parameter when fetching cluster state

* Fix lint issues
  • Loading branch information
miltonhultgren authored Sep 22, 2023
1 parent 4ad4a7d commit 2830492
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
28 changes: 18 additions & 10 deletions metricbeat/module/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,20 @@ 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) {
// 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
}
Expand All @@ -190,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
}
Expand Down Expand Up @@ -236,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 {
Expand Down Expand Up @@ -283,11 +291,11 @@ 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, ",")
}

content, err := fetchPath(http, resetURI, clusterStateURI, "")
content, err := fetchPath(http, resetURI, clusterStateURI, "local=true")
if err != nil {
return nil, err
}
Expand All @@ -310,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)
}
Expand Down Expand Up @@ -451,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
}

Expand Down
2 changes: 1 addition & 1 deletion metricbeat/module/elasticsearch/shard/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2830492

Please sign in to comment.