Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Mgo prometheus collector
package mongo
import (
type mgoCollector struct {
clusterDesc *prometheus.Desc
masterConnDesc *prometheus.Desc
slaveConnDesc *prometheus.Desc
sentOpDesc *prometheus.Desc
receivedOpDesc *prometheus.Desc
receivedDocDesc *prometheus.Desc
socketsAliveDesc *prometheus.Desc
socketsInUseDesc *prometheus.Desc
socketRefDesc *prometheus.Desc
// NewMgoCollector returns a collector which exports metrics about mgo stats.
func NewMgoCollector() prometheus.Collector {
return &mgoCollector{
clusterDesc: prometheus.NewDesc(
"Mgo Cluster from stats",
nil, nil,
masterConnDesc: prometheus.NewDesc(
"Mgo Master Conn from stats",
nil, nil,
slaveConnDesc: prometheus.NewDesc(
"Mgo Slave Conn from stats",
nil, nil,
sentOpDesc: prometheus.NewDesc(
"Mgo sent op from stats",
nil, nil,
receivedOpDesc: prometheus.NewDesc(
"Mgo received op from stats",
nil, nil,
receivedDocDesc: prometheus.NewDesc(
"Mgo received doc from stats",
nil, nil,
socketsAliveDesc: prometheus.NewDesc(
"Mgo sockets alive from stats",
nil, nil,
socketsInUseDesc: prometheus.NewDesc(
"Mgo sockets in use from stats",
nil, nil,
socketRefDesc: prometheus.NewDesc(
"Mgo socket ref from stats",
nil, nil,
// Describe returns all descriptions of the collector.
func (c *mgoCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.clusterDesc
ch <- c.masterConnDesc
ch <- c.slaveConnDesc
ch <- c.sentOpDesc
ch <- c.receivedOpDesc
ch <- c.receivedDocDesc
ch <- c.socketsAliveDesc
ch <- c.socketsInUseDesc
ch <- c.socketRefDesc
// Collect returns the current state of all metrics of the collector.
func (c *mgoCollector) Collect(ch chan<- prometheus.Metric) {
stats := mgo.GetStats()
ch <- prometheus.MustNewConstMetric(c.clusterDesc, prometheus.GaugeValue, float64(stats.Clusters))
ch <- prometheus.MustNewConstMetric(c.masterConnDesc, prometheus.GaugeValue, float64(stats.MasterConns))
ch <- prometheus.MustNewConstMetric(c.slaveConnDesc, prometheus.GaugeValue, float64(stats.SlaveConns))
ch <- prometheus.MustNewConstMetric(c.sentOpDesc, prometheus.GaugeValue, float64(stats.SentOps))
ch <- prometheus.MustNewConstMetric(c.receivedOpDesc, prometheus.GaugeValue, float64(stats.ReceivedOps))
ch <- prometheus.MustNewConstMetric(c.receivedDocDesc, prometheus.GaugeValue, float64(stats.ReceivedDocs))
ch <- prometheus.MustNewConstMetric(c.socketsAliveDesc, prometheus.GaugeValue, float64(stats.SocketsAlive))
ch <- prometheus.MustNewConstMetric(c.socketsInUseDesc, prometheus.GaugeValue, float64(stats.SocketsInUse))
ch <- prometheus.MustNewConstMetric(c.socketRefDesc, prometheus.GaugeValue, float64(stats.SocketRefs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment