Skip to content
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ instances:
instance: rds-mysql57
aws_access_key: AKIAIOSFODNN7EXAMPLE
aws_secret_key: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
disable_basic_metrics: true
disable_enhanced_metrics: false
labels:
foo: bar
baz: qux
Expand Down
6 changes: 5 additions & 1 deletion basic/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@ func (e *Collector) collect(ch chan<- prometheus.Metric) {
var wg sync.WaitGroup
defer wg.Wait()

wg.Add(len(e.config.Instances))
for _, instance := range e.config.Instances {
if instance.DisableBasicMetrics {
e.l.Debugf("Instance %s has disabled basic metrics, skipping.", instance)
continue
}
instance := instance
wg.Add(1)

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)

go func() {
defer wg.Done()

Expand Down
44 changes: 44 additions & 0 deletions basic/collector_test.go
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"
Expand Down Expand Up @@ -46,3 +48,45 @@ func TestCollector(t *testing.T) {
assert.Equal(t, expectedLines, actualLines)
assert.Equal(t, expectedMetrics, actualMetrics)
}

func TestCollectorDisableBasicMetrics(t *testing.T) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func TestCollectorDisableBasicMetrics is unused (from unused)

cfg, err := config.Load("../config.tests.yml")
require.NoError(t, err)
client := client.New()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assignments should only be cuddled with other assignments (from wsl)

instanceGroups := make(map[bool][]string, 2)
for i := range cfg.Instances {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only one cuddle assignment allowed before range statement (from wsl)

// Disable basic metrics in even instances.
// This disable instance: no-such-instance.
isDisabled := i%2 == 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mnd: Magic number: 2, in detected (from gomnd)

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assignments should only be cuddled with other assignments (from wsl)

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

Choose a reason for hiding this comment

The 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 wsl)

}

// 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)
}
}
12 changes: 7 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ import (

// Instance represents a single RDS information from configuration file.
type Instance struct {
Region string `yaml:"region"`
Instance string `yaml:"instance"`
AWSAccessKey string `yaml:"aws_access_key"` // may be empty
AWSSecretKey string `yaml:"aws_secret_key"` // may be empty
Labels map[string]string `yaml:"labels"` // may be empty
Region string `yaml:"region"`
Instance string `yaml:"instance"`
AWSAccessKey string `yaml:"aws_access_key"` // may be empty
AWSSecretKey string `yaml:"aws_secret_key"` // may be empty
DisableBasicMetrics bool `yaml:"disable_basic_metrics"`
DisableEnhancedMetrics bool `yaml:"disable_enhanced_metrics"`
Labels map[string]string `yaml:"labels"` // may be empty

// TODO Type InstanceType `yaml:"type"` // may be empty for old pmm-managed
}
Expand Down
5 changes: 5 additions & 0 deletions enhanced/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func (s *scraper) scrape(ctx context.Context) (map[string][]prometheus.Metric, m
l.Errorf("Failed to find instance.")
continue
}

if instance.DisableEnhancedMetrics {
l.Debugf("Enhanced Metrics are dissabled for instance %v.", instance)
continue
}
l = l.With("region", instance.Region).With("instance", instance.Instance)

// l.Debugf("Message:\n%s", *event.Message)
Expand Down
44 changes: 44 additions & 0 deletions enhanced/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,47 @@ func TestBetterTimes(t *testing.T) {
assert.Equal(t, td.expectedNextStartTime, nextStartTime)
}
}

func TestScraperDisableEnhancedMetrics(t *testing.T) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func TestScraperDisableEnhancedMetrics is unused (from unused)

cfg, err := config.Load("../config.tests.yml")
require.NoError(t, err)
client := client.New()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assignments should only be cuddled with other assignments (from wsl)

for i := range cfg.Instances {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only one cuddle assignment allowed before range statement (from wsl)

// Disable enhanced metrics in even instances.
// This disable instance: no-such-instance.
isDisabled := i%2 == 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mnd: Magic number: 2, in detected (from gomnd)

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

Choose a reason for hiding this comment

The 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 wsl)

}

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)
}
})
}
}
18 changes: 12 additions & 6 deletions sessions/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
type Instance struct {
Region string
Instance string
DisableBasicMetrics bool
DisableEnhancedMetrics bool
ResourceID string
Labels map[string]string
EnhancedMonitoringInterval time.Duration
Expand Down Expand Up @@ -52,9 +54,11 @@ func New(instances []config.Instance, client *http.Client, trace bool) (*Session
// re-use session for the same region and key (explicit or empty for implicit) pair
if s := sharedSessions[instance.Region+"/"+instance.AWSAccessKey]; s != nil {
res.sessions[s] = append(res.sessions[s], Instance{
Region: instance.Region,
Instance: instance.Instance,
Labels: instance.Labels,
Region: instance.Region,
Instance: instance.Instance,
Labels: instance.Labels,
DisableBasicMetrics: instance.DisableBasicMetrics,
DisableEnhancedMetrics: instance.DisableEnhancedMetrics,
})
continue
}
Expand Down Expand Up @@ -96,9 +100,11 @@ func New(instances []config.Instance, client *http.Client, trace bool) (*Session
}
sharedSessions[instance.Region+"/"+instance.AWSAccessKey] = s
res.sessions[s] = append(res.sessions[s], Instance{
Region: instance.Region,
Instance: instance.Instance,
Labels: instance.Labels,
Region: instance.Region,
Instance: instance.Instance,
Labels: instance.Labels,
DisableBasicMetrics: instance.DisableBasicMetrics,
DisableEnhancedMetrics: instance.DisableEnhancedMetrics,
})
}

Expand Down