Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ Usage:
php-fpm-exporter [flags]

Flags:
--addr string listen address for metrics handler (default "127.0.0.1:8080")
--endpoint string url for php-fpm status (default "http://127.0.0.1:9000/status")
--fastcgi string fastcgi url. If this is set, fastcgi will be used instead of HTTP
--addr string listen address for metrics handler (default "127.0.0.1:8080")
--endpoint string url for php-fpm status (default "http://127.0.0.1:9000/status")
--fastcgi string fastcgi url. If this is set, fastcgi will be used instead of HTTP
--status.timeout duration Scrape timeout for php-fpm status. If unset, then will wait forever.
```

When running, a simple healthcheck is available on `/healthz`
Expand Down
2 changes: 2 additions & 0 deletions cmd/php-fpm-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func main() {
endpoint = kingpin.Flag("endpoint", "url for php-fpm status").Default("http://127.0.0.1:9000/status").Envar("ENDPOINT_URL").String()
fcgiEndpoint = kingpin.Flag("fastcgi", "fastcgi url. If this is set, fastcgi will be used instead of HTTP").Envar("FASTCGI_URL").String()
metricsEndpoint = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics. Cannot be /").Default("/metrics").Envar("TELEMETRY_PATH").String()
statusTimeout = kingpin.Flag("status.timeout", "Scrape timeout for php-fpm status. If unset, then will wait forever.").Envar("STATUS_TIMEOUT").Duration()
)

kingpin.HelpFlag.Short('h')
Expand All @@ -29,6 +30,7 @@ func main() {
exporter.SetFastcgi(*fcgiEndpoint),
exporter.SetLogger(logger),
exporter.SetMetricsEndpoint(*metricsEndpoint),
exporter.SetStatusTimeout(statusTimeout),
)

if err != nil {
Expand Down
19 changes: 12 additions & 7 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package exporter
import (
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strconv"

Expand Down Expand Up @@ -70,7 +69,8 @@ func (c *collector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.slowRequests
}

func getDataFastcgi(u *url.URL) ([]byte, error) {
func (c *collector) getDataFastcgi() ([]byte, error) {
u := c.exporter.fcgiEndpoint
path := u.Path
host := u.Host

Expand All @@ -86,13 +86,17 @@ func getDataFastcgi(u *url.URL) ([]byte, error) {
"SCRIPT_NAME": path,
}

fcgi, err := fcgiclient.Dial(u.Scheme, host)
fcgi, err := fcgiclient.DialTimeout(u.Scheme, host, c.exporter.statusTimeout)
if err != nil {
return nil, errors.Wrap(err, "fastcgi dial failed")
}

defer fcgi.Close()

if err = fcgi.SetTimeout(c.exporter.statusTimeout); err != nil {
return nil, errors.Wrap(err, "fastcgi SetTimeout failed")
}

resp, err := fcgi.Get(env)
if err != nil {
return nil, errors.Wrap(err, "fastcgi get failed")
Expand All @@ -112,7 +116,8 @@ func getDataFastcgi(u *url.URL) ([]byte, error) {
return body, nil
}

func getDataHTTP(u *url.URL) ([]byte, error) {
func (c *collector) getDataHTTP() ([]byte, error) {
u := c.exporter.endpoint
req := http.Request{
Method: "GET",
URL: u,
Expand All @@ -123,7 +128,7 @@ func getDataHTTP(u *url.URL) ([]byte, error) {
Host: u.Host,
}

resp, err := http.DefaultClient.Do(&req)
resp, err := (&http.Client{Timeout: c.exporter.statusTimeout}).Do(&req)
if err != nil {
return nil, errors.Wrap(err, "HTTP request failed")
}
Expand All @@ -150,9 +155,9 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
)

if c.exporter.fcgiEndpoint != nil && c.exporter.fcgiEndpoint.String() != "" {
body, err = getDataFastcgi(c.exporter.fcgiEndpoint)
body, err = c.getDataFastcgi()
} else {
body, err = getDataHTTP(c.exporter.endpoint)
body, err = c.getDataHTTP()
}

if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Exporter struct {
fcgiEndpoint *url.URL
logger *zap.Logger
metricsEndpoint string
statusTimeout time.Duration
}

// OptionsFunc is a function passed to new for setting options on a new Exporter.
Expand Down Expand Up @@ -124,6 +125,19 @@ func SetMetricsEndpoint(path string) func(*Exporter) error {
}
}

// SetStatusTimeout sets the timeout for a request to the fpm status endpoint.
// Generally only used when create a new Exporter.
func SetStatusTimeout(timeout *time.Duration) func(*Exporter) error {
return func(e *Exporter) error {
if timeout == nil {
e.statusTimeout = 0
} else {
e.statusTimeout = *timeout
}
return nil
}
}

var healthzOK = []byte("ok\n")

func (e *Exporter) healthz(w http.ResponseWriter, r *http.Request) {
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ require (
golang.org/x/sync v0.0.0-20170418210838-de49d9dcd27d
gopkg.in/alecthomas/kingpin.v2 v2.2.6
)

replace (
github.com/tomasen/fcgi_client => github.com/kanocz/fcgi_client v0.0.0-20210113082628-fff85c8adfb7
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v0.0.0-20170331031902-2bba0603135d h1:KmiEmEGA5sqizMpKnexwioxj8zEUSBc7p9UTQu36lpQ=
github.com/golang/protobuf v0.0.0-20170331031902-2bba0603135d/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/kanocz/fcgi_client v0.0.0-20210113082628-fff85c8adfb7 h1:W0fAsQ7bC1db4k9O2X6yZvatz/0c/ISyxhmNnc6arZA=
github.com/kanocz/fcgi_client v0.0.0-20210113082628-fff85c8adfb7/go.mod h1:dHpIS7C6YjFguh5vo9QBVEojDoL3vh3v6oEho2HtNyA=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
Expand Down
Loading