Skip to content

Commit

Permalink
contrib/globalsign/mgo: check for error after dialing to avoid panic (#…
Browse files Browse the repository at this point in the history
…937)

This commit fixes a bug causing a panic when mgo fails to dial the database.

Fixes #870
  • Loading branch information
knusbaum authored Jul 22, 2021
1 parent 9a5c7ca commit 63dea35
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 8 additions & 2 deletions contrib/globalsign/mgo/mgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ import (
// for tracing.
func Dial(url string, opts ...DialOption) (*Session, error) {
session, err := mgo.Dial(url)
info, _ := session.BuildInfo()
if err != nil {
return nil, err
}
version := "unknown"
if info, err := session.BuildInfo(); err == nil {
version = info.Version
}
s := &Session{
Session: session,
cfg: newConfig(),
tags: map[string]string{
"hosts": strings.Join(session.LiveServers(), ", "),
"mgo_version": info.Version,
"mgo_version": version,
},
}
for _, fn := range opts {
Expand Down
4 changes: 4 additions & 0 deletions contrib/globalsign/mgo/mgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ func TestCollection_Bulk(t *testing.T) {
assert.Equal("mongodb.query", spans[0].OperationName())
}

func TestBadDial(t *testing.T) {
assert.NotPanics(t, func() { Dial("this_is_not_valid?foo&bar") })
}

func TestAnalyticsSettings(t *testing.T) {
assertRate := func(t *testing.T, mt mocktracer.Tracer, rate interface{}, opts ...DialOption) {
assert := assert.New(t)
Expand Down

0 comments on commit 63dea35

Please sign in to comment.