Fix golint issues
This commit is contained in:
parent
109530b35b
commit
f9df1401cd
3 changed files with 27 additions and 27 deletions
|
@ -58,7 +58,7 @@ var getCmd = &cobra.Command{
|
||||||
table.MaxColWidth = 80
|
table.MaxColWidth = 80
|
||||||
table.Wrap = true
|
table.Wrap = true
|
||||||
|
|
||||||
pools := pm.Pools()
|
pools := pm.Pools
|
||||||
|
|
||||||
for _, pool := range pools {
|
for _, pool := range pools {
|
||||||
table.AddRow("Address:", pool.Address)
|
table.AddRow("Address:", pool.Address)
|
||||||
|
|
|
@ -2,7 +2,6 @@ package phpfpm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"net/http"
|
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -10,10 +9,10 @@ const (
|
||||||
namespace = "phpfpm"
|
namespace = "phpfpm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Exporter configures and exposes PHP-FPM metrics to Prometheus.
|
||||||
type Exporter struct {
|
type Exporter struct {
|
||||||
PoolManager PoolManager
|
PoolManager PoolManager
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
client *http.Client
|
|
||||||
|
|
||||||
startSince *prometheus.Desc
|
startSince *prometheus.Desc
|
||||||
acceptedConnections *prometheus.Desc
|
acceptedConnections *prometheus.Desc
|
||||||
|
@ -28,6 +27,7 @@ type Exporter struct {
|
||||||
slowRequests *prometheus.Desc
|
slowRequests *prometheus.Desc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewExporter creates a new Exporter for a PoolManager and configures the necessary metrics.
|
||||||
func NewExporter(pm PoolManager) *Exporter {
|
func NewExporter(pm PoolManager) *Exporter {
|
||||||
return &Exporter{
|
return &Exporter{
|
||||||
PoolManager: pm,
|
PoolManager: pm,
|
||||||
|
@ -100,13 +100,14 @@ func NewExporter(pm PoolManager) *Exporter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Collect updates the Pools and sends the collected metrics to Prometheus
|
||||||
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
||||||
e.mutex.Lock()
|
e.mutex.Lock()
|
||||||
defer e.mutex.Unlock()
|
defer e.mutex.Unlock()
|
||||||
|
|
||||||
e.PoolManager.Update()
|
e.PoolManager.Update()
|
||||||
|
|
||||||
for _, pool := range e.PoolManager.Pools() {
|
for _, pool := range e.PoolManager.Pools {
|
||||||
ch <- prometheus.MustNewConstMetric(e.startSince, prometheus.CounterValue, float64(pool.AcceptedConnections), pool.Name)
|
ch <- prometheus.MustNewConstMetric(e.startSince, prometheus.CounterValue, float64(pool.AcceptedConnections), pool.Name)
|
||||||
ch <- prometheus.MustNewConstMetric(e.acceptedConnections, prometheus.CounterValue, float64(pool.StartSince), pool.Name)
|
ch <- prometheus.MustNewConstMetric(e.acceptedConnections, prometheus.CounterValue, float64(pool.StartSince), pool.Name)
|
||||||
ch <- prometheus.MustNewConstMetric(e.listenQueue, prometheus.GaugeValue, float64(pool.ListenQueue), pool.Name)
|
ch <- prometheus.MustNewConstMetric(e.listenQueue, prometheus.GaugeValue, float64(pool.ListenQueue), pool.Name)
|
||||||
|
@ -128,6 +129,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Describe exposes the metric description to Prometheus
|
||||||
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
|
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
|
||||||
ch <- e.startSince
|
ch <- e.startSince
|
||||||
ch <- e.acceptedConnections
|
ch <- e.acceptedConnections
|
||||||
|
|
|
@ -31,17 +31,19 @@ type logger interface {
|
||||||
Error(ar ...interface{})
|
Error(ar ...interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PoolManager manages all configured Pools
|
||||||
type PoolManager struct {
|
type PoolManager struct {
|
||||||
pools []Pool `json:"pools"`
|
Pools []Pool `json:"pools"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pool describes a single PHP-FPM pool that can be reached via a Socket or TCP address
|
||||||
type Pool struct {
|
type Pool struct {
|
||||||
// The address of the pool, e.g. tcp://127.0.0.1:9000 or unix:///tmp/php-fpm.sock
|
// The address of the pool, e.g. tcp://127.0.0.1:9000 or unix:///tmp/php-fpm.sock
|
||||||
Address string
|
Address string `json:"-"`
|
||||||
CollectionError error
|
CollectionError error `json:"-"`
|
||||||
Name string `json:"pool"`
|
Name string `json:"pool"`
|
||||||
ProcessManager string `json:"process manager"`
|
ProcessManager string `json:"process manager"`
|
||||||
StartTime Timestamp `json:"start time"`
|
StartTime timestamp `json:"start time"`
|
||||||
StartSince int `json:"start since"`
|
StartSince int `json:"start since"`
|
||||||
AcceptedConnections int `json:"accepted conn"`
|
AcceptedConnections int `json:"accepted conn"`
|
||||||
ListenQueue int `json:"listen queue"`
|
ListenQueue int `json:"listen queue"`
|
||||||
|
@ -56,6 +58,7 @@ type Pool struct {
|
||||||
Processes []PoolProcess `json:"processes"`
|
Processes []PoolProcess `json:"processes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PoolProcess describes a single PHP-FPM process. A pool can have multiple processes.
|
||||||
type PoolProcess struct {
|
type PoolProcess struct {
|
||||||
PID int `json:"pid"`
|
PID int `json:"pid"`
|
||||||
State string `json:"state"`
|
State string `json:"state"`
|
||||||
|
@ -72,45 +75,37 @@ type PoolProcess struct {
|
||||||
LastRequestMemory int `json:"last request memory"`
|
LastRequestMemory int `json:"last request memory"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add will add a pool to the pool manager based on the given URI.
|
||||||
func (pm *PoolManager) Add(uri string) Pool {
|
func (pm *PoolManager) Add(uri string) Pool {
|
||||||
p := Pool{Address: uri}
|
p := Pool{Address: uri}
|
||||||
pm.pools = append(pm.pools, p)
|
pm.Pools = append(pm.Pools, p)
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update will run the pool.Update() method concurrently on all Pools.
|
||||||
func (pm *PoolManager) Update() (err error) {
|
func (pm *PoolManager) Update() (err error) {
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
|
|
||||||
started := time.Now()
|
started := time.Now()
|
||||||
|
|
||||||
for idx := range pm.pools {
|
for idx := range pm.Pools {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(p *Pool) {
|
go func(p *Pool) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
p.Update()
|
p.Update()
|
||||||
}(&pm.pools[idx])
|
}(&pm.Pools[idx])
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
ended := time.Now()
|
ended := time.Now()
|
||||||
|
|
||||||
log.Debugf("Updated %v pool(s) in %v", len(pm.pools), ended.Sub(started))
|
log.Debugf("Updated %v pool(s) in %v", len(pm.Pools), ended.Sub(started))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pm *PoolManager) Pools() []Pool {
|
// Update will connect to PHP-FPM and retrieve the latest data for the pool.
|
||||||
return pm.pools
|
|
||||||
}
|
|
||||||
|
|
||||||
// Implement custom Marshaler due to "pools" being unexported
|
|
||||||
func (pm PoolManager) MarshalJSON() ([]byte, error) {
|
|
||||||
return json.Marshal(struct {
|
|
||||||
Pools []Pool `json:"pools"`
|
|
||||||
}{Pools: pm.pools})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Pool) Update() (err error) {
|
func (p *Pool) Update() (err error) {
|
||||||
p.CollectionError = nil
|
p.CollectionError = nil
|
||||||
|
|
||||||
|
@ -158,23 +153,26 @@ func (p *Pool) error(err error) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
type Timestamp time.Time
|
type timestamp time.Time
|
||||||
|
|
||||||
func (t *Timestamp) MarshalJSON() ([]byte, error) {
|
// MarshalJSON customise JSON for timestamp
|
||||||
|
func (t *timestamp) MarshalJSON() ([]byte, error) {
|
||||||
ts := time.Time(*t).Unix()
|
ts := time.Time(*t).Unix()
|
||||||
stamp := fmt.Sprint(ts)
|
stamp := fmt.Sprint(ts)
|
||||||
return []byte(stamp), nil
|
return []byte(stamp), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Timestamp) UnmarshalJSON(b []byte) error {
|
// UnmarshalJSON customise JSON for timestamp
|
||||||
|
func (t *timestamp) UnmarshalJSON(b []byte) error {
|
||||||
ts, err := strconv.Atoi(string(b))
|
ts, err := strconv.Atoi(string(b))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*t = Timestamp(time.Unix(int64(ts), 0))
|
*t = timestamp(time.Unix(int64(ts), 0))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetLogger configures the used logger
|
||||||
func SetLogger(logger logger) {
|
func SetLogger(logger logger) {
|
||||||
log = logger
|
log = logger
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue