@@ -17,7 +17,7 @@ limitations under the License.
17
17
package collectors
18
18
19
19
import (
20
- "log "
20
+ "fmt "
21
21
"regexp"
22
22
"strconv"
23
23
35
35
)
36
36
37
37
type (
38
+ NginxStatusScraper struct {}
39
+
38
40
nginxStatusCollector struct {
41
+ scraper NginxStatusScraper
39
42
scrapeChan chan scrapeRequest
40
43
41
44
data * nginxStatusData
47
50
connections * prometheus.Desc
48
51
}
49
52
50
- basicStatus struct {
53
+ NginxStubStatus struct {
51
54
// Active total number of active connections
52
55
Active int
53
56
// Accepted total number of accepted client connections
@@ -65,6 +68,49 @@ type (
65
68
}
66
69
)
67
70
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
+
68
114
// NGINXStatusCollector defines a status collector interface
69
115
type NGINXStatusCollector interface {
70
116
prometheus.Collector
@@ -131,54 +177,14 @@ func (p nginxStatusCollector) Stop() {
131
177
close (p .scrapeChan )
132
178
}
133
179
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
-
165
180
// nginxStatusCollector scrape the nginx status
166
181
func (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 ()
169
183
if 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 )
172
185
return
173
186
}
174
187
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
-
182
188
ch <- prometheus .MustNewConstMetric (p .data .connectionsTotal ,
183
189
prometheus .CounterValue , float64 (s .Accepted ), "accepted" )
184
190
ch <- prometheus .MustNewConstMetric (p .data .connectionsTotal ,
0 commit comments