Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.10](backport #36586) [elasticsearch] Add the local query parameter when fetching cluster state #36656

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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