Skip to content

Commit

Permalink
Merge pull request #23 from amsingh-wish/amsingh-add-currentop-metric
Browse files Browse the repository at this point in the history
Added currentOp info to metrics
  • Loading branch information
zengmin-wish authored May 26, 2020
2 parents 9583ad7 + 9108736 commit bb3238d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
48 changes: 48 additions & 0 deletions collector/current_op.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package collector

import (
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
)

var (
instanceFsyncLockWorker = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: "instance",
Name: "fsync_lock_worker",
Help: "The value of the fsync field corresponds to whether the fsyncLockWorker is active or not.",
})
)

// CurrentOp keeps the data returned by the currentOp() method.
type CurrentOp struct {
FsyncLockWorker bool `bson:"fsyncLock"`
}

// Export exports the current operation status to be consumed by prometheus.
func (status *CurrentOp) Export(ch chan<- prometheus.Metric) {
var floatVar = float64(0)
if (status.FsyncLockWorker){
floatVar = float64(1)
}
instanceFsyncLockWorker.Set(floatVar)
instanceFsyncLockWorker.Collect(ch)
}

// Describe describes the current operation status for prometheus.
func (status *CurrentOp) Describe(ch chan<- *prometheus.Desc) {
instanceFsyncLockWorker.Describe(ch)
}

// GetCurrentOp returns the current operation info.
func GetCurrentOp(session *mgo.Session, maxTimeMS int64) *CurrentOp {
result := &CurrentOp{}
err := session.DB("admin").Run(bson.D{{"currentOp", 1}, {"notexist", 0}}, result)
if err != nil {
glog.Error("Failed to get currentOp status.")
return nil
}
return result
}
12 changes: 12 additions & 0 deletions collector/mongodb_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func NewMongodbCollector(opts MongodbCollectorOpts) *MongodbCollector {

// Describe describes all mongodb's metrics.
func (exporter *MongodbCollector) Describe(ch chan<- *prometheus.Desc) {
(&CurrentOp{}).Describe(ch)
(&ServerStatus{}).Describe(ch)
(&ReplSetStatus{}).Describe(ch)
(&ReplSetConf{}).Describe(ch)
Expand All @@ -98,6 +99,8 @@ func (exporter *MongodbCollector) Collect(ch chan<- prometheus.Metric) {
upGauge.Reset()
collectorLock.Unlock()
defer mongoSess.Close()
glog.Info("Collecting CurrentOp Info")
exporter.collectCurrentOp(mongoSess, ch)
glog.Info("Collecting Server Status")
exporter.collectServerStatus(mongoSess, ch)
if exporter.Opts.CollectReplSet {
Expand Down Expand Up @@ -152,6 +155,15 @@ func (exporter *MongodbCollector) Collect(ch chan<- prometheus.Metric) {
}
}

func (exporter *MongodbCollector) collectCurrentOp(session *mgo.Session, ch chan<- prometheus.Metric) *CurrentOp {
currentOpInfo := GetCurrentOp(session, exporter.Opts.MaxTimeMS)
if currentOpInfo != nil {
glog.Info("exporting CurrentOp Metrics")
currentOpInfo.Export(ch)
}
return currentOpInfo
}

func (exporter *MongodbCollector) collectServerStatus(session *mgo.Session, ch chan<- prometheus.Metric) *ServerStatus {
serverStatus := GetServerStatus(session, exporter.Opts.MaxTimeMS)
if serverStatus != nil {
Expand Down

0 comments on commit bb3238d

Please sign in to comment.