From 6de44338819b5278f0559b651280ee0c9eddc1dc Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Wed, 28 Feb 2018 12:12:10 +1100 Subject: [PATCH] Emulate PHP-FPMs metrics calculations for --phpfpm.fix-process-count --- phpfpm/exporter.go | 10 ++-------- phpfpm/phpfpm.go | 35 ++++++++++------------------------- 2 files changed, 12 insertions(+), 33 deletions(-) diff --git a/phpfpm/exporter.go b/phpfpm/exporter.go index c4476ee..a817791 100644 --- a/phpfpm/exporter.go +++ b/phpfpm/exporter.go @@ -178,21 +178,15 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) { continue } - pps := CountProcessState(pool) - if !e.CalculateProcessScoreboard && (pps.Active() != pool.ActiveProcesses || pps.Idle != pool.IdleProcesses) { + active, idle, total := CountProcessState(pool.Processes) + if !e.CalculateProcessScoreboard && (active != pool.ActiveProcesses || idle != pool.IdleProcesses) { log.Error("Inconsistent active and idle processes reported. Set `--fix-process-count` to have this calculated by php-fpm_exporter instead.") } - var active, idle, total int64 - if e.CalculateProcessScoreboard { active = pool.ActiveProcesses idle = pool.IdleProcesses total = pool.TotalProcesses - } else { - active = pps.Active() - idle = pps.Idle - total = pps.Total() } ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, 1, pool.Name) diff --git a/phpfpm/phpfpm.go b/phpfpm/phpfpm.go index 09a099f..43e98f0 100644 --- a/phpfpm/phpfpm.go +++ b/phpfpm/phpfpm.go @@ -97,7 +97,7 @@ type PoolProcess struct { User string `json:"user"` Script string `json:"script"` LastRequestCPU float64 `json:"last request cpu"` - LastRequestMemory int `json:"last request memory"` + LastRequestMemory int64 `json:"last request memory"` } // PoolProcessStateCounter holds the calculated metrics for pool processes. @@ -192,40 +192,25 @@ func (p *Pool) error(err error) error { return err } -// Active calculates the number of active processes. -func (pps *PoolProcessStateCounter) Active() int64 { - return pps.Running + pps.ReadingHeaders -} - -// Total calculates the total number of process (Idle + Active). -func (pps *PoolProcessStateCounter) Total() int64 { - return pps.Idle + pps.Active() -} - -// CountProcessState creates a scoreboard with the calculated process metrics. -func CountProcessState(p Pool) PoolProcessStateCounter { - pps := PoolProcessStateCounter{} - - for idx := range p.Processes { - switch p.Processes[idx].State { +// CountProcessState return the calculated metrics based on the reported processes. +func CountProcessState(processes []PoolProcess) (active int64, idle int64, total int64) { + for idx := range processes { + switch processes[idx].State { case PoolProcessRequestRunning: - pps.Running++ + active++ case PoolProcessRequestIdle: - pps.Idle++ + idle++ case PoolProcessRequestEnding: - pps.Ending++ case PoolProcessRequestFinishing: - pps.Finishing++ case PoolProcessRequestInfo: - pps.Info++ case PoolProcessRequestReadingHeaders: - pps.ReadingHeaders++ + active++ default: - log.Errorf("Unknown process state '%v'", p.Processes[idx].State) + log.Errorf("Unknown process state '%v'", processes[idx].State) } } - return pps + return active, idle, active + idle } type timestamp time.Time