File: profile_status.go

package info (click to toggle)
prometheus-mongodb-exporter 1.0.0%2Bgit20180522.e755a44-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 668 kB
  • sloc: sh: 65; makefile: 27
file content (44 lines) | stat: -rw-r--r-- 1,192 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package collector

import (
	"github.com/golang/glog"
	"github.com/prometheus/client_golang/prometheus"
	mgo "gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"time"
)

var (
	profileCount = prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Namespace: Namespace,
		Subsystem: "profile",
		Name:      "slow_query_30s_count",
		Help:      "The number of slow queries in this database during last 30 seconds",
	}, []string{"database"})
)

type ProfileStatus struct {
	Name  string `bson:"database"`
	Count int    `bson:"count"`
}

func (profileStatus *ProfileStatus) Export(ch chan<- prometheus.Metric) {
	profileCount.WithLabelValues(profileStatus.Name).Set(float64(profileStatus.Count))
	profileCount.Collect(ch)
	profileCount.Reset()
}

func (profileStatus *ProfileStatus) Describe(ch chan<- *prometheus.Desc) {
	profileCount.Describe(ch)
}

func CollectProfileStatus(session *mgo.Session, db string, ch chan<- prometheus.Metric) {
	ts := time.Now().Add(-time.Duration(time.Second * 30))
	count, err := session.DB(db).C("system.profile").Find(bson.M{"ts": bson.M{"$gt": ts}}).Count()
	if err != nil {
		glog.Error(err)
		return
	}
	profileStatus := ProfileStatus{db, count}
	profileStatus.Export(ch)
}