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

Only request wildcard expansion for hidden indices if supported #20938

Merged
merged 9 commits into from
Sep 3, 2020
Merged
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix resource tags in aws cloudwatch metricset {issue}20326[20326] {pull}20385[20385]
- Fix ec2 disk and network metrics to use Sum statistic method. {pull}20680[20680]
- Fill cloud.account.name with accountID if account alias doesn't exist. {pull}20736[20736]
- The `elasticsearch/index` metricset only requests wildcard expansion for hidden indices if the monitored Elasticsearch cluster supports it. {pull}20938[20938]

*Packetbeat*

Expand Down
22 changes: 14 additions & 8 deletions metricbeat/module/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,23 @@ func NewModule(base mb.BaseModule) (mb.Module, error) {
return elastic.NewModule(&base, xpackEnabledMetricSets, logp.NewLogger(ModuleName))
}

// CCRStatsAPIAvailableVersion is the version of Elasticsearch since when the CCR stats API is available.
var CCRStatsAPIAvailableVersion = common.MustNewVersion("6.5.0")
var (
// CCRStatsAPIAvailableVersion is the version of Elasticsearch since when the CCR stats API is available.
CCRStatsAPIAvailableVersion = common.MustNewVersion("6.5.0")

// EnrichStatsAPIAvailableVersion is the version of Elasticsearch since when the Enrich stats API is available.
EnrichStatsAPIAvailableVersion = common.MustNewVersion("7.5.0")

// EnrichStatsAPIAvailableVersion is the version of Elasticsearch since when the Enrich stats API is available.
var EnrichStatsAPIAvailableVersion = common.MustNewVersion("7.5.0")
// BulkStatsAvailableVersion is the version since when bulk indexing stats are available
BulkStatsAvailableVersion = common.MustNewVersion("8.0.0")

// BulkStatsAvailableVersion is the version since when bulk indexing stats are available
var BulkStatsAvailableVersion = common.MustNewVersion("8.0.0")
//ExpandWildcardsHiddenAvailableVersion is the version since when the "expand_wildcards" query parameter to
// the Indices Stats API can accept "hidden" as a value.
ExpandWildcardsHiddenAvailableVersion = common.MustNewVersion("7.7.0")

// Global clusterIdCache. Assumption is that the same node id never can belong to a different cluster id.
var clusterIDCache = map[string]string{}
// Global clusterIdCache. Assumption is that the same node id never can belong to a different cluster id.
clusterIDCache = map[string]string{}
)

// ModuleName is the name of this module.
const ModuleName = "elasticsearch"
Expand Down
23 changes: 12 additions & 11 deletions metricbeat/module/elasticsearch/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ func init() {
}

const (
statsMetrics = "docs,fielddata,indexing,merge,search,segments,store,refresh,query_cache,request_cache"
statsPath = "/_stats/" + statsMetrics + "?filter_path=indices&expand_wildcards=open,hidden"
statsMetrics = "docs,fielddata,indexing,merge,search,segments,store,refresh,query_cache,request_cache"
expandWildcards = "expand_wildcards=open"
statsPath = "/_stats/" + statsMetrics + "?filter_path=indices&" + expandWildcards

bulkSuffix = ",bulk"
hiddenSuffix = ",hidden"
)

// MetricSet type defines all fields of the MetricSet
Expand Down Expand Up @@ -114,21 +118,18 @@ func (m *MetricSet) updateServicePath(esVersion common.Version) error {

func getServicePath(esVersion common.Version) (string, error) {
currPath := statsPath
if esVersion.LessThan(elasticsearch.BulkStatsAvailableVersion) {
// Can't request bulk stats so don't change service URI
return currPath, nil
}

u, err := url.Parse(currPath)
if err != nil {
return "", err
}

if strings.HasSuffix(u.Path, ",bulk") {
// Bulk stats already being requested so don't change service URI
return currPath, nil
if !esVersion.LessThan(elasticsearch.BulkStatsAvailableVersion) {
u.Path += bulkSuffix
}

if !esVersion.LessThan(elasticsearch.ExpandWildcardsHiddenAvailableVersion) {
u.RawQuery = strings.Replace(u.RawQuery, expandWildcards, expandWildcards+hiddenSuffix, 1)
}

u.Path += ",bulk"
return u.String(), nil
}
22 changes: 18 additions & 4 deletions metricbeat/module/elasticsearch/index/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,29 @@ import (
"github.com/stretchr/testify/require"
)

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

tests := map[string]struct {
esVersion *common.Version
expectedPath string
}{
"bulk_stats_unavailable": {
esVersion: common.MustNewVersion("7.9.0"),
esVersion: common.MustNewVersion("7.6.0"),
expectedPath: statsPath,
},
"bulk_stats_available": {
esVersion: common.MustNewVersion("8.0.0"),
expectedPath: strings.Replace(statsPath, statsMetrics, statsMetrics+",bulk", 1),
expectedPath: path800,
},
"expand_wildcards_hidden_unavailable": {
esVersion: common.MustNewVersion("7.6.0"),
expectedPath: statsPath,
},
"expand_wildcards_hidden_available": {
esVersion: common.MustNewVersion("7.7.0"),
expectedPath: path770,
},
}

Expand All @@ -52,6 +63,9 @@ func TestGetServiceURI(t *testing.T) {
}

func TestGetServiceURIMultipleCalls(t *testing.T) {
path := strings.Replace(statsPath, expandWildcards, expandWildcards+hiddenSuffix, 1)
path = strings.Replace(path, statsMetrics, statsMetrics+bulkSuffix, 1)

err := quick.Check(func(r uint) bool {
numCalls := 2 + (r % 10) // between 2 and 11

Expand All @@ -64,7 +78,7 @@ func TestGetServiceURIMultipleCalls(t *testing.T) {
}
}

return err == nil && uri == strings.Replace(statsPath, statsMetrics, statsMetrics+",bulk", 1)
return err == nil && uri == path
}, nil)
require.NoError(t, err)
}