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

add global mongo keep-connection session option #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 31 additions & 3 deletions collector/mongodb_collector.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package collector

import (
"errors"
"time"

"github.com/dcu/mongodb_exporter/shared"
Expand Down Expand Up @@ -40,6 +41,7 @@ type MongodbCollectorOpts struct {
UserName string
AuthMechanism string
SocketTimeout time.Duration
KeepConnection bool
}

func (in MongodbCollectorOpts) toSessionOps() shared.MongoSessionOpts {
Expand All @@ -52,6 +54,7 @@ func (in MongodbCollectorOpts) toSessionOps() shared.MongoSessionOpts {
UserName: in.UserName,
AuthMechanism: in.AuthMechanism,
SocketTimeout: in.SocketTimeout,
KeepConnection: in.KeepConnection,
}
}

Expand Down Expand Up @@ -81,14 +84,39 @@ func (exporter *MongodbCollector) Describe(ch chan<- *prometheus.Desc) {
}
}

var mongoGlobalSession *mgo.Session

func checkGlobalSession(exporter *MongodbCollector) error {
if mongoGlobalSession == nil {
mongoGlobalSession = shared.MongoSession(exporter.Opts.toSessionOps())
if mongoGlobalSession == nil {
return errors.New("init mongo global session failed")
}
}
err := mongoGlobalSession.Ping()
if err != nil {
return err
}
return nil
}

// Collect collects all mongodb's metrics.
func (exporter *MongodbCollector) Collect(ch chan<- prometheus.Metric) {
mongoSess := shared.MongoSession(exporter.Opts.toSessionOps())
if mongoSess != nil {
var mongoSess *mgo.Session
var errCheckSession error
if exporter.Opts.KeepConnection {
mongoSess = mongoGlobalSession
errCheckSession = checkGlobalSession(exporter)
} else {
mongoSess = shared.MongoSession(exporter.Opts.toSessionOps())
}
if mongoSess != nil && errCheckSession == nil {
upGauge.WithLabelValues().Set(float64(1))
upGauge.Collect(ch)
upGauge.Reset()
defer mongoSess.Close()
if !exporter.Opts.KeepConnection {
defer mongoSess.Close()
}
glog.Info("Collecting Server Status")
exporter.collectServerStatus(mongoSess, ch)
if exporter.Opts.CollectReplSet {
Expand Down
2 changes: 2 additions & 0 deletions mongodb_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var (
mongodbCollectProfileMetrics = flag.Bool("mongodb.collect.profile", false, "Collect MongoDB profile metrics")
mongodbCollectConnPoolStats = flag.Bool("mongodb.collect.connpoolstats", false, "Collect MongoDB connpoolstats")
mongodbSocketTimeout = flag.Duration("mongodb.socket-timeout", 0, "timeout for socket operations to mongodb")
mongodbKeepConnection = flag.Bool("mongodb.keep-connection", false, "keep one client connection to mongodb")
version = flag.Bool("version", false, "Print mongodb_exporter version")
)

Expand Down Expand Up @@ -164,6 +165,7 @@ func registerCollector() {
UserName: *mongodbUserName,
AuthMechanism: *mongodbAuthMechanism,
SocketTimeout: *mongodbSocketTimeout,
KeepConnection: *mongodbKeepConnection,
})
prometheus.MustRegister(mongodbCollector)
}
Expand Down
5 changes: 5 additions & 0 deletions shared/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type MongoSessionOpts struct {
UserName string
AuthMechanism string
SocketTimeout time.Duration
KeepConnection bool
}

// MongoSession creates a Mongo session
Expand Down Expand Up @@ -57,6 +58,10 @@ func MongoSession(opts MongoSessionOpts) *mgo.Session {
session.SetMode(mgo.Eventual, true)
session.SetSyncTimeout(syncMongodbTimeout)
session.SetSocketTimeout(opts.SocketTimeout)
if opts.KeepConnection {
session.SetMode(mgo.Eventual, false) // refresh doesn't work well while reconnect
session.SetPoolLimit(1) // keep 1 global Connection
}
return session
}

Expand Down