Skip to content

Commit 912566e

Browse files
author
miktwon
committed
Refactored unneded code
1 parent 3502ef6 commit 912566e

File tree

2 files changed

+38
-70
lines changed

2 files changed

+38
-70
lines changed

fpmPrometeus/fpmPrometeus.go

Lines changed: 37 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,23 @@ func Register(fpmConfigPath string, update time.Duration) error {
4545
if err != nil {
4646
return err
4747
}
48-
fmpStatus := NewFPMPoolStatus(cfg.Pools)
49-
prometheus.MustRegister(fmpStatus)
50-
51-
go func() {
52-
for {
53-
go fmpStatus.stat.UpdateStatuses()
54-
time.Sleep(update)
55-
}
56-
}()
48+
fpmStatus := NewFPMPoolStatus(cfg.Pools)
49+
prometheus.MustRegister(fpmStatus)
5750

51+
go startUpdateStatuses(fpmStatus, update)
5852
return nil
5953
}
6054

55+
func startUpdateStatuses(fpmStatus *FPMPoolStatus, update time.Duration) {
56+
var t time.Time
57+
for {
58+
t = time.Now()
59+
fpmStatus.stat.UpdateStatuses()
60+
sleep := update - time.Now().Sub(t)
61+
time.Sleep(sleep)
62+
}
63+
}
64+
6165
func (s *stat) GetStatuses() []Status {
6266
s.mu.Lock()
6367
statuses := make([]Status, len(s.statuses))
@@ -100,27 +104,13 @@ type FPMPoolStatus struct {
100104
idleProcesses *prometheus.GaugeVec
101105
activeProcesses *prometheus.GaugeVec
102106
totalProcesses *prometheus.GaugeVec
107+
acceptedConn *prometheus.GaugeVec
103108

104-
startSince *prometheus.CounterVec
105-
acceptedConn *prometheus.CounterVec
106-
maxListenQueue *prometheus.CounterVec
107-
maxActiveProcesses *prometheus.CounterVec
108-
maxChildrenReached *prometheus.CounterVec
109-
slowRequests *prometheus.CounterVec
110-
}
111-
112-
func (e *FPMPoolStatus) resetMetrics() {
113-
e.listenQueue.Reset()
114-
e.listenQueueLen.Reset()
115-
e.idleProcesses.Reset()
116-
e.activeProcesses.Reset()
117-
e.totalProcesses.Reset()
118-
e.startSince.Reset()
119-
e.acceptedConn.Reset()
120-
e.maxListenQueue.Reset()
121-
e.maxActiveProcesses.Reset()
122-
e.maxChildrenReached.Reset()
123-
e.slowRequests.Reset()
109+
startSince *prometheus.GaugeVec
110+
maxListenQueue *prometheus.GaugeVec
111+
maxActiveProcesses *prometheus.GaugeVec
112+
maxChildrenReached *prometheus.GaugeVec
113+
slowRequests *prometheus.GaugeVec
124114
}
125115

126116
func NewFPMPoolStatus(pools []fpmConfig.Pool) *FPMPoolStatus {
@@ -132,16 +122,16 @@ func NewFPMPoolStatus(pools []fpmConfig.Pool) *FPMPoolStatus {
132122
pools: pools,
133123
statuses: make([]Status, len(pools)),
134124
},
135-
startSince: prometheus.NewCounterVec(
136-
prometheus.CounterOpts{
125+
startSince: prometheus.NewGaugeVec(
126+
prometheus.GaugeOpts{
137127
Namespace: namespace,
138128
Name: "start_since",
139129
Help: "Number of seconds since FPM has started",
140130
},
141131
poolLabelNames,
142132
),
143-
acceptedConn: prometheus.NewCounterVec(
144-
prometheus.CounterOpts{
133+
acceptedConn: prometheus.NewGaugeVec(
134+
prometheus.GaugeOpts{
145135
Namespace: namespace,
146136
Name: "accepted_conn",
147137
Help: "The number of requests accepted by the pool",
@@ -156,8 +146,8 @@ func NewFPMPoolStatus(pools []fpmConfig.Pool) *FPMPoolStatus {
156146
},
157147
poolLabelNames,
158148
),
159-
maxListenQueue: prometheus.NewCounterVec(
160-
prometheus.CounterOpts{
149+
maxListenQueue: prometheus.NewGaugeVec(
150+
prometheus.GaugeOpts{
161151
Namespace: namespace,
162152
Name: "max_listen_queue",
163153
Help: "The maximum number of requests in the queue of pending connections since FPM has started",
@@ -196,24 +186,24 @@ func NewFPMPoolStatus(pools []fpmConfig.Pool) *FPMPoolStatus {
196186
},
197187
poolLabelNames,
198188
),
199-
maxActiveProcesses: prometheus.NewCounterVec(
200-
prometheus.CounterOpts{
189+
maxActiveProcesses: prometheus.NewGaugeVec(
190+
prometheus.GaugeOpts{
201191
Namespace: namespace,
202192
Name: "max_active_processes",
203193
Help: "The maximum number of active processes since FPM has started",
204194
},
205195
poolLabelNames,
206196
),
207-
maxChildrenReached: prometheus.NewCounterVec(
208-
prometheus.CounterOpts{
197+
maxChildrenReached: prometheus.NewGaugeVec(
198+
prometheus.GaugeOpts{
209199
Namespace: namespace,
210200
Name: "max_children_reached",
211201
Help: "The number of times, the process limit has been reached, when pm tries to start more children (works only for pm 'dynamic' and 'ondemand')",
212202
},
213203
poolLabelNames,
214204
),
215-
slowRequests: prometheus.NewCounterVec(
216-
prometheus.CounterOpts{
205+
slowRequests: prometheus.NewGaugeVec(
206+
prometheus.GaugeOpts{
217207
Namespace: namespace,
218208
Name: "slow_requests",
219209
Help: "The number of requests that exceeded your request_slowlog_timeout value",
@@ -223,48 +213,26 @@ func NewFPMPoolStatus(pools []fpmConfig.Pool) *FPMPoolStatus {
223213
}
224214
}
225215

226-
func (e *FPMPoolStatus) reset() {
227-
e.listenQueue.Reset()
228-
e.listenQueueLen.Reset()
229-
e.idleProcesses.Reset()
230-
e.activeProcesses.Reset()
231-
e.totalProcesses.Reset()
232-
e.startSince.Reset()
233-
e.acceptedConn.Reset()
234-
e.maxListenQueue.Reset()
235-
e.maxActiveProcesses.Reset()
236-
e.maxChildrenReached.Reset()
237-
e.slowRequests.Reset()
238-
}
239-
240216
func setAndCollect(gaugeVec *prometheus.GaugeVec, poolName string, val int, ch chan<- prometheus.Metric) {
241217
gauge := gaugeVec.WithLabelValues(poolName)
242218
gauge.Set(float64(val))
243219
gauge.Collect(ch)
244220
}
245221

246-
func addAndCollect(counterVec *prometheus.CounterVec, poolName string, val int, ch chan<- prometheus.Metric) {
247-
counter := counterVec.WithLabelValues(poolName)
248-
counter.Add(float64(val))
249-
counter.Collect(ch)
250-
}
251-
252222
func (e *FPMPoolStatus) Collect(ch chan<- prometheus.Metric) {
253-
e.resetMetrics()
254223
statuses := e.stat.GetStatuses()
255224
for _, p := range statuses {
256225
setAndCollect(e.listenQueue, p.Name, p.ListenQueue, ch)
257226
setAndCollect(e.listenQueueLen, p.Name, p.ListenQueueLen, ch)
258227
setAndCollect(e.idleProcesses, p.Name, p.IdleProcesses, ch)
259228
setAndCollect(e.activeProcesses, p.Name, p.ActiveProcesses, ch)
260229
setAndCollect(e.totalProcesses, p.Name, p.TotalProcesses, ch)
261-
262-
addAndCollect(e.startSince, p.Name, p.StartSince, ch)
263-
addAndCollect(e.acceptedConn, p.Name, p.AcceptedConn, ch)
264-
addAndCollect(e.maxListenQueue, p.Name, p.MaxListenQueue, ch)
265-
addAndCollect(e.maxActiveProcesses, p.Name, p.MaxActiveProcesses, ch)
266-
addAndCollect(e.maxChildrenReached, p.Name, p.MaxChildrenReached, ch)
267-
addAndCollect(e.slowRequests, p.Name, p.SlowRequests, ch)
230+
setAndCollect(e.acceptedConn, p.Name, p.AcceptedConn, ch)
231+
setAndCollect(e.startSince, p.Name, p.StartSince, ch)
232+
setAndCollect(e.maxListenQueue, p.Name, p.MaxListenQueue, ch)
233+
setAndCollect(e.maxActiveProcesses, p.Name, p.MaxActiveProcesses, ch)
234+
setAndCollect(e.maxChildrenReached, p.Name, p.MaxChildrenReached, ch)
235+
setAndCollect(e.slowRequests, p.Name, p.SlowRequests, ch)
268236
}
269237
}
270238

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func init() {
2222

2323
pflag.BoolP("prometheus", "p", false, "enable prometheus statistic")
2424
pflag.String("prometheus-addr", ":8080", "prometheus statistic addr")
25-
pflag.String("prometheus-path", "/prometheus", "prometheus statistic path")
25+
pflag.String("prometheus-path", "/metrics", "prometheus statistic path")
2626
pflag.Duration("fpm-statuses-update-interval", 10*time.Second, "fpm statuses update interval")
2727
pflag.String("fpm-config", "/etc/php/php-fpm.conf", "path to php-fpm config file")
2828
pflag.Parse()

0 commit comments

Comments
 (0)