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-12510: Replace getDiagnosticData for connection tests #2754

Merged
merged 8 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 33 additions & 3 deletions agent/connectionchecker/connection_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,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"
BupycHuk marked this conversation as resolved.
Show resolved Hide resolved
"github.com/percona/pmm/api/agentpb"
"github.com/percona/pmm/api/inventorypb"
"github.com/percona/pmm/version"
)

// configGetter allows for getting a config.
Expand Down Expand Up @@ -145,6 +147,8 @@ func (cc *ConnectionChecker) checkMySQLConnection(ctx context.Context, dsn strin
}

func (cc *ConnectionChecker) checkMongoDBConnection(ctx context.Context, dsn string, files *agentpb.TextFiles, id uint32) *agentpb.CheckConnectionResponse {
const helloCommandVersion = "4.2.10"

var res agentpb.CheckConnectionResponse
var err error

Expand Down Expand Up @@ -177,13 +181,39 @@ 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)
mongoVersion, 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
}

serverInfo := struct {
ArbiterOnly bool `bson:"arbiterOnly"`
}{}

// use hello command for newer MongoDB versions
command := "hello"
if mongoVersion.Less(version.MustParse(helloCommandVersion)) {
command = "isMaster"
}

err = client.Database("admin").RunCommand(ctx, bson.D{{Key: command, Value: 1}}).Decode(&serverInfo)
if err != nil {
cc.l.Debugf("checkMongoDBConnection: failed to runCommand %s: %s", command, err)
res.Error = err.Error()
return &res
}

if !serverInfo.ArbiterOnly {
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)
res.Error = err.Error()
return &res
}
}

return &res
}

Expand Down
24 changes: 5 additions & 19 deletions agent/serviceinfobroker/service_info_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ import (
"github.com/go-sql-driver/mysql"
"github.com/lib/pq"
"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"
"github.com/percona/pmm/agent/utils/version"
"github.com/percona/pmm/api/agentpb"
"github.com/percona/pmm/api/inventorypb"
)
Expand Down Expand Up @@ -174,28 +174,14 @@ 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)
res.Error = err.Error()
return &res
}

resp = client.Database("admin").RunCommand(ctx, bson.D{{Key: "buildInfo", Value: 1}})
if err = resp.Err(); err != nil {
mongoVersion, err := version.GetMongoDBVersion(ctx, client)
if err != nil {
sib.l.Debugf("getMongoDBInfo: failed to get MongoDB version: %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
3 changes: 0 additions & 3 deletions agent/serviceinfobroker/service_info_broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ func TestServiceInfoBroker(t *testing.T) {
Type: inventorypb.ServiceType_MONGODB_SERVICE,
Timeout: durationpb.New(3 * time.Second),
},
expectedErr: `\(Unauthorized\) (?:command getDiagnosticData requires authentication|` +
`there are no users authenticated|` +
`not authorized on admin to execute command \{ getDiagnosticData\: 1 \})`,
},
{
name: "MongoDB wrong params",
Expand Down
46 changes: 46 additions & 0 deletions agent/utils/version/mongo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2023 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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))
idoqo marked this conversation as resolved.
Show resolved Hide resolved
}

stype := service.ServiceType
Expand Down
Loading