-
Couldn't load subscription status.
- Fork 87
PMM-4145 Add RDS disable metric collection. #41
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
Changes from all commits
27d52f3
1dcf70a
71ceece
29fd4c2
2c98741
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| package basic | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sort" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/percona/exporter_shared/helpers" | ||
|
|
@@ -46,3 +48,45 @@ func TestCollector(t *testing.T) { | |
| assert.Equal(t, expectedLines, actualLines) | ||
| assert.Equal(t, expectedMetrics, actualMetrics) | ||
| } | ||
|
|
||
| func TestCollectorDisableBasicMetrics(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. func |
||
| cfg, err := config.Load("../config.tests.yml") | ||
| require.NoError(t, err) | ||
| client := client.New() | ||
|
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. assignments should only be cuddled with other assignments (from |
||
| instanceGroups := make(map[bool][]string, 2) | ||
| for i := range cfg.Instances { | ||
|
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. only one cuddle assignment allowed before range statement (from |
||
| // Disable basic metrics in even instances. | ||
| // This disable instance: no-such-instance. | ||
| isDisabled := i%2 == 0 | ||
|
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. mnd: Magic number: 2, in detected (from |
||
| cfg.Instances[i].DisableBasicMetrics = isDisabled | ||
| // Groups instance names by disabled or enabled metrics. | ||
| instanceGroups[isDisabled] = append(instanceGroups[isDisabled], cfg.Instances[i].Instance) | ||
| } | ||
| sess, err := sessions.New(cfg.Instances, client.HTTP(), false) | ||
|
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. assignments should only be cuddled with other assignments (from |
||
| require.NoError(t, err) | ||
|
|
||
| c := New(cfg, sess) | ||
|
|
||
| actualMetrics := helpers.ReadMetrics(helpers.CollectMetrics(c)) | ||
| actualLines := helpers.Format(helpers.WriteMetrics(actualMetrics)) | ||
|
|
||
| // Check if all collected metrics do not contain metrics for instance whare disabled metrics. | ||
| hasMetricForInstance := func(lines []string, instanceName string) bool { | ||
| for _, line := range lines { | ||
| if strings.Contains(line, fmt.Sprintf("instance=%q", instanceName)) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
|
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. return statements should not be cuddled if block has more than two lines (from |
||
| } | ||
|
|
||
| // Scans if metrics contain a metric for the disabled instance (DisableBasicMetrics = true). | ||
| for _, inst := range instanceGroups[true] { | ||
| assert.Falsef(t, hasMetricForInstance(actualLines, inst), "Found metrics for disabled instance %s", inst) | ||
| } | ||
|
|
||
| // Scans if metrics contain a metric for the enabled instance (DisableBasicMetrics = false). | ||
| for _, inst := range instanceGroups[false] { | ||
| assert.Truef(t, hasMetricForInstance(actualLines, inst), "Did not find metrics for enabled instance %s", inst) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -135,3 +135,47 @@ func TestBetterTimes(t *testing.T) { | |
| assert.Equal(t, td.expectedNextStartTime, nextStartTime) | ||
| } | ||
| } | ||
|
|
||
| func TestScraperDisableEnhancedMetrics(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. func |
||
| cfg, err := config.Load("../config.tests.yml") | ||
| require.NoError(t, err) | ||
| client := client.New() | ||
|
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. assignments should only be cuddled with other assignments (from |
||
| for i := range cfg.Instances { | ||
|
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. only one cuddle assignment allowed before range statement (from |
||
| // Disable enhanced metrics in even instances. | ||
| // This disable instance: no-such-instance. | ||
| isDisabled := i%2 == 0 | ||
|
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. mnd: Magic number: 2, in detected (from |
||
| cfg.Instances[i].DisableEnhancedMetrics = isDisabled | ||
| } | ||
| sess, err := sessions.New(cfg.Instances, client.HTTP(), false) | ||
| require.NoError(t, err) | ||
|
|
||
| // Check if all collected metrics do not contain metrics for instance with disabled metrics. | ||
| hasMetricForInstance := func(lines []string, instanceName string) bool { | ||
| for _, line := range lines { | ||
| if strings.Contains(line, fmt.Sprintf("instance=%q", instanceName)) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
|
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. return statements should not be cuddled if block has more than two lines (from |
||
| } | ||
|
|
||
| for session, instances := range sess.AllSessions() { | ||
| session, instances := session, instances | ||
| t.Run(fmt.Sprint(instances), func(t *testing.T) { | ||
| s := newScraper(session, instances) | ||
| s.testDisallowUnknownFields = true | ||
| metrics, _ := s.scrape(context.Background()) | ||
|
|
||
| for _, instance := range instances { | ||
| actualMetrics := helpers.ReadMetrics(metrics[instance.ResourceID]) | ||
| actualLines := helpers.Format(helpers.WriteMetrics(actualMetrics)) | ||
| name := instance.Instance | ||
| if instance.DisableEnhancedMetrics { | ||
| assert.Falsef(t, hasMetricForInstance(actualLines, name), "Found metrics for disabled instance %s", name) | ||
| continue | ||
| } | ||
| assert.Truef(t, hasMetricForInstance(actualLines, name), "Did not find metrics for enabled instance %s", name) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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.
only cuddled expressions if assigning variable or using from line above (from
wsl)