Skip to content

Commit

Permalink
PMM-4474 Add extra labels. (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi authored Dec 4, 2019
1 parent 7956cc9 commit 80355c9
Show file tree
Hide file tree
Showing 543 changed files with 141,780 additions and 53,473 deletions.
108 changes: 51 additions & 57 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ instances:
region: us-east-1
aws_access_key: AKIAIOSFODNN7EXAMPLE
aws_secret_key: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
labels:
foo: bar
baz: qux
```
If `aws_access_key` and `aws_secret_key` are present, they are used for that instance.
Otherwise, [default credential provider chain](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html#specifying-credentials)
is used, which includes `AWS_ACCESS_KEY_ID`/`AWS_ACCESS_KEY` and `AWS_SECRET_ACCESS_KEY`/`AWS_SECRET_KEY` environment variables, `~/.aws/credentials` file,
and IAM role for EC2.

Returned metrics contain `instance` and `region` labels set. They also contain extra labels specified in the configuration file.

Start exporter by running:
```
Expand Down
41 changes: 20 additions & 21 deletions basic/basic.go → basic/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,46 @@ var (
)

type Metric struct {
Name string
Desc *prometheus.Desc
cwName string
prometheusName string
prometheusHelp string
}

type Exporter struct {
type Collector struct {
config *config.Config
sessions *sessions.Sessions
metrics []Metric
l log.Logger
}

// New creates a new instance of a Exporter.
func New(config *config.Config, sessions *sessions.Sessions) *Exporter {
return &Exporter{
// New creates a new instance of a Collector.
func New(config *config.Config, sessions *sessions.Sessions) *Collector {
return &Collector{
config: config,
sessions: sessions,
metrics: Metrics,
l: log.With("component", "basic"),
}
}

func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
func (e *Collector) Describe(ch chan<- *prometheus.Desc) {
// unchecked collector
}

func (e *Collector) Collect(ch chan<- prometheus.Metric) {
now := time.Now()
e.collect(ch)

// Collect scrape time
ch <- prometheus.MustNewConstMetric(scrapeTimeDesc, prometheus.GaugeValue, time.Since(now).Seconds())
}

func (e *Exporter) collect(ch chan<- prometheus.Metric) {
wg := &sync.WaitGroup{}
func (e *Collector) collect(ch chan<- prometheus.Metric) {
var wg sync.WaitGroup
defer wg.Wait()

instances := e.config.Instances
wg.Add(len(instances))
for _, instance := range instances {
wg.Add(len(e.config.Instances))
for _, instance := range e.config.Instances {
instance := instance
go func() {
defer wg.Done()
Expand All @@ -73,12 +77,7 @@ func (e *Exporter) collect(ch chan<- prometheus.Metric) {
}
}

func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
// RDS metrics
for _, m := range e.metrics {
ch <- m.Desc
}

// Scrape time
ch <- scrapeTimeDesc
}
// check interfaces
var (
_ prometheus.Collector = (*Collector)(nil)
)
6 changes: 3 additions & 3 deletions basic/basic_test.go → basic/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/percona/rds_exporter/sessions"
)

func getExporter(t *testing.T) *Exporter {
func getCollector(t *testing.T) *Collector {
t.Helper()

cfg, err := config.Load("../config.yml")
Expand All @@ -25,7 +25,7 @@ func getExporter(t *testing.T) *Exporter {
}

func TestCollector_Describe(t *testing.T) {
c := getExporter(t)
c := getCollector(t)
ch := make(chan *prometheus.Desc)
go func() {
c.Describe(ch)
Expand All @@ -42,7 +42,7 @@ func TestCollector_Describe(t *testing.T) {
}

func TestCollector_Collect(t *testing.T) {
c := getExporter(t)
c := getCollector(t)
ch := make(chan prometheus.Metric)
go func() {
c.Collect(ch)
Expand Down
Loading

0 comments on commit 80355c9

Please sign in to comment.