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-10974 Fallback to PGSS in case of not available PGSM. #2827

Merged
merged 16 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
8 changes: 8 additions & 0 deletions agent/serviceinfobroker/service_info_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/go-sql-driver/mysql"
"github.com/lib/pq"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/mongo"

Expand Down Expand Up @@ -231,6 +232,13 @@ func (sib *ServiceInfoBroker) getPostgreSQLInfo(ctx context.Context, dsn string,
}
res.Version = version

var pgsmVersion string
err = db.QueryRowContext(ctx, "SELECT /* agent='serviceinfobroker' */ extversion FROM pg_extension WHERE extname = 'pg_stat_monitor';").Scan(&pgsmVersion)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
res.Error = err.Error()
}
res.PgsmVersion = pgsmVersion

return &res
}

Expand Down
14 changes: 14 additions & 0 deletions agent/serviceinfobroker/service_info_broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ func TestServiceInfoBroker(t *testing.T) {
assert.InDelta(t, 250, resp.TableCount, 150)
})

t.Run("PostgreSQLOptions", func(t *testing.T) {
cfgStorage := config.NewStorage(&config.Config{
Paths: config.Paths{TempDir: t.TempDir()},
})
c := New(cfgStorage)
resp := c.GetInfoFromService(context.Background(), &agentpb.ServiceInfoRequest{
Dsn: tests.GetTestPostgreSQLDSN(t),
Type: inventorypb.ServiceType_POSTGRESQL_SERVICE,
}, 0)
require.NotNil(t, resp)
assert.Equal(t, []string{"postgres", "pmm-agent"}, resp.DatabaseList)
assert.Equal(t, "", resp.PgsmVersion)
})

t.Run("MongoDBWithSSL", func(t *testing.T) {
mongoDBDSNWithSSL, mongoDBTextFiles := tests.GetTestMongoDBWithSSLDSN(t, "../")

Expand Down
843 changes: 427 additions & 416 deletions api/agentpb/agent.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions api/agentpb/agent.pb.validate.go

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

2 changes: 2 additions & 0 deletions api/agentpb/agent.proto
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ message ServiceInfoResponse {
string version = 3;
// A list of PostgreSQL databases.
repeated string database_list = 4;
// A version of pg_stat_monitor, empty if unavailable.
string pgsm_version = 5;
}

// JobStatusRequest is a ServerMessage asking pmm-agent for job status.
Expand Down
1 change: 1 addition & 0 deletions managed/models/agent_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
SSLKey string `json:"ssl_key"`
AutoDiscoveryLimit int32 `json:"auto_discovery_limit"`
DatabaseCount int32 `json:"database_count"`
PGSMVersion string `json:"pgsm_version"`
}

// Value implements database/sql/driver.Valuer interface. Should be defined on the value.
Expand Down Expand Up @@ -439,7 +440,7 @@

if s.MongoDBOptions != nil {
if s.MongoDBOptions.TLSCertificateKey != "" {
q.Add("tlsCertificateKeyFile", tdp.Left+".TextFiles."+certificateKeyFilePlaceholder+tdp.Right)

Check failure on line 443 in managed/models/agent_model.go

View workflow job for this annotation

GitHub Actions / Checks

string `.TextFiles.` has 5 occurrences, make it a constant (goconst)
}
if s.MongoDBOptions.TLSCertificateKeyFilePassword != "" {
q.Add("tlsCertificateKeyFilePassword", s.MongoDBOptions.TLSCertificateKeyFilePassword)
Expand Down
1 change: 1 addition & 0 deletions managed/services/agents/service_info_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func (c *ServiceInfoBroker) GetInfoFromService(ctx context.Context, q *reform.Qu
}
}
}
agent.PostgreSQLOptions.PGSMVersion = sInfo.PgsmVersion
agent.PostgreSQLOptions.DatabaseCount = int32(databaseCount - excludedDatabaseCount)

l.Debugf("Updating PostgreSQL options, database count: %d.", agent.PostgreSQLOptions.DatabaseCount)
Expand Down
17 changes: 14 additions & 3 deletions managed/services/management/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"

"github.com/AlekSi/pointer"
"github.com/sirupsen/logrus"
"gopkg.in/reform.v1"

"github.com/percona/pmm/api/inventorypb"
Expand All @@ -37,6 +38,7 @@ type PostgreSQLService struct {
state agentsStateUpdater
cc connectionChecker
sib serviceInfoBroker
l *logrus.Entry
}

// NewPostgreSQLService creates new PostgreSQL Management Service.
Expand All @@ -46,6 +48,7 @@ func NewPostgreSQLService(db *reform.DB, state agentsStateUpdater, cc connection
state: state,
cc: cc,
sib: sib,
l: logrus.WithField("component", "postgresql"),
}
}

Expand Down Expand Up @@ -93,6 +96,7 @@ func (s *PostgreSQLService) Add(ctx context.Context, req *managementpb.AddPostgr
return err
}

options := models.PostgreSQLOptionsFromRequest(req)
row, err := models.CreateAgent(tx.Querier, models.PostgresExporterType, &models.CreateAgentParams{
PMMAgentID: req.PmmAgentId,
ServiceID: service.ServiceID,
Expand All @@ -104,7 +108,7 @@ func (s *PostgreSQLService) Add(ctx context.Context, req *managementpb.AddPostgr
PushMetrics: isPushMode(req.MetricsMode),
ExposeExporter: req.ExposeExporter,
DisableCollectors: req.DisableCollectors,
PostgreSQLOptions: models.PostgreSQLOptionsFromRequest(req),
PostgreSQLOptions: options,
LogLevel: services.SpecifyLogLevel(req.LogLevel, inventorypb.LogLevel_error),
})
if err != nil {
Expand All @@ -119,6 +123,13 @@ func (s *PostgreSQLService) Add(ctx context.Context, req *managementpb.AddPostgr
if err = s.sib.GetInfoFromService(ctx, tx.Querier, service, row); err != nil {
return err
}

// In case of not available PGSM extension it is switch to PGSS.
if req.QanPostgresqlPgstatmonitorAgent && row.PostgreSQLOptions.PGSMVersion == "" {
s.l.Warningln("Extension pg_stat_monitor is not available. Switching to pg_stats_statements.")
req.QanPostgresqlPgstatementsAgent = true
req.QanPostgresqlPgstatmonitorAgent = false
}
}

agent, err := services.ToAPIAgent(tx.Querier, row)
Expand All @@ -138,7 +149,7 @@ func (s *PostgreSQLService) Add(ctx context.Context, req *managementpb.AddPostgr
CommentsParsingDisabled: req.DisableCommentsParsing,
TLS: req.Tls,
TLSSkipVerify: req.TlsSkipVerify,
PostgreSQLOptions: models.PostgreSQLOptionsFromRequest(req),
PostgreSQLOptions: options,
LogLevel: services.SpecifyLogLevel(req.LogLevel, inventorypb.LogLevel_fatal),
})
if err != nil {
Expand All @@ -163,7 +174,7 @@ func (s *PostgreSQLService) Add(ctx context.Context, req *managementpb.AddPostgr
CommentsParsingDisabled: req.DisableCommentsParsing,
TLS: req.Tls,
TLSSkipVerify: req.TlsSkipVerify,
PostgreSQLOptions: models.PostgreSQLOptionsFromRequest(req),
PostgreSQLOptions: options,
LogLevel: services.SpecifyLogLevel(req.LogLevel, inventorypb.LogLevel_fatal),
})
if err != nil {
Expand Down
Loading