-
Notifications
You must be signed in to change notification settings - Fork 126
/
metrics.go
51 lines (41 loc) · 1.5 KB
/
metrics.go
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
package metrics
import (
"github.com/prometheus/client_golang/prometheus"
)
const MetricPrefix = "lookout_"
var dbOpenConnectionsDesc = prometheus.NewDesc(
MetricPrefix+"db_open_connections_total",
"Number of open connections to database",
nil,
nil,
)
var dbOpenConnectionsUtilizationDesc = prometheus.NewDesc(
MetricPrefix+"db_open_connections_utilisation",
"Percentage of connections used over total allowed connections to database",
nil,
nil,
)
type LookoutCollector interface {
Describe(desc chan<- *prometheus.Desc)
Collect(metrics chan<- prometheus.Metric)
}
type LookoutApiCollector struct {
lookoutDbMetricsProvider LookoutDbMetricsProvider
}
func ExposeLookoutMetrics(lookoutDbMetricsProvider LookoutDbMetricsProvider) LookoutCollector {
collector := &LookoutApiCollector{
lookoutDbMetricsProvider: lookoutDbMetricsProvider,
}
prometheus.MustRegister(collector)
return collector
}
func (c *LookoutApiCollector) Describe(desc chan<- *prometheus.Desc) {
desc <- dbOpenConnectionsDesc
desc <- dbOpenConnectionsUtilizationDesc
}
func (c *LookoutApiCollector) Collect(metrics chan<- prometheus.Metric) {
nOpenConnections := c.lookoutDbMetricsProvider.GetOpenConnections()
openConnectionsUtilization := c.lookoutDbMetricsProvider.GetOpenConnectionsUtilization()
metrics <- prometheus.MustNewConstMetric(dbOpenConnectionsDesc, prometheus.GaugeValue, float64(nOpenConnections))
metrics <- prometheus.MustNewConstMetric(dbOpenConnectionsUtilizationDesc, prometheus.GaugeValue, openConnectionsUtilization)
}