diff --git a/collector/mongod/oplog_status.go b/collector/mongod/oplog_status.go index 9d842aabc..aeadf414d 100644 --- a/collector/mongod/oplog_status.go +++ b/collector/mongod/oplog_status.go @@ -15,6 +15,7 @@ package collector_mongod import ( + "github.com/percona/mongodb_exporter/shared" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/log" "gopkg.in/mgo.v2" @@ -22,6 +23,8 @@ import ( ) var ( + oplogDb = "local" + oplogCollection = "oplog.rs" oplogStatusCount = prometheus.NewGauge(prometheus.GaugeOpts{ Namespace: Namespace, Subsystem: "replset_oplog", @@ -69,44 +72,44 @@ func BsonMongoTimestampToUnix(timestamp bson.MongoTimestamp) float64 { return float64(timestamp >> 32) } -func GetOplogTimestamps(session *mgo.Session) (*OplogTimestamps, error) { - oplogTimestamps := &OplogTimestamps{} - var err error - - // retry once if there is an error - var tries int64 = 0 - var head_result struct { +func getOplogTailOrHeadTimestamp(session *mgo.Session, returnHead bool) (float64, error) { + var result struct { Timestamp bson.MongoTimestamp `bson:"ts"` } - for tries < 2 { - err = session.DB("local").C("oplog.rs").Find(nil).Sort("-$natural").Limit(1).One(&head_result) - if err == nil { - break - } - tries += 1 - } - if err != nil { - return oplogTimestamps, err + + var sortCond string = "$natural" + if returnHead { + sortCond = "-$natural" } // retry once if there is an error - tries = 0 - var tail_result struct { - Timestamp bson.MongoTimestamp `bson:"ts"` - } + var err error + var tries int64 = 0 for tries < 2 { - err = session.DB("local").C("oplog.rs").Find(nil).Sort("$natural").Limit(1).One(&tail_result) + findQuery := session.DB(oplogDb).C(oplogCollection).Find(nil).Sort(sortCond).Limit(1) + err = shared.AddCodeCommentToQuery(findQuery).One(&result) if err == nil { break } tries += 1 } + + return BsonMongoTimestampToUnix(result.Timestamp), err +} + +func GetOplogTimestamps(session *mgo.Session) (*OplogTimestamps, error) { + headTs, err := getOplogTailOrHeadTimestamp(session, true) + if err != nil { + return nil, err + } + tailTs, err := getOplogTailOrHeadTimestamp(session, false) if err != nil { - return oplogTimestamps, err + return nil, err + } + oplogTimestamps := &OplogTimestamps{ + Head: headTs, + Tail: tailTs, } - - oplogTimestamps.Tail = BsonMongoTimestampToUnix(tail_result.Timestamp) - oplogTimestamps.Head = BsonMongoTimestampToUnix(head_result.Timestamp) return oplogTimestamps, err } diff --git a/shared/utils.go b/shared/utils.go index 4c8c71ecd..372a456ee 100644 --- a/shared/utils.go +++ b/shared/utils.go @@ -18,6 +18,10 @@ import ( "crypto/tls" "crypto/x509" "io/ioutil" + "runtime" + "strconv" + + "gopkg.in/mgo.v2" ) func LoadCaFrom(pemFile string) (*x509.CertPool, error) { @@ -37,3 +41,8 @@ func LoadKeyPairFrom(pemFile string, privateKeyPemFile string) (tls.Certificate, } return tls.LoadX509KeyPair(pemFile, targetPrivateKeyPemFile) } + +func AddCodeCommentToQuery(query *mgo.Query) *mgo.Query { + _, fileName, lineNum, _ := runtime.Caller(1) + return query.Comment(fileName + ":" + strconv.Itoa(lineNum)) +}