-
Notifications
You must be signed in to change notification settings - Fork 891
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
Changes from 5 commits
620f948
613cdc9
ed734f1
60d159d
ff92957
13ec0df
eef56f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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{ | ||
".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 | ||
|
@@ -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{ | ||
|
@@ -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{}, | ||
|
@@ -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) | ||
} | ||
|
@@ -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()) | ||
} | ||
|
||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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. | ||
|
@@ -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()) | ||
} | ||
|
||
|
@@ -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() | ||
|
@@ -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) | ||
} | ||
|
@@ -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()) | ||
} | ||
|
@@ -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) | ||
} | ||
} | ||
|
||
|
@@ -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) | ||
} | ||
} |
There was a problem hiding this comment.
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.