Skip to content
Merged
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
53 changes: 28 additions & 25 deletions collector/mongod/oplog_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
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"
"gopkg.in/mgo.v2/bson"
)

var (
oplogDb = "local"
oplogCollection = "oplog.rs"
oplogStatusCount = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: "replset_oplog",
Expand Down Expand Up @@ -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
}

Expand Down
9 changes: 9 additions & 0 deletions shared/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"runtime"
"strconv"

"gopkg.in/mgo.v2"
)

func LoadCaFrom(pemFile string) (*x509.CertPool, error) {
Expand All @@ -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))
}