11package haproxy
22
33import (
4+ "strings"
45 "time"
56
6- "github.com/davecgh/go-spew/spew"
77 "github.com/haproxytech/models"
88 "github.com/prometheus/client_golang/prometheus"
99 "github.com/prometheus/client_golang/prometheus/promauto"
1010 log "github.com/sirupsen/logrus"
1111)
1212
1313var (
14- opsProcessed = promauto .NewGaugeVec (prometheus.GaugeOpts {
15- Name : "http_requests_total" ,
14+ upMetric = promauto .NewGaugeVec (prometheus.GaugeOpts {
15+ Name : "haproxy_connect_up" ,
16+ Help : "The total number of http requests" ,
17+ }, []string {"service" })
18+
19+ reqOutRate = promauto .NewGaugeVec (prometheus.GaugeOpts {
20+ Name : "haproxy_connect_http_request_out_rate" ,
21+ Help : "The total number of http requests" ,
22+ }, []string {"service" , "target" })
23+ reqInRate = promauto .NewGaugeVec (prometheus.GaugeOpts {
24+ Name : "haproxy_connect_http_request_in_rate" ,
25+ Help : "The total number of http requests" ,
26+ }, []string {"service" })
27+ resInTotal = promauto .NewGaugeVec (prometheus.GaugeOpts {
28+ Name : "haproxy_connect_http_response_in_total" ,
29+ Help : "The total number of http requests" ,
30+ }, []string {"service" , "code" })
31+ resOutTotal = promauto .NewGaugeVec (prometheus.GaugeOpts {
32+ Name : "haproxy_connect_http_response_out_total" ,
33+ Help : "The total number of http requests" ,
34+ }, []string {"service" , "target" , "code" })
35+
36+ resTimeIn = promauto .NewGaugeVec (prometheus.GaugeOpts {
37+ Name : "haproxy_connect_http_response_in_avg_time_second" ,
38+ Help : "The total number of http requests" ,
39+ }, []string {"service" })
40+ resTimeOut = promauto .NewGaugeVec (prometheus.GaugeOpts {
41+ Name : "haproxy_connect_http_response_out_avg_time_second" ,
42+ Help : "The total number of http requests" ,
43+ }, []string {"service" , "target" })
44+
45+ connOutCount = promauto .NewGaugeVec (prometheus.GaugeOpts {
46+ Name : "haproxy_connect_connection_out_rate" ,
47+ Help : "The total number of http requests" ,
48+ }, []string {"service" , "target" })
49+ connInCount = promauto .NewGaugeVec (prometheus.GaugeOpts {
50+ Name : "haproxy_connect_connection_in_count" ,
51+ Help : "The total number of http requests" ,
52+ }, []string {"service" })
53+
54+ bytesInOut = promauto .NewGaugeVec (prometheus.GaugeOpts {
55+ Name : "haproxy_connect_bytes_in_out_total" ,
56+ Help : "The total number of http requests" ,
57+ }, []string {"service" , "target" })
58+ bytesOutOut = promauto .NewGaugeVec (prometheus.GaugeOpts {
59+ Name : "haproxy_connect_bytes_out_out_total" ,
60+ Help : "The total number of http requests" ,
61+ }, []string {"service" , "target" })
62+ bytesInIn = promauto .NewGaugeVec (prometheus.GaugeOpts {
63+ Name : "haproxy_connect_bytes_in_in_total" ,
64+ Help : "The total number of http requests" ,
65+ }, []string {"service" })
66+ bytesOutIn = promauto .NewGaugeVec (prometheus.GaugeOpts {
67+ Name : "haproxy_connect_bytes_out_in_total" ,
1668 Help : "The total number of http requests" ,
1769 }, []string {"service" })
1870)
1971
2072type Stats struct {
21- dpapi * dataplaneClient
73+ service string
74+ dpapi * dataplaneClient
2275}
2376
2477func (s * Stats ) Run () {
78+ upMetric .WithLabelValues (s .service ).Set (1 )
2579 for {
2680 time .Sleep (time .Second )
2781 stats , err := s .dpapi .Stats ()
@@ -34,7 +88,65 @@ func (s *Stats) Run() {
3488}
3589
3690func (s * Stats ) handle (stats []models.NativeStat ) {
37- for _ , s := range stats {
38- spew .Dump (s )
91+ for _ , stats := range stats {
92+ switch stats .Type {
93+ case models .NativeStatTypeFrontend :
94+ s .handleFrontend (stats )
95+ case models .NativeStatTypeBackend :
96+ s .handlebackend (stats )
97+ case models .NativeStatTypeServer :
98+ s .handleServer (stats )
99+ }
100+ }
101+ }
102+
103+ func statVal (i * int64 ) float64 {
104+ if i == nil {
105+ return 0
106+ }
107+ return float64 (* i )
108+ }
109+
110+ func (s * Stats ) handleFrontend (stats models.NativeStat ) {
111+ targetService := strings .TrimPrefix (stats .Name , "front_" )
112+
113+ if targetService == "downstream" {
114+ reqInRate .WithLabelValues (s .service ).Set (statVal (stats .Stats .Rate ))
115+ connInCount .WithLabelValues (s .service ).Set (statVal (stats .Stats .Scur ))
116+ bytesInIn .WithLabelValues (s .service ).Set (statVal (stats .Stats .Bin ))
117+ bytesOutIn .WithLabelValues (s .service ).Set (statVal (stats .Stats .Bout ))
118+
119+ resInTotal .WithLabelValues (s .service , "1xx" ).Set (statVal (stats .Stats .Hrsp1xx ))
120+ resInTotal .WithLabelValues (s .service , "2xx" ).Set (statVal (stats .Stats .Hrsp2xx ))
121+ resInTotal .WithLabelValues (s .service , "3xx" ).Set (statVal (stats .Stats .Hrsp3xx ))
122+ resInTotal .WithLabelValues (s .service , "4xx" ).Set (statVal (stats .Stats .Hrsp4xx ))
123+ resInTotal .WithLabelValues (s .service , "5xx" ).Set (statVal (stats .Stats .Hrsp5xx ))
124+ resInTotal .WithLabelValues (s .service , "other" ).Set (statVal (stats .Stats .HrspOther ))
125+ } else {
126+ reqOutRate .WithLabelValues (s .service , targetService ).Set (statVal (stats .Stats .Rate ))
127+ connOutCount .WithLabelValues (s .service , targetService ).Set (statVal (stats .Stats .Scur ))
128+ bytesInOut .WithLabelValues (s .service , targetService ).Set (statVal (stats .Stats .Bin ))
129+ bytesOutOut .WithLabelValues (s .service , targetService ).Set (statVal (stats .Stats .Bout ))
130+
131+ resOutTotal .WithLabelValues (s .service , targetService , "1xx" ).Set (statVal (stats .Stats .Hrsp1xx ))
132+ resOutTotal .WithLabelValues (s .service , targetService , "2xx" ).Set (statVal (stats .Stats .Hrsp2xx ))
133+ resOutTotal .WithLabelValues (s .service , targetService , "3xx" ).Set (statVal (stats .Stats .Hrsp3xx ))
134+ resOutTotal .WithLabelValues (s .service , targetService , "4xx" ).Set (statVal (stats .Stats .Hrsp4xx ))
135+ resOutTotal .WithLabelValues (s .service , targetService , "5xx" ).Set (statVal (stats .Stats .Hrsp5xx ))
136+ resOutTotal .WithLabelValues (s .service , targetService , "other" ).Set (statVal (stats .Stats .HrspOther ))
39137 }
40138}
139+
140+ func (s * Stats ) handlebackend (stats models.NativeStat ) {
141+ targetService := strings .TrimPrefix (stats .Name , "back_" )
142+
143+ if targetService == "downstream" {
144+ resTimeIn .WithLabelValues (s .service ).Set (statVal (stats .Stats .Ttime ) / 1000 )
145+ } else {
146+ resTimeOut .WithLabelValues (s .service , targetService ).Set (statVal (stats .Stats .Ttime ) / 1000 )
147+ }
148+ }
149+
150+ func (s * Stats ) handleServer (stats models.NativeStat ) {
151+ resTimeOut .WithLabelValues (s .service , stats .Name ).Set (statVal (stats .Stats .Ttime ) / 1000 )
152+ }
0 commit comments