diff --git a/agent/connectionchecker/connection_checker.go b/agent/connectionchecker/connection_checker.go index 9837842259..3691c198af 100644 --- a/agent/connectionchecker/connection_checker.go +++ b/agent/connectionchecker/connection_checker.go @@ -31,13 +31,13 @@ import ( "github.com/pkg/errors" "github.com/prometheus/common/expfmt" "github.com/sirupsen/logrus" - "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "github.com/percona/pmm/agent/config" "github.com/percona/pmm/agent/tlshelpers" "github.com/percona/pmm/agent/utils/mongo_fix" "github.com/percona/pmm/agent/utils/templates" + agent_version "github.com/percona/pmm/agent/utils/version" "github.com/percona/pmm/api/agentpb" "github.com/percona/pmm/api/inventorypb" ) @@ -177,9 +177,9 @@ func (cc *ConnectionChecker) checkMongoDBConnection(ctx context.Context, dsn str return &res } - resp := client.Database("admin").RunCommand(ctx, bson.D{{Key: "getDiagnosticData", Value: 1}}) - if err = resp.Err(); err != nil { - cc.l.Debugf("checkMongoDBConnection: failed to runCommand getDiagnosticData: %s", err) + _, err = agent_version.GetMongoDBVersion(ctx, client) + if err != nil { + cc.l.Debugf("checkMongoDBConnection: failed to get MongoDB version: %s", err) res.Error = err.Error() return &res } diff --git a/agent/serviceinfobroker/service_info_broker.go b/agent/serviceinfobroker/service_info_broker.go index df62102b86..3144b39628 100644 --- a/agent/serviceinfobroker/service_info_broker.go +++ b/agent/serviceinfobroker/service_info_broker.go @@ -34,8 +34,10 @@ import ( "github.com/percona/pmm/agent/tlshelpers" "github.com/percona/pmm/agent/utils/mongo_fix" "github.com/percona/pmm/agent/utils/templates" + agent_version "github.com/percona/pmm/agent/utils/version" "github.com/percona/pmm/api/agentpb" "github.com/percona/pmm/api/inventorypb" + "github.com/percona/pmm/version" ) // configGetter allows to get a config. @@ -174,28 +176,28 @@ func (sib *ServiceInfoBroker) getMongoDBInfo(ctx context.Context, dsn string, fi return &res } - resp := client.Database("admin").RunCommand(ctx, bson.D{{Key: "getDiagnosticData", Value: 1}}) - if err = resp.Err(); err != nil { - sib.l.Debugf("getMongoDBInfo: failed to runCommand getDiagnosticData: %s", err) + mongoVersion, err := agent_version.GetMongoDBVersion(ctx, client) + if err != nil { + sib.l.Debugf("getMongoDBInfo: failed to get MongoDB version: %s", err) res.Error = err.Error() return &res } - resp = client.Database("admin").RunCommand(ctx, bson.D{{Key: "buildInfo", Value: 1}}) + // use hello command for newer MongoDB versions + command := "hello" + helloCommandVersion := version.MustParse("4.2.10") + if mongoVersion.Less(helloCommandVersion) { + command = "isMaster" + } + + resp := client.Database("admin").RunCommand(ctx, bson.D{{Key: command, Value: 1}}) if err = resp.Err(); err != nil { + sib.l.Debugf("getMongoDBInfo: failed to runCommand hello: %s", err) res.Error = err.Error() return &res } - buildInfo := struct { - Version string `bson:"version"` - }{} - - if err = resp.Decode(&buildInfo); err != nil { - sib.l.Debugf("getMongoDBInfo: failed to decode buildInfo: %s", err) - } - - res.Version = buildInfo.Version + res.Version = mongoVersion.String() return &res } diff --git a/agent/utils/version/mongo.go b/agent/utils/version/mongo.go new file mode 100644 index 0000000000..89d7fa76fd --- /dev/null +++ b/agent/utils/version/mongo.go @@ -0,0 +1,32 @@ +package version + +import ( + "context" + + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" + + "github.com/percona/pmm/version" +) + +// GetMongoDBVersion returns the parsed version of the connected MongoDB server. +func GetMongoDBVersion(ctx context.Context, client *mongo.Client) (*version.Parsed, error) { + resp := client.Database("admin").RunCommand(ctx, bson.D{{Key: "buildInfo", Value: 1}}) + if err := resp.Err(); err != nil { + return nil, err + } + + buildInfo := struct { + Version string `bson:"version"` + }{} + + if err := resp.Decode(&buildInfo); err != nil { + return nil, err + } + + mongoVersion, err := version.Parse(buildInfo.Version) + if err != nil { + return nil, err + } + return mongoVersion, nil +} diff --git a/managed/services/agents/service_info_broker.go b/managed/services/agents/service_info_broker.go index 74f7713a6b..60a1813697 100644 --- a/managed/services/agents/service_info_broker.go +++ b/managed/services/agents/service_info_broker.go @@ -183,7 +183,7 @@ func (c *ServiceInfoBroker) GetInfoFromService(ctx context.Context, q *reform.Qu msg := sInfo.Error if msg == context.Canceled.Error() || msg == context.DeadlineExceeded.Error() { msg = fmt.Sprintf("timeout (%s)", msg) - return status.Error(codes.FailedPrecondition, fmt.Sprintf("Connection check failed: %s.", msg)) + return status.Error(codes.FailedPrecondition, fmt.Sprintf("failed to get connection service info: %s.", msg)) } stype := service.ServiceType