Skip to content

Support excluding specific postgresql databases from stats collection #29605

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

Merged
merged 5 commits into from
Dec 6, 2023
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
27 changes: 27 additions & 0 deletions .chloggen/postgresql-exclude.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'enhancement'

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: postgresqlreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add config property for excluding specific databases from scraping

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [29605]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
4 changes: 3 additions & 1 deletion receiver/postgresqlreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ The following settings are optional:

- `databases` (default = `[]`): The list of databases for which the receiver will attempt to collect statistics. If an empty list is provided, the receiver will attempt to collect statistics for all non-template databases.

- `exclude_databases` (default = `[]`): List of databases which will be excluded when collecting statistics.

The following settings are also optional and nested under `tls` to help configure client transport security

- `insecure` (default = `false`): Whether to enable client transport security for the postgresql connection.
Expand Down Expand Up @@ -70,7 +72,7 @@ receivers:
key_file: /home/otel/mypostgreskey.key
```

The full list of settings exposed for this receiver are documented [here](./config.go) with detailed sample configurations [here](./testdata/config.yaml). TLS config is documented further under the [opentelemetry collector's configtls package](https://github.com/open-telemetry/opentelemetry-collector/blob/main/config/configtls/README.md).
The full list of settings exposed for this receiver are documented [here](./config.go) with detailed sample configurations [here](./testdata/config.yaml). TLS config is documented further under the [opentelemetry collector's configtls package](https://github.com/open-telemetry/opentelemetry-collector/blob/main/config/configtls/README.md).

## Metrics

Expand Down
1 change: 1 addition & 0 deletions receiver/postgresqlreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Config struct {
Username string `mapstructure:"username"`
Password configopaque.String `mapstructure:"password"`
Databases []string `mapstructure:"databases"`
ExcludeDatabases []string `mapstructure:"exclude_databases"`
confignet.NetAddr `mapstructure:",squash"` // provides Endpoint and Transport
configtls.TLSClientSetting `mapstructure:"tls,omitempty"` // provides SSL details
metadata.MetricsBuilderConfig `mapstructure:",squash"`
Expand Down
1 change: 1 addition & 0 deletions receiver/postgresqlreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func TestLoadConfig(t *testing.T) {
expected.Username = "otel"
expected.Password = "${env:POSTGRESQL_PASSWORD}"
expected.Databases = []string{"otel"}
expected.ExcludeDatabases = []string{"template0"}
expected.CollectionInterval = 10 * time.Second
expected.TLSClientSetting = configtls.TLSClientSetting{
Insecure: false,
Expand Down
13 changes: 13 additions & 0 deletions receiver/postgresqlreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type postgreSQLScraper struct {
config *Config
clientFactory postgreSQLClientFactory
mb *metadata.MetricsBuilder
excludes map[string]struct{}
}
type errsMux struct {
sync.RWMutex
Expand Down Expand Up @@ -69,11 +70,16 @@ func newPostgreSQLScraper(
config *Config,
clientFactory postgreSQLClientFactory,
) *postgreSQLScraper {
excludes := make(map[string]struct{})
for _, db := range config.ExcludeDatabases {
excludes[db] = struct{}{}
}
return &postgreSQLScraper{
logger: settings.Logger,
config: config,
clientFactory: clientFactory,
mb: metadata.NewMetricsBuilder(config.MetricsBuilderConfig, settings),
excludes: excludes,
}
}

Expand Down Expand Up @@ -102,6 +108,13 @@ func (p *postgreSQLScraper) scrape(ctx context.Context) (pmetric.Metrics, error)
}
databases = dbList
}
var filteredDatabases []string
for _, db := range databases {
if _, ok := p.excludes[db]; !ok {
filteredDatabases = append(filteredDatabases, db)
}
}
databases = filteredDatabases

now := pcommon.NewTimestampFromTime(time.Now())

Expand Down
21 changes: 21 additions & 0 deletions receiver/postgresqlreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,27 @@ func TestScraperWithResourceAttributeFeatureGateSingle(t *testing.T) {
pmetrictest.IgnoreMetricDataPointsOrder(), pmetrictest.IgnoreStartTimestamp(), pmetrictest.IgnoreTimestamp()))
}

func TestScraperExcludeDatabase(t *testing.T) {
factory := mockClientFactory{}
factory.initMocks([]string{"otel", "telemetry"})

cfg := createDefaultConfig().(*Config)
cfg.ExcludeDatabases = []string{"open"}

scraper := newPostgreSQLScraper(receivertest.NewNopCreateSettings(), cfg, &factory)

actualMetrics, err := scraper.scrape(context.Background())
require.NoError(t, err)

expectedFile := filepath.Join("testdata", "scraper", "multiple", "exclude.yaml")

expectedMetrics, err := golden.ReadMetrics(expectedFile)
require.NoError(t, err)

require.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, actualMetrics, pmetrictest.IgnoreResourceMetricsOrder(),
pmetrictest.IgnoreMetricDataPointsOrder(), pmetrictest.IgnoreStartTimestamp(), pmetrictest.IgnoreTimestamp()))
}

type mockClientFactory struct{ mock.Mock }
type mockClient struct{ mock.Mock }

Expand Down
2 changes: 2 additions & 0 deletions receiver/postgresqlreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ postgresql/all:
password: ${env:POSTGRESQL_PASSWORD}
databases:
- otel
exclude_databases:
- template0
collection_interval: 10s
tls:
insecure: false
Expand Down
Loading