Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
Add 'databases' property into YAML configuration - it is used for cla…
Browse files Browse the repository at this point in the history
…rifying databases from which builtin metrics should be collected. Add database skip logic into functions, tables, indexes, schema collectors.
  • Loading branch information
lesovsky committed May 17, 2021
1 parent 59e1da4 commit 8547190
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 5 deletions.
3 changes: 3 additions & 0 deletions internal/collector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/weaponry/pgscv/internal/log"
"github.com/weaponry/pgscv/internal/model"
"github.com/weaponry/pgscv/internal/store"
"regexp"
"strconv"
"strings"
)
Expand All @@ -22,6 +23,8 @@ type Config struct {
NoTrackMode bool
// PostgresServiceConfig defines collector's options specific for Postgres service
PostgresServiceConfig
// DatabasesRE defines regexp with databases from which builtin metrics should be collected.
DatabasesRE *regexp.Regexp
// Filters are user-defined regular expressions allow to include/exclude collecting various stats.
Filters map[string]filter.Filter
// Settings defines collectors settings propagated from main YAML configuration.
Expand Down
5 changes: 5 additions & 0 deletions internal/collector/postgres_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func (c *postgresFunctionsCollector) Update(config Config, ch chan<- prometheus.
}

for _, d := range databases {
// Skip database if not matched to allowed.
if config.DatabasesRE != nil && !config.DatabasesRE.MatchString(d) {
continue
}

pgconfig.Database = d
conn, err := store.NewWithConfig(pgconfig)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/collector/postgres_indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ func (c *postgresIndexesCollector) Update(config Config, ch chan<- prometheus.Me
}

for _, d := range databases {
// Skip database if not matched to allowed.
if config.DatabasesRE != nil && !config.DatabasesRE.MatchString(d) {
continue
}

pgconfig.Database = d
conn, err := store.NewWithConfig(pgconfig)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/collector/postgres_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ func (c *postgresSchemaCollector) Update(config Config, ch chan<- prometheus.Met

// walk through all databases, connect to it and collect schema-specific stats
for _, d := range databases {
// Skip database if not matched to allowed.
if config.DatabasesRE != nil && !config.DatabasesRE.MatchString(d) {
continue
}

pgconfig.Database = d
conn, err := store.NewWithConfig(pgconfig)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/collector/postgres_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ func (c *postgresTablesCollector) Update(config Config, ch chan<- prometheus.Met
}

for _, d := range databases {
// Skip database if not matched to allowed.
if config.DatabasesRE != nil && !config.DatabasesRE.MatchString(d) {
continue
}

pgconfig.Database = d
conn, err := store.NewWithConfig(pgconfig)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions internal/pgscv/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type Config struct {
Filters filter.Filters `yaml:"filters"`
DisableCollectors []string `yaml:"disable_collectors"` // List of collectors which should be disabled. DEPRECATED in favor collectors settings
CollectorsSettings model.CollectorsSettings `yaml:"collectors"` // Collectors settings propagated from main YAML configuration
Databases string `yaml:"databases"` // Regular expression string specifies databases from which metrics should be collected
DatabasesRE *regexp.Regexp // Regular expression object compiled from Databases
}

// NewConfig creates new config based on config file or return default config of config is not exists.
Expand Down Expand Up @@ -131,6 +133,13 @@ func (c *Config) Validate() error {
}
}

// Create 'databases' regexp object for builtin metrics.
re, err := newDatabasesRegexp(c.Databases)
if err != nil {
return err
}
c.DatabasesRE = re

// Add default filters and compile regexps.
if c.Filters == nil {
c.Filters = filter.New()
Expand Down Expand Up @@ -226,3 +235,12 @@ func toggleAutoupdate(value string) (string, error) {
return "", fmt.Errorf("invalid value '%s' for 'autoupdate'", value)
}
}

// newDatabasesRegexp creates new regexp depending on passed string.
func newDatabasesRegexp(s string) (*regexp.Regexp, error) {
if s == "" {
s = ".+"
}

return regexp.Compile(s)
}
27 changes: 27 additions & 0 deletions internal/pgscv/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ func TestConfig_Validate(t *testing.T) {
valid: false,
in: &Config{ListenAddress: "127.0.0.1:8080", Filters: map[string]filter.Filter{"test": {Include: "["}}},
},
{
name: "invalid config: invalid databases string",
valid: false,
in: &Config{ListenAddress: "127.0.0.1:8080", Databases: "["},
},
}

for _, tc := range testcases {
Expand Down Expand Up @@ -395,3 +400,25 @@ func Test_toggleAutoupdate(t *testing.T) {
}
}
}

func Test_newDatabasesRegexp(t *testing.T) {
testcases := []struct {
valid bool
str string
}{
{valid: true, str: "example(1|2)"},
{valid: true, str: ""},
{valid: false, str: "["},
}

for _, tc := range testcases {
got, err := newDatabasesRegexp(tc.str)
if tc.valid {
assert.NoError(t, err)
assert.NotNil(t, got)
} else {
assert.Error(t, err)
assert.Nil(t, got)
}
}
}
1 change: 1 addition & 0 deletions internal/pgscv/pgscv.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func Start(ctx context.Context, config *Config) error {
ConnDefaults: config.Defaults,
ConnsSettings: config.ServicesConnsSettings,
Filters: config.Filters,
DatabasesRE: config.DatabasesRE,
DisabledCollectors: config.DisableCollectors,
CollectorsSettings: config.CollectorsSettings,
}
Expand Down
14 changes: 9 additions & 5 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"io"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -51,11 +52,13 @@ type Service struct {

// Config defines service's configuration.
type Config struct {
RuntimeMode int
NoTrackMode bool
ConnDefaults map[string]string `yaml:"defaults"` // Defaults
ConnsSettings ConnsSettings
Filters map[string]filter.Filter
RuntimeMode int
NoTrackMode bool
ConnDefaults map[string]string `yaml:"defaults"` // Defaults
ConnsSettings ConnsSettings
Filters map[string]filter.Filter
// DatabasesRE defines regexp with databases from which builtin metrics should be collected.
DatabasesRE *regexp.Regexp
DisabledCollectors []string
// CollectorsSettings defines all collector settings propagated from main YAML configuration.
CollectorsSettings model.CollectorsSettings
Expand Down Expand Up @@ -364,6 +367,7 @@ func (repo *Repository) setupServices(config Config) error {
ConnString: service.ConnSettings.Conninfo,
Filters: config.Filters,
Settings: config.CollectorsSettings,
DatabasesRE: config.DatabasesRE,
}

switch service.ConnSettings.ServiceType {
Expand Down

0 comments on commit 8547190

Please sign in to comment.