Emulate PHP-FPMs metrics calculations for --phpfpm.fix-process-count

This commit is contained in:
Enrico Stahn 2018-02-28 12:12:10 +11:00
parent afbeb201b1
commit 6de4433881
No known key found for this signature in database
GPG key ID: 5263621C269A50DE
2 changed files with 12 additions and 33 deletions

View file

@ -178,21 +178,15 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
continue continue
} }
pps := CountProcessState(pool) active, idle, total := CountProcessState(pool.Processes)
if !e.CalculateProcessScoreboard && (pps.Active() != pool.ActiveProcesses || pps.Idle != pool.IdleProcesses) { 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.") 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 { if e.CalculateProcessScoreboard {
active = pool.ActiveProcesses active = pool.ActiveProcesses
idle = pool.IdleProcesses idle = pool.IdleProcesses
total = pool.TotalProcesses total = pool.TotalProcesses
} else {
active = pps.Active()
idle = pps.Idle
total = pps.Total()
} }
ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, 1, pool.Name) ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, 1, pool.Name)

View file

@ -97,7 +97,7 @@ type PoolProcess struct {
User string `json:"user"` User string `json:"user"`
Script string `json:"script"` Script string `json:"script"`
LastRequestCPU float64 `json:"last request cpu"` 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. // PoolProcessStateCounter holds the calculated metrics for pool processes.
@ -192,40 +192,25 @@ func (p *Pool) error(err error) error {
return err return err
} }
// Active calculates the number of active processes. // CountProcessState return the calculated metrics based on the reported processes.
func (pps *PoolProcessStateCounter) Active() int64 { func CountProcessState(processes []PoolProcess) (active int64, idle int64, total int64) {
return pps.Running + pps.ReadingHeaders for idx := range processes {
} switch processes[idx].State {
// 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 {
case PoolProcessRequestRunning: case PoolProcessRequestRunning:
pps.Running++ active++
case PoolProcessRequestIdle: case PoolProcessRequestIdle:
pps.Idle++ idle++
case PoolProcessRequestEnding: case PoolProcessRequestEnding:
pps.Ending++
case PoolProcessRequestFinishing: case PoolProcessRequestFinishing:
pps.Finishing++
case PoolProcessRequestInfo: case PoolProcessRequestInfo:
pps.Info++
case PoolProcessRequestReadingHeaders: case PoolProcessRequestReadingHeaders:
pps.ReadingHeaders++ active++
default: 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 type timestamp time.Time