Skip to content

Commit 404e72d

Browse files
committed
refactor: Extract out stub_status scraper for external use
1 parent 55ae9c0 commit 404e72d

File tree

1 file changed

+50
-44
lines changed

1 file changed

+50
-44
lines changed

internal/ingress/metric/collectors/nginx_status.go

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package collectors
1818

1919
import (
20-
"log"
20+
"fmt"
2121
"regexp"
2222
"strconv"
2323

@@ -35,7 +35,10 @@ var (
3535
)
3636

3737
type (
38+
NginxStatusScraper struct{}
39+
3840
nginxStatusCollector struct {
41+
scraper NginxStatusScraper
3942
scrapeChan chan scrapeRequest
4043

4144
data *nginxStatusData
@@ -47,7 +50,7 @@ type (
4750
connections *prometheus.Desc
4851
}
4952

50-
basicStatus struct {
53+
NginxStubStatus struct {
5154
// Active total number of active connections
5255
Active 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
69115
type NGINXStatusCollector interface {
70116
prometheus.Collector
@@ -131,54 +177,14 @@ func (p nginxStatusCollector) Stop() {
131177
close(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
166181
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()
169183
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)
172185
return
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-
182188
ch <- prometheus.MustNewConstMetric(p.data.connectionsTotal,
183189
prometheus.CounterValue, float64(s.Accepted), "accepted")
184190
ch <- prometheus.MustNewConstMetric(p.data.connectionsTotal,

0 commit comments

Comments
 (0)