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 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
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
78 changes: 56 additions & 22 deletions x/mongo/driver/topology/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,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 @@ -176,15 +176,45 @@ func logTopologyMessage(topo *Topology, msg string, keysAndValues ...interface{}
}, keysAndValues...)...)
}

func logTopologyThirdPartyUsage(topo *Topology, parsedHosts []string) {
thirdPartyMessages := [2]string{
`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`,
`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`,
}

thirdPartySuffixes := map[string]int{
".cosmos.azure.com": 0,
".docdb.amazonaws.com": 1,
".docdb-elastic.amazonaws.com": 1,
}

hostSet := make([]bool, len(thirdPartyMessages))
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 hostSet[env] {
break
}
hostSet[env] = true
logTopologyMessage(topo, logger.LevelInfo, thirdPartyMessages[env])
}
}
}

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

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 +254,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 +265,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 +351,17 @@ func (t *Topology) Connect() error {
}

t.serversLock.Unlock()
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(...)
}

logTopologyThirdPartyUsage(t, parsedHosts)
}
if t.pollingRequired {
uri, err := url.Parse(t.cfg.URI)
if err != nil {
return err
}
// 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 +526,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 +565,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 +1004,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 +1013,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 +1031,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 +1048,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 +1063,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