From daec91ff2c8e58e697707cf7738aa087c332047d Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Sun, 25 Feb 2018 10:08:12 +1100 Subject: [PATCH 01/11] Code-cleanup with goimports --- cmd/get.go | 3 ++- cmd/server.go | 17 +++++------------ cmd/version.go | 10 ---------- phpfpm/exporter.go | 4 +++- phpfpm/phpfpm.go | 3 ++- 5 files changed, 12 insertions(+), 25 deletions(-) diff --git a/cmd/get.go b/cmd/get.go index 7ecbc20..57533a0 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -16,11 +16,12 @@ package cmd import ( "encoding/json" "fmt" + "time" + "github.com/davecgh/go-spew/spew" "github.com/gosuri/uitable" "github.com/hipages/php-fpm_exporter/phpfpm" "github.com/spf13/cobra" - "time" ) // Configuration variables diff --git a/cmd/server.go b/cmd/server.go index 3e15f38..3146a34 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -16,14 +16,15 @@ package cmd import ( "context" "fmt" - "github.com/hipages/php-fpm_exporter/phpfpm" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promhttp" - "github.com/spf13/cobra" "net/http" "os" "os/signal" "time" + + "github.com/hipages/php-fpm_exporter/phpfpm" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/spf13/cobra" ) // Configuration variables @@ -114,14 +115,6 @@ to quickly create a Cobra application.`, func init() { RootCmd.AddCommand(serverCmd) - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // serverCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: serverCmd.Flags().StringVar(&listeningAddress, "web.listen-address", ":9253", "Address on which to expose metrics and web interface.") serverCmd.Flags().StringVar(&metricsEndpoint, "web.telemetry-path", "/metrics", "Path under which to expose metrics.") 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") diff --git a/cmd/version.go b/cmd/version.go index 9cb124e..6ad7252 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -31,14 +31,4 @@ var versionCmd = &cobra.Command{ func init() { RootCmd.AddCommand(versionCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // versionCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/phpfpm/exporter.go b/phpfpm/exporter.go index 69af823..08c0fb2 100644 --- a/phpfpm/exporter.go +++ b/phpfpm/exporter.go @@ -11,12 +11,14 @@ // See the License for the specific language governing permissions and // limitations under the License. +// Package phpfpm provides convenient access to PHP-FPM pool data package phpfpm import ( + "sync" + "github.com/prometheus/client_golang/prometheus" "github.com/speps/go-hashids" - "sync" ) const ( diff --git a/phpfpm/phpfpm.go b/phpfpm/phpfpm.go index 5a3f1a0..06f3cba 100644 --- a/phpfpm/phpfpm.go +++ b/phpfpm/phpfpm.go @@ -17,12 +17,13 @@ package phpfpm import ( "encoding/json" "fmt" - "github.com/tomasen/fcgi_client" "io/ioutil" "net/url" "strconv" "sync" "time" + + "github.com/tomasen/fcgi_client" ) // PoolProcessRequestIdle defines a process that is idle. From 86c787e350d478331cf578178dfc1008b9c7699d Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Sun, 25 Feb 2018 10:18:47 +1100 Subject: [PATCH 02/11] Split test and lint for CircleCI --- .circleci/config.yml | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 5e59a88..7e17c9f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,23 +1,32 @@ -# Golang CircleCI 2.0 configuration file -# -# Check https://circleci.com/docs/2.0/language-go/ for more details +defaults: &defaults + working_directory: /go/src/github.com/hipages/php-fpm_exporter + version: 2 jobs: - build: + test: + <<: *defaults docker: - image: circleci/golang:1.9 - working_directory: /go/src/github.com/hipages/php-fpm_exporter steps: - checkout - run: curl -L -s https://github.com/golang/dep/releases/download/v0.4.1/dep-linux-amd64 -o /go/bin/dep && chmod +x /go/bin/dep - run: dep ensure -vendor-only - run: go test -v ./... - deploy: + lint: + <<: *defaults + docker: + - image: circleci/golang:1.9 + steps: + - checkout + - run: curl -L -s https://github.com/alecthomas/gometalinter/releases/download/v2.0.5/gometalinter-2.0.5-linux-amd64.tar.gz | tar xvfz - -C /go/bin/ --strip 1 + - run: gometalinter ./... + + deploy: + <<: *defaults docker: - image: circleci/golang:1.9 - working_directory: /go/src/github.com/hipages/php-fpm_exporter steps: - checkout - setup_remote_docker: @@ -31,7 +40,11 @@ workflows: version: 2 build-n-deploy: jobs: - - build: + - test: + filters: + tags: + only: /^v.*/ + - lint: filters: tags: only: /^v.*/ From 90766c44b68bf044cf788696a6c786e5791bc43b Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Sun, 25 Feb 2018 11:10:08 +1100 Subject: [PATCH 03/11] Add Kubernets Example section header --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 2b8b8f3..80f8bd7 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,8 @@ The `server` command runs the server required for prometheus to retrieve the sta [![asciicast](https://asciinema.org/a/1msR8nqAsFdHzROosUb7PiHvf.png)](https://asciinema.org/a/1msR8nqAsFdHzROosUb7PiHvf) +### Kubernetes Example + ## Metrics collected ``` From ad238dacbed5681c7e225eea9aa82758ea8e3d44 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Sun, 25 Feb 2018 11:13:14 +1100 Subject: [PATCH 04/11] Fixing CircleCI workflow --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7e17c9f..88ad1f1 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -50,7 +50,7 @@ workflows: only: /^v.*/ - deploy: requires: - - build + - test filters: branches: ignore: /.*/ From 5ddcff2dc8a3902c4238e5994d9f264f1357d883 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Sun, 25 Feb 2018 11:17:56 +1100 Subject: [PATCH 05/11] Add vendors for linting job --- .circleci/config.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 88ad1f1..dcfde49 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -20,6 +20,8 @@ jobs: - image: circleci/golang:1.9 steps: - checkout + - run: curl -L -s https://github.com/golang/dep/releases/download/v0.4.1/dep-linux-amd64 -o /go/bin/dep && chmod +x /go/bin/dep + - run: dep ensure -vendor-only - run: curl -L -s https://github.com/alecthomas/gometalinter/releases/download/v2.0.5/gometalinter-2.0.5-linux-amd64.tar.gz | tar xvfz - -C /go/bin/ --strip 1 - run: gometalinter ./... From 38b9c07ab7b7a50b293dda3ad3f4721682871a20 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Sun, 25 Feb 2018 11:20:36 +1100 Subject: [PATCH 06/11] Enable only some linters --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index dcfde49..51909d3 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -23,7 +23,7 @@ jobs: - run: curl -L -s https://github.com/golang/dep/releases/download/v0.4.1/dep-linux-amd64 -o /go/bin/dep && chmod +x /go/bin/dep - run: dep ensure -vendor-only - run: curl -L -s https://github.com/alecthomas/gometalinter/releases/download/v2.0.5/gometalinter-2.0.5-linux-amd64.tar.gz | tar xvfz - -C /go/bin/ --strip 1 - - run: gometalinter ./... + - run: gometalinter --disable-all --enable=errcheck --enable=vet --enable=vetshadow ./... deploy: <<: *defaults From 3ffe5787c8354b4ca2f4a6aa42d6818937b3577e Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Sun, 25 Feb 2018 21:45:46 +1100 Subject: [PATCH 07/11] More error checks thanks to errcheck --- cmd/get.go | 4 +++- cmd/server.go | 8 ++++++-- main.go | 1 + 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cmd/get.go b/cmd/get.go index 57533a0..94d44db 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -45,7 +45,9 @@ var getCmd = &cobra.Command{ pm.Add(uri) } - pm.Update() + if err := pm.Update(); err != nil { + log.Fatal("Could not update pool.", err) + } switch output { case "json": diff --git a/cmd/server.go b/cmd/server.go index 3146a34..4aa851e 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -103,7 +103,9 @@ to quickly create a Cobra application.`, defer cancel() // Doesn't block if no connections, but will otherwise wait // until the timeout deadline. - srv.Shutdown(ctx) + if err := srv.Shutdown(ctx); err != nil { + log.Fatal("Error during shutdown", err) + } // Optionally, you could run srv.Shutdown in a goroutine and block on // <-ctx.Done() if your application should wait for other services // to finalize based on context cancellation. @@ -136,7 +138,9 @@ func init() { flag := serverCmd.Flags().Lookup(flag) flag.Usage = fmt.Sprintf("%v [env %v]", flag.Usage, env) if value := os.Getenv(env); value != "" { - flag.Value.Set(value) + if err := flag.Value.Set(value); err != nil { + log.Error(err) + } } } } diff --git a/main.go b/main.go index 8fffe69..bb05034 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ package main import ( "fmt" + "github.com/hipages/php-fpm_exporter/cmd" ) From d119057d0e9c58d83f64949c1933fcab8f0af7d1 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Sun, 25 Feb 2018 21:46:14 +1100 Subject: [PATCH 08/11] Disable errcheck linter --- .circleci/config.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 51909d3..3232389 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -23,7 +23,8 @@ jobs: - run: curl -L -s https://github.com/golang/dep/releases/download/v0.4.1/dep-linux-amd64 -o /go/bin/dep && chmod +x /go/bin/dep - run: dep ensure -vendor-only - run: curl -L -s https://github.com/alecthomas/gometalinter/releases/download/v2.0.5/gometalinter-2.0.5-linux-amd64.tar.gz | tar xvfz - -C /go/bin/ --strip 1 - - run: gometalinter --disable-all --enable=errcheck --enable=vet --enable=vetshadow ./... +# - run: gometalinter --disable-all --enable=errcheck --enable=vet --enable=vetshadow --vendor ./... + - run: gometalinter --disable-all --enable=vet --enable=vetshadow --vendor ./... deploy: <<: *defaults From e3385e0a3146bf5ceed3761ceb0f935f98be19f5 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Sun, 25 Feb 2018 22:06:23 +1100 Subject: [PATCH 09/11] Update README.md --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 80f8bd7..3715012 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,8 @@ The `server` command runs the server required for prometheus to retrieve the sta | `--web.listen-address` | Address on which to expose metrics and web interface. | `PHP_FPM_WEB_LISTEN_ADDRESS` | [`:9253`](https://github.com/prometheus/prometheus/wiki/Default-port-allocations) | | `--web.telemetry-path` | Path under which to expose metrics. | `PHP_FPM_WEB_TELEMETRY_PATH` | `/metrics` | | `--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` | -| `--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 | +| `--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` | +| `--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 | ### CLI Examples @@ -129,4 +130,4 @@ Before starting any work, please either comment on an existing issue, or file a * [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) \ No newline at end of file +* [craigmj/phpfpm_exporter](https://github.com/craigmj/phpfpm_exporter) From 4596f8c89b31bc65cf851e7fab461542e2745e09 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Sun, 25 Feb 2018 22:14:25 +1100 Subject: [PATCH 10/11] Refactor environment mapping into separate function --- cmd/root.go | 12 ++++++++++++ cmd/server.go | 11 +---------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index d19e5c4..9899ccc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -101,3 +101,15 @@ func initLogger() { log.SetLevel(lvl) } + +func mapEnvVars(envs map[string]string, cmd *cobra.Command) { + for env, flag := range envs { + flag := cmd.Flags().Lookup(flag) + flag.Usage = fmt.Sprintf("%v [env %v]", flag.Usage, env) + if value := os.Getenv(env); value != "" { + if err := flag.Value.Set(value); err != nil { + log.Error(err) + } + } + } +} diff --git a/cmd/server.go b/cmd/server.go index 4aa851e..4cb2beb 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -15,7 +15,6 @@ package cmd import ( "context" - "fmt" "net/http" "os" "os/signal" @@ -134,13 +133,5 @@ func init() { "PHP_FPM_FIX_PROCESS_COUNT": "phpfpm.fix-process-count", } - for env, flag := range envs { - flag := serverCmd.Flags().Lookup(flag) - flag.Usage = fmt.Sprintf("%v [env %v]", flag.Usage, env) - if value := os.Getenv(env); value != "" { - if err := flag.Value.Set(value); err != nil { - log.Error(err) - } - } - } + mapEnvVars(envs, serverCmd) } From 698089f84cda0f774ccd23b5ffe02909f088f4e9 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Mon, 26 Feb 2018 09:36:01 +1100 Subject: [PATCH 11/11] * Add an example for environment variable usage * Update Metrics collected section --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 3715012..7bf3b1e 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,11 @@ The `server` command runs the server required for prometheus to retrieve the sta 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: + ``` + PHP_FPM_FIX_PROCESS_COUNT=1 go run main.go server --web.listen-address ":12345" --log.level=debug + ``` + ### Docker Examples * Run docker manually @@ -97,6 +102,12 @@ The `server` command runs the server required for prometheus to retrieve the sta # TYPE phpfpm_max_children_reached counter # HELP phpfpm_max_listen_queue The maximum number of requests in the queue of pending connections since FPM has started. # TYPE phpfpm_max_listen_queue counter +# HELP phpfpm_process_last_request_cpu +# TYPE phpfpm_process_last_request_cpu gauge +# HELP phpfpm_process_last_request_memory +# TYPE phpfpm_process_last_request_memory gauge +# HELP phpfpm_process_requests +# TYPE phpfpm_process_requests counter # HELP phpfpm_scrape_failures The number of failures scraping from PHP-FPM. # TYPE phpfpm_scrape_failures counter # HELP phpfpm_slow_requests The number of requests that exceeded your 'request_slowlog_timeout' value.