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

GODRIVER-2904 Add environment log. #1373

Merged
merged 7 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions x/mongo/driver/topology/polling_srv_records_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (ss serverSorter) Less(i, j int) bool {
}

func compareHosts(t *testing.T, received []description.Server, expected []string) {
t.Helper()
if len(received) != len(expected) {
t.Fatalf("Number of hosts in topology does not match expected value. Got %v; want %v.", len(received), len(expected))
}
Expand Down
90 changes: 68 additions & 22 deletions x/mongo/driver/topology/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,32 @@ const (
SingleMode
)

type hostEnv int

const (
genuine hostEnv = iota
cosmosDB
documentDB
)

const (
cosmosDBMsg = `You appear to be connected to a CosmosDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/cosmosdb`
documentDBMsg = `You appear to be connected to a DocumentDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/documentdb`
)

var (
thirdPartySuffixes = map[string]hostEnv{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: If we create a separate function (logTopologyThirdPartyUsage), then there is no need to create package-level variables, we can nest these declarations within the logging function to give them context.

".cosmos.azure.com": cosmosDB,
".docdb.amazonaws.com": documentDB,
".docdb-elastic.amazonaws.com": documentDB,
}

thirdPartyMessages = map[hostEnv]string{
cosmosDB: cosmosDBMsg,
documentDB: documentDBMsg,
}
)

// Topology represents a MongoDB deployment.
type Topology struct {
state int64
Expand Down Expand Up @@ -161,13 +187,13 @@ func New(cfg *Config) (*Topology, error) {
return t, nil
}

func mustLogTopologyMessage(topo *Topology) bool {
func mustLogTopologyMessage(topo *Topology, level logger.Level) bool {
return topo.cfg.logger != nil && topo.cfg.logger.LevelComponentEnabled(
logger.LevelDebug, logger.ComponentTopology)
level, logger.ComponentTopology)
}

func logTopologyMessage(topo *Topology, msg string, keysAndValues ...interface{}) {
topo.cfg.logger.Print(logger.LevelDebug,
func logTopologyMessage(topo *Topology, level logger.Level, msg string, keysAndValues ...interface{}) {
topo.cfg.logger.Print(level,
logger.ComponentTopology,
msg,
logger.SerializeTopology(logger.Topology{
Expand All @@ -183,8 +209,8 @@ func mustLogServerSelection(topo *Topology, level logger.Level) bool {

func logServerSelection(
ctx context.Context,
level logger.Level,
topo *Topology,
level logger.Level,
msg string,
srvSelector description.ServerSelector,
keysAndValues ...interface{},
Expand Down Expand Up @@ -224,7 +250,7 @@ func logServerSelectionSucceeded(

portInt64, _ := strconv.ParseInt(port, 10, 32)

logServerSelection(ctx, logger.LevelDebug, topo, logger.ServerSelectionSucceeded, srvSelector,
logServerSelection(ctx, topo, logger.LevelDebug, logger.ServerSelectionSucceeded, srvSelector,
logger.KeyServerHost, host,
logger.KeyServerPort, portInt64)
}
Expand All @@ -235,7 +261,7 @@ func logServerSelectionFailed(
srvSelector description.ServerSelector,
err error,
) {
logServerSelection(ctx, logger.LevelDebug, topo, logger.ServerSelectionFailed, srvSelector,
logServerSelection(ctx, topo, logger.LevelDebug, logger.ServerSelectionFailed, srvSelector,
logger.KeyFailure, err.Error())
}

Expand Down Expand Up @@ -321,13 +347,33 @@ func (t *Topology) Connect() error {
}

t.serversLock.Unlock()
if t.pollingRequired {
uri, err := url.Parse(t.cfg.URI)
if err != nil {
return err
uri, err := url.Parse(t.cfg.URI)
if err != nil {
return err
}
parsedHosts := strings.Split(uri.Host, ",")
if mustLogTopologyMessage(t, logger.LevelInfo) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: the only purpose of this block is to log third party connection information. It could be cleaner to create a separate function called logTopologyThirdPartyUsage to hold all of this logic:

if mustLogTopologyMessage(t, logger.LevelInfo) {
    logTopologyThirdPartyUsage(...)
}

hostSet := make(map[hostEnv]bool)
for _, host := range parsedHosts {
if h, _, err := net.SplitHostPort(host); err == nil {
host = h
}
for suffix, env := range thirdPartySuffixes {
if !strings.HasSuffix(host, suffix) {
continue
}
if logged, ok := hostSet[env]; ok && logged {
break
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: this else is not necessary, the logic can be written like this:

				if !strings.HasSuffix(host, suffix) {
					continue
				}
				
				if logged, ok := hostSet[env]; ok && logged {
					break
				} 
				
				hostSet[env] = true
				logTopologyMessage(t, logger.LevelInfo, thirdPartyMessages[env])

hostSet[env] = true
logTopologyMessage(t, logger.LevelInfo, thirdPartyMessages[env])
}
}
}
}
if t.pollingRequired {
// sanity check before passing the hostname to resolver
if parsedHosts := strings.Split(uri.Host, ","); len(parsedHosts) != 1 {
if len(parsedHosts) != 1 {
return fmt.Errorf("URI with SRV must include one and only one hostname")
}
_, _, err = net.SplitHostPort(uri.Host)
Expand Down Expand Up @@ -492,7 +538,7 @@ func (t *Topology) SelectServer(ctx context.Context, ss description.ServerSelect

if !doneOnce {
if mustLogServerSelection(t, logger.LevelDebug) {
logServerSelection(ctx, logger.LevelDebug, t, logger.ServerSelectionStarted, ss)
logServerSelection(ctx, t, logger.LevelDebug, logger.ServerSelectionStarted, ss)
}

// for the first pass, select a server from the current description.
Expand Down Expand Up @@ -531,7 +577,7 @@ func (t *Topology) SelectServer(ctx context.Context, ss description.ServerSelect
elapsed := time.Since(startTime)
remainingTimeMS := t.cfg.ServerSelectionTimeout - elapsed

logServerSelection(ctx, logger.LevelInfo, t, logger.ServerSelectionWaiting, ss,
logServerSelection(ctx, t, logger.LevelInfo, logger.ServerSelectionWaiting, ss,
logger.KeyRemainingTimeMS, remainingTimeMS.Milliseconds())
}

Expand Down Expand Up @@ -970,7 +1016,7 @@ func (t *Topology) publishServerClosedEvent(addr address.Address) {
t.cfg.ServerMonitor.ServerClosed(serverClosed)
}

if mustLogTopologyMessage(t) {
if mustLogTopologyMessage(t, logger.LevelDebug) {
serverHost, serverPort, err := net.SplitHostPort(addr.String())
if err != nil {
serverHost = addr.String()
Expand All @@ -979,7 +1025,7 @@ func (t *Topology) publishServerClosedEvent(addr address.Address) {

portInt64, _ := strconv.ParseInt(serverPort, 10, 32)

logTopologyMessage(t, logger.TopologyServerClosed,
logTopologyMessage(t, logger.LevelDebug, logger.TopologyServerClosed,
logger.KeyServerHost, serverHost,
logger.KeyServerPort, portInt64)
}
Expand All @@ -997,8 +1043,8 @@ func (t *Topology) publishTopologyDescriptionChangedEvent(prev description.Topol
t.cfg.ServerMonitor.TopologyDescriptionChanged(topologyDescriptionChanged)
}

if mustLogTopologyMessage(t) {
logTopologyMessage(t, logger.TopologyDescriptionChanged,
if mustLogTopologyMessage(t, logger.LevelDebug) {
logTopologyMessage(t, logger.LevelDebug, logger.TopologyDescriptionChanged,
logger.KeyPreviousDescription, prev.String(),
logger.KeyNewDescription, current.String())
}
Expand All @@ -1014,8 +1060,8 @@ func (t *Topology) publishTopologyOpeningEvent() {
t.cfg.ServerMonitor.TopologyOpening(topologyOpening)
}

if mustLogTopologyMessage(t) {
logTopologyMessage(t, logger.TopologyOpening)
if mustLogTopologyMessage(t, logger.LevelDebug) {
logTopologyMessage(t, logger.LevelDebug, logger.TopologyOpening)
}
}

Expand All @@ -1029,7 +1075,7 @@ func (t *Topology) publishTopologyClosedEvent() {
t.cfg.ServerMonitor.TopologyClosed(topologyClosed)
}

if mustLogTopologyMessage(t) {
logTopologyMessage(t, logger.TopologyClosed)
if mustLogTopologyMessage(t, logger.LevelDebug) {
logTopologyMessage(t, logger.LevelDebug, logger.TopologyClosed)
}
}
Loading
Loading