Merge pull request #7 from hipages/code-cleanup

Code cleanup
This commit is contained in:
Enrico Stahn 2018-02-26 15:00:56 +11:00 committed by GitHub
commit 07154a3a86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 73 additions and 46 deletions

View file

@ -1,23 +1,35 @@
# Golang CircleCI 2.0 configuration file defaults: &defaults
# working_directory: /go/src/github.com/hipages/php-fpm_exporter
# Check https://circleci.com/docs/2.0/language-go/ for more details
version: 2 version: 2
jobs: jobs:
build: test:
<<: *defaults
docker: docker:
- image: circleci/golang:1.9 - image: circleci/golang:1.9
working_directory: /go/src/github.com/hipages/php-fpm_exporter
steps: steps:
- checkout - 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: 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: dep ensure -vendor-only
- run: go test -v ./... - run: go test -v ./...
deploy: lint:
<<: *defaults
docker:
- 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 --disable-all --enable=errcheck --enable=vet --enable=vetshadow --vendor ./...
- run: gometalinter --disable-all --enable=vet --enable=vetshadow --vendor ./...
deploy:
<<: *defaults
docker: docker:
- image: circleci/golang:1.9 - image: circleci/golang:1.9
working_directory: /go/src/github.com/hipages/php-fpm_exporter
steps: steps:
- checkout - checkout
- setup_remote_docker: - setup_remote_docker:
@ -31,13 +43,17 @@ workflows:
version: 2 version: 2
build-n-deploy: build-n-deploy:
jobs: jobs:
- build: - test:
filters:
tags:
only: /^v.*/
- lint:
filters: filters:
tags: tags:
only: /^v.*/ only: /^v.*/
- deploy: - deploy:
requires: requires:
- build - test
filters: filters:
branches: branches:
ignore: /.*/ ignore: /.*/

View file

@ -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.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` | | `--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` | | `--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 ### CLI Examples
@ -54,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 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 ### Docker Examples
* Run docker manually * Run docker manually
@ -75,6 +81,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) [![asciicast](https://asciinema.org/a/1msR8nqAsFdHzROosUb7PiHvf.png)](https://asciinema.org/a/1msR8nqAsFdHzROosUb7PiHvf)
### Kubernetes Example
## Metrics collected ## Metrics collected
``` ```
@ -94,6 +102,12 @@ The `server` command runs the server required for prometheus to retrieve the sta
# TYPE phpfpm_max_children_reached counter # 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. # 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 # 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. # HELP phpfpm_scrape_failures The number of failures scraping from PHP-FPM.
# TYPE phpfpm_scrape_failures counter # TYPE phpfpm_scrape_failures counter
# HELP phpfpm_slow_requests The number of requests that exceeded your 'request_slowlog_timeout' value. # HELP phpfpm_slow_requests The number of requests that exceeded your 'request_slowlog_timeout' value.

View file

@ -16,11 +16,12 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"time"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/gosuri/uitable" "github.com/gosuri/uitable"
"github.com/hipages/php-fpm_exporter/phpfpm" "github.com/hipages/php-fpm_exporter/phpfpm"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"time"
) )
// Configuration variables // Configuration variables
@ -44,7 +45,9 @@ var getCmd = &cobra.Command{
pm.Add(uri) pm.Add(uri)
} }
pm.Update() if err := pm.Update(); err != nil {
log.Fatal("Could not update pool.", err)
}
switch output { switch output {
case "json": case "json":

View file

@ -101,3 +101,15 @@ func initLogger() {
log.SetLevel(lvl) 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)
}
}
}
}

View file

@ -15,15 +15,15 @@ package cmd
import ( import (
"context" "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" "net/http"
"os" "os"
"os/signal" "os/signal"
"time" "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 // Configuration variables
@ -102,7 +102,9 @@ to quickly create a Cobra application.`,
defer cancel() defer cancel()
// Doesn't block if no connections, but will otherwise wait // Doesn't block if no connections, but will otherwise wait
// until the timeout deadline. // 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 // Optionally, you could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if your application should wait for other services // <-ctx.Done() if your application should wait for other services
// to finalize based on context cancellation. // to finalize based on context cancellation.
@ -114,14 +116,6 @@ to quickly create a Cobra application.`,
func init() { func init() {
RootCmd.AddCommand(serverCmd) 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(&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().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") 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")
@ -139,11 +133,5 @@ func init() {
"PHP_FPM_FIX_PROCESS_COUNT": "phpfpm.fix-process-count", "PHP_FPM_FIX_PROCESS_COUNT": "phpfpm.fix-process-count",
} }
for env, flag := range envs { mapEnvVars(envs, serverCmd)
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)
}
}
} }

View file

@ -31,14 +31,4 @@ var versionCmd = &cobra.Command{
func init() { func init() {
RootCmd.AddCommand(versionCmd) 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")
} }

View file

@ -15,6 +15,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/hipages/php-fpm_exporter/cmd" "github.com/hipages/php-fpm_exporter/cmd"
) )

View file

@ -11,12 +11,14 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Package phpfpm provides convenient access to PHP-FPM pool data
package phpfpm package phpfpm
import ( import (
"sync"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/speps/go-hashids" "github.com/speps/go-hashids"
"sync"
) )
const ( const (

View file

@ -17,12 +17,13 @@ package phpfpm
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/tomasen/fcgi_client"
"io/ioutil" "io/ioutil"
"net/url" "net/url"
"strconv" "strconv"
"sync" "sync"
"time" "time"
"github.com/tomasen/fcgi_client"
) )
// PoolProcessRequestIdle defines a process that is idle. // PoolProcessRequestIdle defines a process that is idle.