From 11ccb59b6cf9a6d15ac2e5b7f8664c8f969c0475 Mon Sep 17 00:00:00 2001 From: Brian Akins Date: Fri, 28 Jul 2017 07:29:36 -0400 Subject: [PATCH] Use labels for process state --- README.md | 2 +- VERSION | 2 +- collector.go | 77 +++++++++++----------------------------- test/README.md | 7 ++++ test/fastcgi.conf | 26 ++++++++++++++ test/index.php | 0 test/mime.types | 89 +++++++++++++++++++++++++++++++++++++++++++++++ test/nginx.conf | 50 ++++++++++++++++++++++++++ test/php-fpm.conf | 15 ++++++++ 9 files changed, 209 insertions(+), 59 deletions(-) create mode 100644 test/README.md create mode 100644 test/fastcgi.conf create mode 100644 test/index.php create mode 100644 test/mime.types create mode 100644 test/nginx.conf create mode 100644 test/php-fpm.conf diff --git a/README.md b/README.md index 58bad84..d2435bd 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Usage: Flags: --addr string listen address for metrics handler (default "127.0.0.1:8080") - --endpoint string address of driveshaft (default "http://127.0.0.1:9000/status") + --endpoint string url of php-fpm (default "http://127.0.0.1:9000/status") ``` When running, a simple healthcheck is available on `/healthz` diff --git a/VERSION b/VERSION index 8308b63..268b033 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v0.1.1 +v0.3.0 diff --git a/collector.go b/collector.go index cb5a6ca..9720c93 100644 --- a/collector.go +++ b/collector.go @@ -24,9 +24,7 @@ type collector struct { listenQueue *prometheus.Desc maxListenQueue *prometheus.Desc listenQueueLength *prometheus.Desc - idleProcesses *prometheus.Desc - activeProcesses *prometheus.Desc - totalProcesses *prometheus.Desc + phpProcesses *prometheus.Desc maxActiveProcesses *prometheus.Desc maxChildrenReached *prometheus.Desc slowRequests *prometheus.Desc @@ -36,28 +34,26 @@ type collector struct { const metricsNamespace = "phpfpm" -func newFuncMetric(metricName string, docString string) *prometheus.Desc { +func newFuncMetric(metricName string, docString string, labels []string) *prometheus.Desc { return prometheus.NewDesc( prometheus.BuildFQName(metricsNamespace, "", metricName), - docString, nil, nil, + docString, labels, nil, ) } func (e *Exporter) newCollector() *collector { return &collector{ exporter: e, - up: newFuncMetric("up", "able to contact php-fpm"), - acceptedConn: newFuncMetric("accepted_connections_total", "Total number of accepted connections"), - listenQueue: newFuncMetric("listen_queue_connections", "Number of connections that have been initiated but not yet accepted"), - maxListenQueue: newFuncMetric("listen_queue_max_connections", "Max number of connections the listen queue has reached since FPM start"), - listenQueueLength: newFuncMetric("listen_queue_length_connections", "The length of the socket queue, dictating maximum number of pending connections"), - idleProcesses: newFuncMetric("idle_processes", "Idle process count"), - activeProcesses: newFuncMetric("active_processes", "Active process count"), - totalProcesses: newFuncMetric("total_processes", "Total process count"), - maxActiveProcesses: newFuncMetric("active_max_processes", "Maximum active process count"), - maxChildrenReached: newFuncMetric("max_children_reached_total", "Number of times the process limit has been reached"), - slowRequests: newFuncMetric("slow_requests_total", "Number of requests that exceed request_slowlog_timeout"), - scrapeFailures: newFuncMetric("scrape_failures_total", "Number of errors while scraping php_fpm"), + up: newFuncMetric("up", "able to contact php-fpm", nil), + acceptedConn: newFuncMetric("accepted_connections_total", "Total number of accepted connections", nil), + listenQueue: newFuncMetric("listen_queue_connections", "Number of connections that have been initiated but not yet accepted", nil), + maxListenQueue: newFuncMetric("listen_queue_max_connections", "Max number of connections the listen queue has reached since FPM start", nil), + listenQueueLength: newFuncMetric("listen_queue_length_connections", "The length of the socket queue, dictating maximum number of pending connections", nil), + phpProcesses: newFuncMetric("processes_total", "process count", []string{"state"}), + maxActiveProcesses: newFuncMetric("active_max_processes", "Maximum active process count", nil), + maxChildrenReached: newFuncMetric("max_children_reached_total", "Number of times the process limit has been reached", nil), + slowRequests: newFuncMetric("slow_requests_total", "Number of requests that exceed request_slowlog_timeout", nil), + scrapeFailures: newFuncMetric("scrape_failures_total", "Number of errors while scraping php_fpm", nil), } } @@ -68,9 +64,7 @@ func (c *collector) Describe(ch chan<- *prometheus.Desc) { ch <- c.listenQueue ch <- c.maxListenQueue ch <- c.listenQueueLength - ch <- c.idleProcesses - ch <- c.activeProcesses - ch <- c.totalProcesses + ch <- c.phpProcesses ch <- c.maxActiveProcesses ch <- c.maxChildrenReached ch <- c.slowRequests @@ -140,6 +134,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) { var desc *prometheus.Desc var valueType prometheus.ValueType + labels := []string{} switch key { case "accepted conn": @@ -155,14 +150,13 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) { desc = c.listenQueueLength valueType = prometheus.GaugeValue case "idle processes": - desc = c.idleProcesses + desc = c.phpProcesses valueType = prometheus.GaugeValue + labels = append(labels, "idle") case "active processes": - desc = c.activeProcesses - valueType = prometheus.GaugeValue - case "total processes": - desc = c.totalProcesses + desc = c.phpProcesses valueType = prometheus.GaugeValue + labels = append(labels, "active") case "max active processes": desc = c.maxActiveProcesses valueType = prometheus.CounterValue @@ -176,7 +170,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) { continue } - m, err := prometheus.NewConstMetric(desc, valueType, float64(value)) + m, err := prometheus.NewConstMetric(desc, valueType, float64(value), labels...) if err != nil { c.exporter.logger.Error( "failed to create metrics", @@ -189,34 +183,3 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) { ch <- m } } - -/* -func (c *collector) collectThreads(ch chan<- prometheus.Metric) { - t, err := c.driveshaft.getThreads() - if err != nil { - c.exporter.logger.Error("failed to get driveshaft status", zap.Error(err)) - ch <- prometheus.MustNewConstMetric( - c.up, - prometheus.GaugeValue, - float64(0), - ) - return - } - - ch <- prometheus.MustNewConstMetric( - c.up, - prometheus.GaugeValue, - float64(1), - ) - - for _, v := range t { - ch <- prometheus.MustNewConstMetric( - c.threadsGauge, - prometheus.GaugeValue, - float64(v.count), - v.function, v.state) - - } -} - -*/ diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000..ab5b530 --- /dev/null +++ b/test/README.md @@ -0,0 +1,7 @@ +This directory contains simple configurations for testing. + +`php-fpm --nodaemonize --fpm-config ./php-fpm.conf` + +`nginx -c `pwd`/nginx.conf` + +`php-fpm-exporter` diff --git a/test/fastcgi.conf b/test/fastcgi.conf new file mode 100644 index 0000000..091738c --- /dev/null +++ b/test/fastcgi.conf @@ -0,0 +1,26 @@ + +fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; +fastcgi_param QUERY_STRING $query_string; +fastcgi_param REQUEST_METHOD $request_method; +fastcgi_param CONTENT_TYPE $content_type; +fastcgi_param CONTENT_LENGTH $content_length; + +fastcgi_param SCRIPT_NAME $fastcgi_script_name; +fastcgi_param REQUEST_URI $request_uri; +fastcgi_param DOCUMENT_URI $document_uri; +fastcgi_param DOCUMENT_ROOT $document_root; +fastcgi_param SERVER_PROTOCOL $server_protocol; +fastcgi_param REQUEST_SCHEME $scheme; +fastcgi_param HTTPS $https if_not_empty; + +fastcgi_param GATEWAY_INTERFACE CGI/1.1; +fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; + +fastcgi_param REMOTE_ADDR $remote_addr; +fastcgi_param REMOTE_PORT $remote_port; +fastcgi_param SERVER_ADDR $server_addr; +fastcgi_param SERVER_PORT $server_port; +fastcgi_param SERVER_NAME $server_name; + +# PHP only, required if PHP was built with --enable-force-cgi-redirect +fastcgi_param REDIRECT_STATUS 200; diff --git a/test/index.php b/test/index.php new file mode 100644 index 0000000..e69de29 diff --git a/test/mime.types b/test/mime.types new file mode 100644 index 0000000..89be9a4 --- /dev/null +++ b/test/mime.types @@ -0,0 +1,89 @@ + +types { + text/html html htm shtml; + text/css css; + text/xml xml; + image/gif gif; + image/jpeg jpeg jpg; + application/javascript js; + application/atom+xml atom; + application/rss+xml rss; + + text/mathml mml; + text/plain txt; + text/vnd.sun.j2me.app-descriptor jad; + text/vnd.wap.wml wml; + text/x-component htc; + + image/png png; + image/tiff tif tiff; + image/vnd.wap.wbmp wbmp; + image/x-icon ico; + image/x-jng jng; + image/x-ms-bmp bmp; + image/svg+xml svg svgz; + image/webp webp; + + application/font-woff woff; + application/java-archive jar war ear; + application/json json; + application/mac-binhex40 hqx; + application/msword doc; + application/pdf pdf; + application/postscript ps eps ai; + application/rtf rtf; + application/vnd.apple.mpegurl m3u8; + application/vnd.ms-excel xls; + application/vnd.ms-fontobject eot; + application/vnd.ms-powerpoint ppt; + application/vnd.wap.wmlc wmlc; + application/vnd.google-earth.kml+xml kml; + application/vnd.google-earth.kmz kmz; + application/x-7z-compressed 7z; + application/x-cocoa cco; + application/x-java-archive-diff jardiff; + application/x-java-jnlp-file jnlp; + application/x-makeself run; + application/x-perl pl pm; + application/x-pilot prc pdb; + application/x-rar-compressed rar; + application/x-redhat-package-manager rpm; + application/x-sea sea; + application/x-shockwave-flash swf; + application/x-stuffit sit; + application/x-tcl tcl tk; + application/x-x509-ca-cert der pem crt; + application/x-xpinstall xpi; + application/xhtml+xml xhtml; + application/xspf+xml xspf; + application/zip zip; + + application/octet-stream bin exe dll; + application/octet-stream deb; + application/octet-stream dmg; + application/octet-stream iso img; + application/octet-stream msi msp msm; + + application/vnd.openxmlformats-officedocument.wordprocessingml.document docx; + application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx; + application/vnd.openxmlformats-officedocument.presentationml.presentation pptx; + + audio/midi mid midi kar; + audio/mpeg mp3; + audio/ogg ogg; + audio/x-m4a m4a; + audio/x-realaudio ra; + + video/3gpp 3gpp 3gp; + video/mp2t ts; + video/mp4 mp4; + video/mpeg mpeg mpg; + video/quicktime mov; + video/webm webm; + video/x-flv flv; + video/x-m4v m4v; + video/x-mng mng; + video/x-ms-asf asx asf; + video/x-ms-wmv wmv; + video/x-msvideo avi; +} diff --git a/test/nginx.conf b/test/nginx.conf new file mode 100644 index 0000000..fda4e50 --- /dev/null +++ b/test/nginx.conf @@ -0,0 +1,50 @@ +worker_processes 1; +pid /tmp/nginx.pid; +daemon off; + +events { + worker_connections 1024; +} + +http { + default_type application/octet-stream; + server_tokens off; + + upstream php { + server 127.0.0.1:9090; + keepalive 5; + } + + access_log off; + + server { + listen 9000 default_server; + + location ~ ^/(status|ping)$ { + fastcgi_param HTTP_PROXY ""; + fastcgi_pass php; + fastcgi_keep_conn on; + include fastcgi.conf; + } + + root .; + + index index.html index.php; + + location / { + try_files $uri $uri/ /index.php?$args; + } + + location ~ [^/]\.php(/|$) { + fastcgi_split_path_info ^(.+?\.php)(/.*)$; + if (!-f $document_root$fastcgi_script_name) { + return 404; + } + fastcgi_param HTTP_PROXY ""; + fastcgi_pass php; + fastcgi_index index.php; + fastcgi_keep_conn on; + include fastcgi.conf; + } + } +} diff --git a/test/php-fpm.conf b/test/php-fpm.conf new file mode 100644 index 0000000..1293083 --- /dev/null +++ b/test/php-fpm.conf @@ -0,0 +1,15 @@ +[global] +daemonize = no +error_log = /dev/stderr + +[www] +clear_env = no +user = nobody +group = nobody +listen = 127.0.0.1:9090 +pm = static +pm.max_children = 5 +pm.max_requests = 499 +pm.status_path = /status +ping.path = /ping +catch_workers_output = yes