Skip to content

Commit

Permalink
PMM-10974 Fallback to PGSS in case of not available PGSM. (#2827)
Browse files Browse the repository at this point in the history
* PMM-12872 Fallback to PGSS in case of not available PGSM.

* PMM-12872 Lint.

* PMM-12872 Lint.

* PMM-12872 Add test for PostgreSQLOptions.

* PMM-12872 Small change in if.

* Update api/agentpb/agent.proto

Co-authored-by: Alex Demidoff <[email protected]>

* Update managed/services/management/postgresql.go

Co-authored-by: Alex Demidoff <[email protected]>

* PMM-12872 Remove nil check in if.

* PMM-12872 Gen.

* PMM-10974 Warning message on pmm-admin side.

* PMM-10974 Change warning message.

---------

Co-authored-by: Alex Demidoff <[email protected]>
  • Loading branch information
JiriCtvrtka and ademidoff authored Feb 26, 2024
1 parent 4867d47 commit f8d5049
Show file tree
Hide file tree
Showing 16 changed files with 542 additions and 455 deletions.
5 changes: 5 additions & 0 deletions admin/commands/management/add_postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ var addPostgreSQLResultT = commands.ParseTemplate(`
PostgreSQL Service added.
Service ID : {{ .Service.ServiceID }}
Service name: {{ .Service.ServiceName }}
{{ if .Warning }}
Warning: {{ .Warning }}
{{- end -}}
`)

type addPostgreSQLResult struct {
Service *postgresql.AddPostgreSQLOKBodyService `json:"service"`
Warning string `json:"warning"`
}

func (res *addPostgreSQLResult) Result() {}
Expand Down Expand Up @@ -223,5 +227,6 @@ func (cmd *AddPostgreSQLCommand) RunCmd() (commands.Result, error) {

return &addPostgreSQLResult{
Service: resp.Payload.Service,
Warning: resp.Payload.Warning,
}, nil
}
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

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

5 changes: 5 additions & 0 deletions api/managementpb/json/managementpb.json
Original file line number Diff line number Diff line change
Expand Up @@ -4626,6 +4626,11 @@
}
},
"x-order": 0
},
"warning": {
"description": "Warning message.",
"type": "string",
"x-order": 4
}
}
}
Expand Down
82 changes: 46 additions & 36 deletions api/managementpb/postgresql.pb.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/managementpb/postgresql.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/managementpb/postgresql.proto
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ message AddPostgreSQLResponse {
inventory.PostgresExporter postgres_exporter = 2;
inventory.QANPostgreSQLPgStatementsAgent qan_postgresql_pgstatements_agent = 3;
inventory.QANPostgreSQLPgStatMonitorAgent qan_postgresql_pgstatmonitor_agent = 4;
// Warning message.
string warning = 5;
}

// PostgreSQL service provides public Management API methods for PostgreSQL Service.
Expand Down
5 changes: 5 additions & 0 deletions api/swagger/swagger-dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -28470,6 +28470,11 @@
}
},
"x-order": 3
},
"warning": {
"description": "Warning message.",
"type": "string",
"x-order": 4
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions api/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -19653,6 +19653,11 @@
}
},
"x-order": 3
},
"warning": {
"description": "Warning message.",
"type": "string",
"x-order": 4
}
}
}
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 @@ type PostgreSQLOptions struct {
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
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 == "" {
res.Warning = "Could not to detect the pg_stat_monitor extension on your system. Falling back to the pg_stat_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

0 comments on commit f8d5049

Please sign in to comment.