Skip to content
Closed
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
29 changes: 25 additions & 4 deletions collector/mongodb_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package collector

import (
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -57,6 +56,7 @@ type MongodbCollector struct {
scrapesTotal prometheus.Counter
lastScrapeError prometheus.Gauge
lastScrapeDurationSeconds prometheus.Gauge
mongoSess *mgo.Session
}

// NewMongodbCollector returns a new instance of a MongodbCollector.
Expand Down Expand Up @@ -87,6 +87,28 @@ func NewMongodbCollector(opts MongodbCollectorOpts) *MongodbCollector {
return exporter
}

// getSession returns the cached *mgo.Session (after a test ping)
// or creates a new session and returns it. The cached session is
// reconnected if the ping to it fails.
func (exporter *MongodbCollector) getSession() *mgo.Session {
if exporter.mongoSess != nil {
err := exporter.mongoSess.Ping()
if err == nil {
return exporter.mongoSess
}
exporter.mongoSess.Close()
}
exporter.mongoSess = shared.MongoSession(exporter.Opts.toSessionOps())
return exporter.mongoSess
}

// Close cleanly closes the mongo session if it exists.
func (exporter *MongodbCollector) Close() {
if exporter.mongoSess != nil {
exporter.mongoSess.Close()
}
}

// Describe sends the super-set of all possible descriptors of metrics collected by this Collector
// to the provided channel and returns once the last descriptor has been sent.
// Part of prometheus.Collector interface.
Expand Down Expand Up @@ -139,12 +161,11 @@ func (exporter *MongodbCollector) scrape(ch chan<- prometheus.Metric) {
}
}(time.Now())

mongoSess := shared.MongoSession(exporter.Opts.toSessionOps())
mongoSess := exporter.getSession()
if mongoSess == nil {
err = errors.New("can't create mongo session")
log.Errorf("can't create mongo session")
return
}
defer mongoSess.Close()

var serverVersion string
serverVersion, err = shared.MongoSessionServerVersion(mongoSess)
Expand Down
7 changes: 4 additions & 3 deletions mongodb_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ func startWebServer() {
}

handler := prometheusHandler()

registerCollector()
collector := registerCollector()
defer collector.Close()

if (*sslCertFileF == "") != (*sslKeyFileF == "") {
log.Fatal("One of the flags -web.ssl-cert-file or -web.ssl-key-file is missing to enable HTTPS/TLS")
Expand Down Expand Up @@ -210,7 +210,7 @@ func startWebServer() {
}
}

func registerCollector() {
func registerCollector() *collector.MongodbCollector {
mongodbCollector := collector.NewMongodbCollector(collector.MongodbCollectorOpts{
URI: *uriF,
TLSConnection: *tlsF,
Expand All @@ -220,6 +220,7 @@ func registerCollector() {
TLSHostnameValidation: !(*tlsDisableHostnameValidationF),
})
prometheus.MustRegister(mongodbCollector)
return mongodbCollector
}

func main() {
Expand Down
2 changes: 2 additions & 0 deletions shared/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func MongoSession(opts MongoSessionOpts) *mgo.Session {
return nil
}
session.SetMode(mgo.Eventual, true)
session.SetPoolLimit(2)
session.SetPrefetch(0.00)
session.SetSyncTimeout(syncMongodbTimeout)
session.SetSocketTimeout(0)
return session
Expand Down