-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[Metricbeat/Kibana/stats] Enforce exclude_usage=true
#22732
Merged
afharo
merged 5 commits into
elastic:master
from
afharo:metricbeat/kibana/stats/exclude_usage_removal
Nov 25, 2020
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
06ff260
[Metricbeat/Kibana/stats] `exclude_usage=true` by default
afharo e49f7ec
Add CHANGELOG
afharo cc68bbb
Merge branch 'master' into metricbeat/kibana/stats/exclude_usage_removal
afharo 6a17c2b
Fix comment
afharo 40de116
Merge branch 'master' of github.com:elastic/beats into metricbeat/kib…
afharo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,15 +23,14 @@ import ( | |
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" | ||
"github.com/elastic/beats/v7/metricbeat/module/kibana/mtest" | ||
) | ||
|
||
func TestFetchUsage(t *testing.T) { | ||
func TestFetchExcludeUsage(t *testing.T) { | ||
// Spin up mock Kibana server | ||
numStatsRequests := 0 | ||
kib := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
@@ -45,17 +44,15 @@ func TestFetchUsage(t *testing.T) { | |
// Make GET /api/stats return 503 for first call, 200 for subsequent calls | ||
switch numStatsRequests { | ||
case 0: // first call | ||
require.Equal(t, "false", excludeUsage) | ||
require.Equal(t, "true", excludeUsage) // exclude_usage is always true | ||
w.WriteHeader(503) | ||
|
||
case 1: // second call | ||
// Make sure exclude_usage is true since first call failed and it should not try again until usageCollectionBackoff time has passed | ||
require.Equal(t, "true", excludeUsage) | ||
require.Equal(t, "true", excludeUsage) // exclude_usage is always true | ||
w.WriteHeader(200) | ||
|
||
case 2: // third call | ||
// Make sure exclude_usage is still true | ||
require.Equal(t, "true", excludeUsage) | ||
require.Equal(t, "true", excludeUsage) // exclude_usage is always true | ||
w.WriteHeader(200) | ||
} | ||
|
||
|
@@ -78,39 +75,25 @@ func TestFetchUsage(t *testing.T) { | |
mbtest.ReportingFetchV2Error(f) | ||
} | ||
|
||
func TestShouldCollectUsage(t *testing.T) { | ||
now := time.Now() | ||
|
||
cases := map[string]struct { | ||
usageLastCollectedOn time.Time | ||
usageNextCollectOn time.Time | ||
expectedResult bool | ||
}{ | ||
"within_usage_collection_period": { | ||
usageLastCollectedOn: now.Add(-1 * usageCollectionPeriod), | ||
expectedResult: false, | ||
}, | ||
"after_usage_collection_period_but_before_next_scheduled_collection": { | ||
usageLastCollectedOn: now.Add(-2 * usageCollectionPeriod), | ||
usageNextCollectOn: now.Add(3 * time.Hour), | ||
expectedResult: false, | ||
}, | ||
"after_usage_collection_period_and_after_next_scheduled_collection": { | ||
usageLastCollectedOn: now.Add(-2 * usageCollectionPeriod), | ||
usageNextCollectOn: now.Add(-1 * time.Hour), | ||
expectedResult: true, | ||
}, | ||
} | ||
|
||
for name, test := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
m := MetricSet{ | ||
usageLastCollectedOn: test.usageLastCollectedOn, | ||
usageNextCollectOn: test.usageNextCollectOn, | ||
} | ||
func TestFetchNoExcludeUsage(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for adding this test. 👍 |
||
// Spin up mock Kibana server | ||
kib := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
switch r.URL.Path { | ||
case "/api/status": | ||
w.Write([]byte("{ \"version\": { \"number\": \"7.0.0\" }}")) // v7.0.0 does not support exclude_usage and should not be sent | ||
|
||
actualResult := m.shouldCollectUsage(now) | ||
require.Equal(t, test.expectedResult, actualResult) | ||
}) | ||
} | ||
case "/api/stats": | ||
excludeUsage := r.FormValue("exclude_usage") | ||
require.Empty(t, excludeUsage) // exclude_usage should not be provided | ||
w.WriteHeader(200) | ||
} | ||
})) | ||
defer kib.Close() | ||
|
||
config := mtest.GetConfig("stats", kib.URL, true) | ||
|
||
f := mbtest.NewReportingMetricSetV2Error(t, config) | ||
|
||
// First fetch | ||
mbtest.ReportingFetchV2Error(f) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this change we no longer need the comment above
if m.isUsageExcludable
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well spotted! I updated it to explain what's actually happening now :)
Thank you!