Skip to content

Commit

Permalink
[elasticsearch] Allow closed indices (elastic#38408)
Browse files Browse the repository at this point in the history
* [elasticsearch] Allow closed indices

* Apply same change to index_summary

* Fix formatting

* Update unit tests
  • Loading branch information
miltonhultgren authored Mar 21, 2024
1 parent 4b9fe7c commit c91eaee
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
6 changes: 4 additions & 2 deletions metricbeat/module/elasticsearch/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ const (
expandWildcards = "expand_wildcards=open"
statsPath = "/_stats/" + statsMetrics + "?filter_path=indices&" + expandWildcards

bulkSuffix = ",bulk"
hiddenSuffix = ",hidden"
bulkSuffix = ",bulk"
hiddenSuffix = ",hidden"
allowClosedIndices = "&forbid_closed_indices=false"
)

// MetricSet type defines all fields of the MetricSet
Expand Down Expand Up @@ -107,6 +108,7 @@ func getServicePath(esVersion version.V) (string, error) {

if !esVersion.LessThan(elasticsearch.BulkStatsAvailableVersion) {
u.Path += bulkSuffix
u.RawQuery += allowClosedIndices
}

if !esVersion.LessThan(elasticsearch.ExpandWildcardsHiddenAvailableVersion) {
Expand Down
3 changes: 2 additions & 1 deletion metricbeat/module/elasticsearch/index/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

func TestGetServiceURIExpectedPath(t *testing.T) {
path770 := strings.Replace(statsPath, expandWildcards, expandWildcards+hiddenSuffix, 1)
path800 := strings.Replace(path770, statsMetrics, statsMetrics+bulkSuffix, 1)
path800 := strings.Replace(path770, statsMetrics, statsMetrics+bulkSuffix, 1) + allowClosedIndices

tests := map[string]struct {
esVersion *version.V
Expand Down Expand Up @@ -65,6 +65,7 @@ func TestGetServiceURIExpectedPath(t *testing.T) {
func TestGetServiceURIMultipleCalls(t *testing.T) {
path := strings.Replace(statsPath, expandWildcards, expandWildcards+hiddenSuffix, 1)
path = strings.Replace(path, statsMetrics, statsMetrics+bulkSuffix, 1)
path += allowClosedIndices

err := quick.Check(func(r uint) bool {
numCalls := 2 + (r % 10) // between 2 and 11
Expand Down
39 changes: 36 additions & 3 deletions metricbeat/module/elasticsearch/index_summary/index_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ package index_summary

import (
"fmt"
"net/url"

"github.com/elastic/beats/v7/metricbeat/mb"
"github.com/elastic/beats/v7/metricbeat/mb/parse"
"github.com/elastic/beats/v7/metricbeat/module/elasticsearch"

"github.com/elastic/elastic-agent-libs/version"
)

// init registers the MetricSet with the central registry.
Expand All @@ -36,6 +39,8 @@ func init() {

const (
statsPath = "/_stats"

allowClosedIndices = "forbid_closed_indices=false"
)

var (
Expand Down Expand Up @@ -70,15 +75,43 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error {
return nil
}

content, err := m.HTTP.FetchContent()
info, err := elasticsearch.GetInfo(m.HTTP, m.HostData().SanitizedURI+statsPath)
if err != nil {
return fmt.Errorf("failed to get info from Elasticsearch: %w", err)
}

if err := m.updateServicePath(*info.Version.Number); err != nil {
return err
}

info, err := elasticsearch.GetInfo(m.HTTP, m.HostData().SanitizedURI+statsPath)
content, err := m.HTTP.FetchContent()
if err != nil {
return fmt.Errorf("failed to get info from Elasticsearch: %w", err)
return err
}

return eventMapping(r, info, content, m.XPackEnabled)
}

func (m *MetricSet) updateServicePath(esVersion version.V) error {
p, err := getServicePath(esVersion)
if err != nil {
return err
}

m.SetServiceURI(p)
return nil
}

func getServicePath(esVersion version.V) (string, error) {
currPath := statsPath
u, err := url.Parse(currPath)
if err != nil {
return "", err
}

if !esVersion.LessThan(elasticsearch.BulkStatsAvailableVersion) {
u.RawQuery += allowClosedIndices
}

return u.String(), nil
}

0 comments on commit c91eaee

Please sign in to comment.