File: conn_pool_stats.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 (109 lines) | stat: -rw-r--r-- 3,408 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package collector

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

// server connections -- all of these!
var (
	syncClientConnections = prometheus.NewGauge(prometheus.GaugeOpts{
		Namespace: Namespace,
		Subsystem: "connpoolstats",
		Name:      "connection_sync",
		Help:      "Corresponds to the total number of client connections to mongo.",
	})

	numAScopedConnections = prometheus.NewGauge(prometheus.GaugeOpts{
		Namespace: Namespace,
		Subsystem: "connpoolstats",
		Name:      "connections_scoped_sync",
		Help:      "Corresponds to the number of active and stored outgoing scoped synchronous connections from the current instance to other members of the sharded cluster or replica set.",
	})

	totalInUse = prometheus.NewGauge(prometheus.GaugeOpts{
		Namespace: Namespace,
		Subsystem: "connpoolstats",
		Name:      "connections_in_use",
		Help:      "Corresponds to the total number of client connections to mongo currently in use.",
	})

	totalAvailable = prometheus.NewGauge(prometheus.GaugeOpts{
		Namespace: Namespace,
		Subsystem: "connpoolstats",
		Name:      "connections_available",
		Help:      "Corresponds to the total number of client connections to mongo that are currently available.",
	})

	totalCreated = prometheus.NewGauge(prometheus.GaugeOpts{
		Namespace: Namespace,
		Subsystem: "connpoolstats",
		Name:      "connections_created_total",
		Help:      "Corresponds to the total number of client connections to mongo created since instance start",
	})
)

// ServerStatus keeps the data returned by the serverStatus() method.
type ConnPoolStats struct {
	SyncClientConnections float64 `bson:"numClientConnections"`
	ASScopedConnections   float64 `bson:"numAScopedConnections"`
	TotalInUse            float64 `bson:"totalInUse"`
	TotalAvailable        float64 `bson:"totalAvailable"`
	TotalCreated          float64 `bson:"totalCreated"`

	Hosts map[string]*HostConnPoolStats `bson:hosts"`
	// TODO:? not sure if *this* level of granularity is helpful
	//ReplicaSets map[string]ConnPoolReplicaSetStats `bson:"replicaSets"`
}

// Export exports the server status to be consumed by prometheus.
func (stats *ConnPoolStats) Export(ch chan<- prometheus.Metric) {
	syncClientConnections.Set(stats.SyncClientConnections)
	syncClientConnections.Collect(ch)

	numAScopedConnections.Set(stats.ASScopedConnections)
	numAScopedConnections.Collect(ch)

	totalInUse.Set(stats.TotalInUse)
	totalInUse.Collect(ch)

	totalAvailable.Set(stats.TotalAvailable)
	totalAvailable.Collect(ch)

	totalCreated.Set(stats.TotalCreated)
	totalCreated.Collect(ch)

	for hostname, hostStat := range stats.Hosts {
		hostStat.Export(hostname, ch)
	}
}

// Describe describes the server status for prometheus.
func (stats *ConnPoolStats) Describe(ch chan<- *prometheus.Desc) {
	syncClientConnections.Describe(ch)

	numAScopedConnections.Describe(ch)

	totalInUse.Describe(ch)

	totalAvailable.Describe(ch)

	totalCreated.Describe(ch)

	for _, hostStat := range stats.Hosts {
		hostStat.Describe(ch)
	}
}

// GetServerStatus returns the server status info.
func GetConnPoolStats(session *mgo.Session) *ConnPoolStats {
	result := &ConnPoolStats{}
	err := session.DB("admin").Run(bson.D{{"connPoolStats", 1}, {"recordStats", 0}}, result)
	if err != nil {
		glog.Error("Failed to get server status.")
		return nil
	}
	return result
}