Skip to content

Commit

Permalink
source-mongodb: log buildInfo on startup
Browse files Browse the repository at this point in the history
Log some basic information about the database that is being connected to, including its version and
maximum object size setting, if we are able to.
  • Loading branch information
williamhbaker committed Feb 22, 2024
1 parent bd27dce commit 0884e33
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions source-mongodb/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ func (d *driver) Pull(open *pc.Request_Open, stream *boilerplate.PullOutput) err
if err != nil {
return fmt.Errorf("connecting to database: %w", err)
}

// Log some basic information about the database we are connected to if possible. buildInfo is
// an administrator command so it may not be available on all databases / configurations.
if info, err := getBuildInfo(ctx, client); err != nil {
log.WithError(err).Info("could not query buildInfo")
} else {
log.WithFields(log.Fields{
"version": info.Version,
}).Info("buildInfo")
}

log.Info("connected to database")

defer func() {
Expand Down Expand Up @@ -797,3 +808,18 @@ func idToString(value interface{}) string {
}
return string(j)
}

type buildInfo struct {
Version string `bson:"version"`
}

func getBuildInfo(ctx context.Context, client *mongo.Client) (buildInfo, error) {
var info buildInfo
if res := client.Database("admin").RunCommand(ctx, bson.D{{"buildInfo", 1}}); res.Err() != nil {
return info, res.Err()
} else if err := res.Decode(&info); err != nil {
return info, err
} else {
return info, nil
}
}

0 comments on commit 0884e33

Please sign in to comment.