Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support set default value when scrape failed #3

Merged
merged 2 commits into from
Dec 13, 2021
Merged
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
81 changes: 46 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,34 @@ A webserver such as NGINX or Apache is **NOT** needed!

<!-- toc -->

- [Features](#features)
- [Usage](#usage)
* [Options and defaults](#options-and-defaults)
* [Why `--phpfpm.fix-process-count`?](#why---phpfpmfix-process-count)
* [CLI Examples](#cli-examples)
* [Docker Examples](#docker-examples)
* [Kubernetes Example](#kubernetes-example)
- [Metrics collected](#metrics-collected)
- [Grafana Dasbhoard for Kubernetes](#grafana-dasbhoard-for-kubernetes)
- [FAQ](#faq)
- [Development](#development)
* [E2E Tests](#e2e-tests)
- [Contributing](#contributing)
- [Contributors](#contributors)
- [Alternatives](#alternatives)
- [php-fpm_exporter](#php-fpm_exporter)
- [Table of Contents](#table-of-contents)
- [Features](#features)
- [Usage](#usage)
- [Options and defaults](#options-and-defaults)
- [Why `--phpfpm.fix-process-count`?](#why---phpfpmfix-process-count)
- [CLI Examples](#cli-examples)
- [Docker Examples](#docker-examples)
- [Kubernetes Example](#kubernetes-example)
- [Metrics collected](#metrics-collected)
- [Grafana Dasbhoard for Kubernetes](#grafana-dasbhoard-for-kubernetes)
- [FAQ](#faq)
- [Development](#development)
- [E2E Tests](#e2e-tests)
- [Contributing](#contributing)
- [Contributors](#contributors)
- [Alternatives](#alternatives)

<!-- tocstop -->

## Features

* Export single or multiple pools
* Export to CLI as text or JSON
* Connects directly to PHP-FPM via TCP or Socket
* Maps environment variables to CLI options
* Fix for PHP-FPM metrics oddities
* [Grafana Dashboard](https://grafana.com/dashboards/4912) for Kubernetes
- Export single or multiple pools
- Export to CLI as text or JSON
- Connects directly to PHP-FPM via TCP or Socket
- Maps environment variables to CLI options
- Fix for PHP-FPM metrics oddities
- [Grafana Dashboard](https://grafana.com/dashboards/4912) for Kubernetes

## Usage

Expand All @@ -65,6 +67,7 @@ The `server` command runs the server required for prometheus to retrieve the sta
| `--web.disable-process-state-metrics` | Exclude metrics about phpfpm process state. `--phpfpm.fix-process-count` will be set to `false` if the flag is set to `ture`. | `PHP_FPM_WEB_DISABLE_PROCESS_STATE_METRICS` | `false` |
| `--phpfpm.scrape-uri` | FastCGI address, e.g. unix:///tmp/php.sock;/status or tcp://127.0.0.1:9000/status | `PHP_FPM_SCRAPE_URI` | `tcp://127.0.0.1:9000/status` |
| `--phpfpm.fix-process-count` | Enable to calculate process numbers via php-fpm_exporter since PHP-FPM sporadically reports wrong active/idle/total process numbers. | `PHP_FPM_FIX_PROCESS_COUNT`| `false` |
| `--phpfpm.phpfpm.default-active-processes` | Default phpfpm_active_processes value when scrape failed. | `PHP_FPM_DEFAULT_ACTIVE_PROCESSES`| `0` |
| `--log.level` | Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal] (default "error") | `PHP_FPM_LOG_LEVEL` | info |

### Why `--phpfpm.fix-process-count`?
Expand All @@ -81,49 +84,56 @@ Which shouldn't matter and `active processes` should still be equal or lower to

If you like to have a more granular reporting please use `phpfpm_process_state`.

* https://bugs.php.net/bug.php?id=76003
* https://stackoverflow.com/questions/48961556/can-active-processes-be-larger-than-max-children-for-php-fpm
- https://bugs.php.net/bug.php?id=76003
- https://stackoverflow.com/questions/48961556/can-active-processes-be-larger-than-max-children-for-php-fpm

### CLI Examples

* Retrieve information from PHP-FPM running on `127.0.0.1:9000` with status endpoint being `/status`
- Retrieve information from PHP-FPM running on `127.0.0.1:9000` with status endpoint being `/status`

```
php-fpm_exporter get
```

* Retrieve information from PHP-FPM running on `127.0.0.1:9000` and `127.0.0.1:9001`
- Retrieve information from PHP-FPM running on `127.0.0.1:9000` and `127.0.0.1:9001`

```
php-fpm_exporter get --phpfpm.scrape-uri tcp://127.0.0.1:9000/status,tcp://127.0.0.1:9001/status
```

* Run as server with 2 pools:
- Run as server with 2 pools:

```
php-fpm_exporter server --phpfpm.scrape-uri tcp://127.0.0.1:9000/status,tcp://127.0.0.1:9001/status
```

* Run as server and enable process count fix via environment variable:
- Run as server and enable process count fix via environment variable:

```
PHP_FPM_FIX_PROCESS_COUNT=1 go run main.go server --web.listen-address ":12345" --log.level=debug
```

### Docker Examples

* Run docker manually
- Run docker manually

```
docker pull hipages/php-fpm_exporter
docker run -it --rm -e PHP_FPM_SCRAPE_URI="tcp://127.0.0.1:9000/status,tcp://127.0.0.1:9001/status" hipages/php-fpm_exporter
```

* Run the docker-compose example
- Run the docker-compose example

```
git clone git@github.com:hipages/php-fpm_exporter.git
cd php-fpm_exporter/test
docker-compose -p php-fpm_exporter up
```

You can now access the following links:

* Prometheus: http://127.0.0.1:9090/
* php-fpm_exporter metrics: http://127.0.0.1:9253/metrics
- Prometheus: http://127.0.0.1:9090/
- php-fpm_exporter metrics: http://127.0.0.1:9253/metrics

[![asciicast](https://asciinema.org/a/1msR8nqAsFdHzROosUb7PiHvf.png)](https://asciinema.org/a/1msR8nqAsFdHzROosUb7PiHvf)

Expand Down Expand Up @@ -180,9 +190,10 @@ The Grafana dashboard can be found [here](https://grafana.com/dashboards/4912).

## FAQ

* **How to update "Metrics collected"?**
- **How to update "Metrics collected"?**

Copy&paste the output from:

```
curl http://127.0.0.1:12345/metrics | grep phpfpm | grep "#"
```
Expand Down Expand Up @@ -238,6 +249,6 @@ This project follows the [all-contributors](https://github.com/all-contributors/

## Alternatives

* [bakins/php-fpm-exporter](https://github.com/bakins/php-fpm-exporter)
* [peakgames/php-fpm-prometheus](https://github.com/peakgames/php-fpm-prometheus)
* [craigmj/phpfpm_exporter](https://github.com/craigmj/phpfpm_exporter)
- [bakins/php-fpm-exporter](https://github.com/bakins/php-fpm-exporter)
- [peakgames/php-fpm-prometheus](https://github.com/peakgames/php-fpm-prometheus)
- [craigmj/phpfpm_exporter](https://github.com/craigmj/phpfpm_exporter)
4 changes: 4 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
fixProcessCount bool
disableExporterMetrics bool
disableProcessStateMetrics bool
defaultActiveProcesses float64
)

// serverCmd represents the server command
Expand All @@ -59,6 +60,7 @@ to quickly create a Cobra application.`,
}

exporter := phpfpm.NewExporter(pm)
exporter.DefaultActiveProcesses = defaultActiveProcesses

if disableProcessStateMetrics {
fixProcessCount = false
Expand Down Expand Up @@ -150,6 +152,7 @@ func init() {
serverCmd.Flags().BoolVar(&disableProcessStateMetrics, "web.disable-process-state-metrics", false, "Exclude metrics about phpfpm process state. --phpfpm.fix-process-count will be set to false if the flag is set to ture.")
serverCmd.Flags().StringSliceVar(&scrapeURIs, "phpfpm.scrape-uri", []string{"tcp://127.0.0.1:9000/status"}, "FastCGI address, e.g. unix:///tmp/php.sock;/status or tcp://127.0.0.1:9000/status")
serverCmd.Flags().BoolVar(&fixProcessCount, "phpfpm.fix-process-count", false, "Enable to calculate process numbers via php-fpm_exporter since PHP-FPM sporadically reports wrong active/idle/total process numbers.")
serverCmd.Flags().Float64Var(&defaultActiveProcesses, "phpfpm.default-active-processes", 0, "Default phpfpm_active_processes value when scrape failed.")

//viper.BindEnv("web.listen-address", "PHP_FPM_WEB_LISTEN_ADDRESS")
//viper.BindPFlag("web.listen-address", serverCmd.Flags().Lookup("web.listen-address"))
Expand All @@ -163,6 +166,7 @@ func init() {
"PHP_FPM_WEB_DISABLE_PROCESS_STATE_METRICS": "web.disable-process-state-metrics",
"PHP_FPM_SCRAPE_URI": "phpfpm.scrape-uri",
"PHP_FPM_FIX_PROCESS_COUNT": "phpfpm.fix-process-count",
"PHP_FPM_DEFAULT_ACTIVE_PROCESSES": "phpfpm.default-active-processes",
}

mapEnvVars(envs, serverCmd)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require (
github.com/fatih/color v1.9.0 // indirect
github.com/golang/protobuf v1.3.4 // indirect
github.com/gosuri/uitable v0.0.4
github.com/kanocz/fcgi_client v0.0.0-20210113082628-fff85c8adfb7
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-runewidth v0.0.8 // indirect
Expand All @@ -21,7 +22,6 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.6.2
github.com/stretchr/testify v1.5.1
github.com/tomasen/fcgi_client v0.0.0-20180423082037-2bb3d819fd19
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect
golang.org/x/text v0.3.2 // indirect
gopkg.in/ini.v1 v1.52.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
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/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
Expand Down
2 changes: 2 additions & 0 deletions phpfpm/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Exporter struct {

DisableProcessStateMetrics bool
CountProcessState bool
DefaultActiveProcesses float64

up *prometheus.Desc
scrapeFailues *prometheus.Desc
Expand Down Expand Up @@ -183,6 +184,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(e.scrapeFailues, prometheus.CounterValue, float64(pool.ScrapeFailures), pool.Name, pool.Address)

if pool.ScrapeError != nil {
ch <- prometheus.MustNewConstMetric(e.activeProcesses, prometheus.GaugeValue, e.DefaultActiveProcesses, pool.Name, pool.Address)
ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, 0, pool.Name, pool.Address)
log.Errorf("Error scraping PHP-FPM: %v", pool.ScrapeError)
continue
Expand Down
6 changes: 3 additions & 3 deletions phpfpm/phpfpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"sync"
"time"

fcgiclient "github.com/tomasen/fcgi_client"
fcgiclient "github.com/kanocz/fcgi_client"
)

// PoolProcessRequestIdle defines a process that is idle.
Expand Down Expand Up @@ -156,11 +156,11 @@ func (p *Pool) Update(outputFullStatus bool) (err error) {
return p.error(err)
}

fcgi, err := fcgiclient.DialTimeout(scheme, address, time.Duration(3)*time.Second)
fcgi, err := fcgiclient.DialTimeout(scheme, address, time.Duration(2)*time.Second)
if err != nil {
return p.error(err)
}

fcgi.SetTimeout(time.Duration(2) * time.Second)
defer fcgi.Close()
queryString := "json"
if outputFullStatus {
Expand Down