@@ -17,7 +17,7 @@ limitations under the License.
1717package collectors
1818
1919import (
20- "log "
20+ "fmt "
2121"regexp"
2222"strconv"
2323
3535)
3636
3737type (
38+ NginxStatusScraper struct {}
39+
3840nginxStatusCollector struct {
41+ scraper NginxStatusScraper
3942scrapeChan chan scrapeRequest
4043
4144data * nginxStatusData
4750connections * prometheus.Desc
4851}
4952
50- basicStatus struct {
53+ NginxStubStatus struct {
5154// Active total number of active connections
5255Active int
5356// Accepted total number of accepted client connections
@@ -65,6 +68,49 @@ type (
6568}
6669)
6770
71+ func toInt (data []string , pos int ) int {
72+ if len (data ) == 0 {
73+ return 0
74+ }
75+ if pos > len (data ) {
76+ return 0
77+ }
78+ if v , err := strconv .Atoi (data [pos ]); err == nil {
79+ return v
80+ }
81+ return 0
82+ }
83+
84+ func parse (data string ) * NginxStubStatus {
85+ acr := ac .FindStringSubmatch (data )
86+ sahrr := sahr .FindStringSubmatch (data )
87+ readingr := reading .FindStringSubmatch (data )
88+ writingr := writing .FindStringSubmatch (data )
89+ waitingr := waiting .FindStringSubmatch (data )
90+
91+ return & NginxStubStatus {
92+ toInt (acr , 1 ),
93+ toInt (sahrr , 1 ),
94+ toInt (sahrr , 2 ),
95+ toInt (sahrr , 3 ),
96+ toInt (readingr , 1 ),
97+ toInt (writingr , 1 ),
98+ toInt (waitingr , 1 ),
99+ }
100+ }
101+
102+ func (s * NginxStatusScraper ) Scrape () (* NginxStubStatus , error ) {
103+ klog .V (3 ).InfoS ("starting scraping socket" , "path" , nginx .StatusPath )
104+ status , data , err := nginx .NewGetStatusRequest (nginx .StatusPath )
105+ if err != nil {
106+ return nil , fmt .Errorf ("obtaining nginx status info: %w" , err )
107+ }
108+ if status < 200 || status >= 400 {
109+ return nil , fmt .Errorf ("obtaining nginx status info (status %v)" , status )
110+ }
111+ return parse (string (data )), nil
112+ }
113+
68114// NGINXStatusCollector defines a status collector interface
69115type NGINXStatusCollector interface {
70116prometheus.Collector
@@ -131,54 +177,14 @@ func (p nginxStatusCollector) Stop() {
131177close (p .scrapeChan )
132178}
133179
134- func toInt (data []string , pos int ) int {
135- if len (data ) == 0 {
136- return 0
137- }
138- if pos > len (data ) {
139- return 0
140- }
141- if v , err := strconv .Atoi (data [pos ]); err == nil {
142- return v
143- }
144- return 0
145- }
146-
147- func parse (data string ) * basicStatus {
148- acr := ac .FindStringSubmatch (data )
149- sahrr := sahr .FindStringSubmatch (data )
150- readingr := reading .FindStringSubmatch (data )
151- writingr := writing .FindStringSubmatch (data )
152- waitingr := waiting .FindStringSubmatch (data )
153-
154- return & basicStatus {
155- toInt (acr , 1 ),
156- toInt (sahrr , 1 ),
157- toInt (sahrr , 2 ),
158- toInt (sahrr , 3 ),
159- toInt (readingr , 1 ),
160- toInt (writingr , 1 ),
161- toInt (waitingr , 1 ),
162- }
163- }
164-
165180// nginxStatusCollector scrape the nginx status
166181func (p nginxStatusCollector ) scrape (ch chan <- prometheus.Metric ) {
167- klog .V (3 ).InfoS ("starting scraping socket" , "path" , nginx .StatusPath )
168- status , data , err := nginx .NewGetStatusRequest (nginx .StatusPath )
182+ s , err := p .scraper .Scrape ()
169183if err != nil {
170- log .Printf ("%v" , err )
171- klog .Warningf ("unexpected error obtaining nginx status info: %v" , err )
184+ klog .Warningf ("failed to scrape nginx status: %v" , err )
172185return
173186}
174187
175- if status < 200 || status >= 400 {
176- klog .Warningf ("unexpected error obtaining nginx status info (status %v)" , status )
177- return
178- }
179-
180- s := parse (string (data ))
181-
182188ch <- prometheus .MustNewConstMetric (p .data .connectionsTotal ,
183189prometheus .CounterValue , float64 (s .Accepted ), "accepted" )
184190ch <- prometheus .MustNewConstMetric (p .data .connectionsTotal ,
0 commit comments