Skip to content
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

PMM-13111 disable new postgresql collectors. #3111

Merged
merged 12 commits into from
Aug 26, 2024
4 changes: 2 additions & 2 deletions managed/services/agents/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func v226Args(exporter *models.Agent, tdp *models.DelimiterPair, listenAddress s
collstatsLimit = exporter.MongoDBOptions.CollectionsLimit
}

collectors := defaultCollectors(collectAll)
collectors := defaultMongoDBCollectors(collectAll)

if !pmmAgentVersion.Less(v2_41_1) { // >= 2.41.1
collectors["shards"] = collectorArgs{
Expand Down Expand Up @@ -240,7 +240,7 @@ func v225Args(exporter *models.Agent, tdp *models.DelimiterPair, listenAddress s
return args
}

func defaultCollectors(collectAll bool) map[string]collectorArgs {
func defaultMongoDBCollectors(collectAll bool) map[string]collectorArgs {
return map[string]collectorArgs{
"diagnosticdata": {
enabled: true,
Expand Down
22 changes: 21 additions & 1 deletion managed/services/agents/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,25 @@ import (
var (
postgresExporterAutodiscoveryVersion = version.MustParse("2.15.99")
postgresExporterWebConfigVersion = version.MustParse("2.30.99")
postgresSSLSniVersion = version.MustParse("2.40.99")
postgresSSLSniVersion = version.MustParse("2.41.0-0")
postgresExporterCollectorsVersion = version.MustParse("2.41.0-0")
postgresMaxExporterConnsVersion = version.MustParse("2.41.2-0")
)

var defaultPostgresExporterCollectors = []string{
"database",
"database_wraparound",
"extensions",
"locks",
"replication",
"replication_slot",
"stat_bgwriter",
"stat_database",
"stat_user_tables",
"statio_user_tables",
"wal",
}

const defaultAutoDiscoveryDatabaseLimit = 50

func postgresExcludedDatabases() []string {
Expand Down Expand Up @@ -101,6 +116,11 @@ func postgresExporterConfig(node *models.Node, service *models.Service, exporter

args = collectors.FilterOutCollectors("--collect.", args, exporter.DisabledCollectors)

if !pmmAgentVersion.Less(postgresExporterCollectorsVersion) {
disableCollectorArgs := collectors.DisableDefaultEnabledCollectors("--no-collector.", defaultPostgresExporterCollectors, exporter.DisabledCollectors)
args = append(args, disableCollectorArgs...)
}

args = withLogLevel(args, exporter.LogLevel, pmmAgentVersion, false)

sort.Strings(args)
Expand Down
12 changes: 8 additions & 4 deletions managed/services/agents/postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ func (s *PostgresExporterConfigTestSuite) TestSocket() {
}

func (s *PostgresExporterConfigTestSuite) TestDisabledCollectors() {
s.pmmAgentVersion = &version.Parsed{}
s.pmmAgentVersion = version.MustParse("2.42.0")
s.postgresql.Address = nil
s.postgresql.Port = nil
s.postgresql.Socket = pointer.ToString("/var/run/postgres")
s.exporter.DisabledCollectors = []string{"custom_query.hr", "custom_query.hr.directory"}
s.exporter.DisabledCollectors = []string{"custom_query.hr", "custom_query.hr.directory", "locks"}

actual, err := postgresExporterConfig(s.node, s.postgresql, s.exporter, exposeSecrets, s.pmmAgentVersion)
s.NoError(err, "Failed to create exporter config")
Expand All @@ -164,11 +164,15 @@ func (s *PostgresExporterConfigTestSuite) TestDisabledCollectors() {
TemplateLeftDelim: "{{",
TemplateRightDelim: "}}",
Args: []string{
"--auto-discover-databases",
"--collect.custom_query.lr",
"--collect.custom_query.lr.directory=" + pathsBase(s.pmmAgentVersion, "{{", "}}") + "/collectors/custom-queries/postgresql/low-resolution",
"--collect.custom_query.lr.directory={{ .paths_base }}/collectors/custom-queries/postgresql/low-resolution",
"--collect.custom_query.mr",
"--collect.custom_query.mr.directory=" + pathsBase(s.pmmAgentVersion, "{{", "}}") + "/collectors/custom-queries/postgresql/medium-resolution",
"--collect.custom_query.mr.directory={{ .paths_base }}/collectors/custom-queries/postgresql/medium-resolution",
"--exclude-databases=template0,template1,postgres,cloudsqladmin,pmm-managed-dev,azure_maintenance,rdsadmin",
"--no-collector.locks",
"--web.listen-address=0.0.0.0:{{ .listen_port }}",
"--web.config={{ .TextFiles.webConfigPlaceholder }}",
},
}
requireNoDuplicateFlags(s.T(), actual.Args)
Expand Down
17 changes: 17 additions & 0 deletions managed/utils/collectors/collectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,20 @@ func FilterOutCollectors(prefix string, args, disabledCollectors []string) []str
}
return enabledArgs
}

// DisableDefaultEnabledCollectors returns CLI arguments to disable default enabled collectors based on input.
// DefaultCollectors and disabledCollectors should be collector names without prefix.
// Result will be returned with prefix.
func DisableDefaultEnabledCollectors(prefix string, defaultCollectors []string, disabledCollectors []string) []string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Considering we always use --no-collector as prefix for disabling collectors, should we just make it a constant and drop the prefix parameter?

Suggested change
func DisableDefaultEnabledCollectors(prefix string, defaultCollectors []string, disabledCollectors []string) []string {
func DisableDefaultEnabledCollectors(defaultCollectors []string, disabledCollectors []string) []string {

defaultCollectorsMap := make(map[string]struct{})
for _, defaultCollector := range defaultCollectors {
defaultCollectorsMap[defaultCollector] = struct{}{}
}
args := []string{}
for _, collector := range disabledCollectors {
if _, ok := defaultCollectorsMap[collector]; ok {
args = append(args, fmt.Sprintf("%s%s", prefix, collector))
}
}
return args
}
78 changes: 78 additions & 0 deletions managed/utils/collectors/collectors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (C) 2023 Percona LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package collectors

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestDisableDefaultEnabledCollectors(t *testing.T) {
type args struct {
prefix string
defaultCollectors []string
disabledCollectors []string
}
tests := []struct {
name string
args args
want []string
}{
{
name: "Disable single default enabled collectors",
args: args{
prefix: "--no-collector.",
defaultCollectors: []string{"a", "b", "c", "d", "e"},
disabledCollectors: []string{"b"},
},
want: []string{"--no-collector.b"},
},
{
name: "Disable multiple default enabled collectors",
args: args{
prefix: "--no-collector.",
defaultCollectors: []string{"a", "b", "c", "d", "e", "f"},
disabledCollectors: []string{"a", "c"},
},
want: []string{"--no-collector.a", "--no-collector.c"},
},
{
name: "Disable all default enabled collectors",
args: args{
prefix: "--no-collector.",
defaultCollectors: []string{"a", "b", "c"},
disabledCollectors: []string{"a", "b", "c"},
},
want: []string{"--no-collector.a", "--no-collector.b", "--no-collector.c"},
},
{
name: "Disable non-default enabled collectors",
args: args{
prefix: "--no-collector.",
defaultCollectors: []string{"a", "b", "c"},
disabledCollectors: []string{"d", "e", "f"},
},
want: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := DisableDefaultEnabledCollectors(tt.args.prefix, tt.args.defaultCollectors, tt.args.disabledCollectors)
require.Equal(t, tt.want, actual, "DisableDefaultEnabledCollectors() = %v, want %v", actual, tt.want)
})
}
}
Loading