11package server
22
33import (
4+ "strings"
45 "time"
56
67 "github.com/cenkalti/log"
@@ -10,35 +11,63 @@ import (
1011
1112// PrometheusMetric is the type that contains all metrics those will be exposed.
1213type PrometheusMetric struct {
13- connCount prometheus.Gauge
14- hostConn * prometheus.GaugeVec
15- dbCount * prometheus.GaugeVec
16- Server * Server
14+ conns prometheus.Gauge
15+ host * prometheus.GaugeVec
16+ db * prometheus.GaugeVec
17+ command * prometheus.GaugeVec
18+ state * prometheus.GaugeVec
19+ cmdline * prometheus.GaugeVec
20+ Server * Server
1721}
1822
1923// NewPrometheusMetric is the constructor function of PrometheusMetric
2024func NewPrometheusMetric (server * Server ) * PrometheusMetric {
2125 return & PrometheusMetric {
2226 Server : server ,
23- connCount : promauto .NewGauge (prometheus.GaugeOpts {
24- Name : "kimo_conn_count " ,
25- Help : "Total number of db process " ,
27+ conns : promauto .NewGauge (prometheus.GaugeOpts {
28+ Name : "kimo_conns_total " ,
29+ Help : "Total number of db processes (conns) " ,
2630 }),
27- hostConn : promauto .NewGaugeVec (prometheus.GaugeOpts {
28- Name : "kimo_host_conns " ,
29- Help : "Connection count per host" ,
31+ host : promauto .NewGaugeVec (prometheus.GaugeOpts {
32+ Name : "kimo_conns_host " ,
33+ Help : "Conns per host" ,
3034 },
3135 []string {
3236 "host" ,
3337 },
3438 ),
35- dbCount : promauto .NewGaugeVec (prometheus.GaugeOpts {
36- Name : "kimo_db_count " ,
37- Help : "Total number of connections per db" ,
39+ db : promauto .NewGaugeVec (prometheus.GaugeOpts {
40+ Name : "kimo_conns_db " ,
41+ Help : "Conns per db" ,
3842 },
3943 []string {
4044 "db" ,
41- }),
45+ },
46+ ),
47+ command : promauto .NewGaugeVec (prometheus.GaugeOpts {
48+ Name : "kimo_conns_command" ,
49+ Help : "Conns per command" ,
50+ },
51+ []string {
52+ "command" ,
53+ },
54+ ),
55+ state : promauto .NewGaugeVec (prometheus.GaugeOpts {
56+ Name : "kimo_conns_state" ,
57+ Help : "Conns per state" ,
58+ },
59+ []string {
60+ "state" ,
61+ },
62+ ),
63+ cmdline : promauto .NewGaugeVec (prometheus.GaugeOpts {
64+ Name : "kimo_conns_cmdline" ,
65+ Help : "conns per cmdline" ,
66+ },
67+ []string {
68+ "cmdline" ,
69+ },
70+ ),
4271 }
4372}
4473
@@ -63,27 +92,35 @@ func (pm *PrometheusMetric) Set() {
6392 return
6493 }
6594 log .Debugf ("Found '%d' processes. Setting metrics...\n " , len (pm .Server .Processes ))
66- pm .connCount .Set (float64 (len (ps )))
95+ pm .conns .Set (float64 (len (ps )))
6796
97+ // todo: too much duplication
6898 var metricM = map [string ]map [string ]int {}
69- // todo: keys should be constant at somewhere else and we should iterate through them
7099 metricM ["db" ] = map [string ]int {}
71100 metricM ["host" ] = map [string ]int {}
101+ metricM ["state" ] = map [string ]int {}
102+ metricM ["command" ] = map [string ]int {}
103+ metricM ["cmdline" ] = map [string ]int {}
72104
73105 for _ , p := range ps {
74- // todo: keys should be constant at somewhere else and we should iterate through them
75106 metricM ["db" ][p .DB ]++
76107 metricM ["host" ][p .Host ]++
108+ metricM ["command" ][p .Command ]++
109+ metricM ["state" ][p .State ]++
110+ metricM ["cmdline" ][strings .Join (p .CmdLine , " " )]++
77111 }
78112 for k , v := range metricM {
79- // todo: define a new type for metrics and contain name, protmetheus metric type. So, we won't have to
80- // do manuel if-else check here.
81- if k == "db" {
82- setGaugeVec (k , v , pm .dbCount )
83- } else {
84- if k == "host" {
85- setGaugeVec (k , v , pm .hostConn )
86- }
113+ switch k {
114+ case "db" :
115+ setGaugeVec (k , v , pm .db )
116+ case "host" :
117+ setGaugeVec (k , v , pm .host )
118+ case "command" :
119+ setGaugeVec (k , v , pm .command )
120+ case "state" :
121+ setGaugeVec (k , v , pm .state )
122+ case "cmdline" :
123+ setGaugeVec (k , v , pm .cmdline )
87124 }
88125 }
89126}
0 commit comments