Skip to content

Commit

Permalink
replace getDiagnosticData for connection tests
Browse files Browse the repository at this point in the history
  • Loading branch information
idoqo committed Jan 10, 2024
1 parent c049207 commit 22894e7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
8 changes: 4 additions & 4 deletions agent/connectionchecker/connection_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down
28 changes: 15 additions & 13 deletions agent/serviceinfobroker/service_info_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down
32 changes: 32 additions & 0 deletions agent/utils/version/mongo.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion managed/services/agents/service_info_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 22894e7

Please sign in to comment.