Skip to content

Commit

Permalink
Merge pull request #5 from bakins/process-labels
Browse files Browse the repository at this point in the history
Use labels for process state
  • Loading branch information
bakins committed Jul 28, 2017
2 parents fb02c88 + 11ccb59 commit 4e10095
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 59 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.1.1
v0.3.0
77 changes: 20 additions & 57 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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":
Expand All @@ -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
Expand All @@ -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",
Expand All @@ -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)
}
}
*/
7 changes: 7 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -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`
26 changes: 26 additions & 0 deletions test/fastcgi.conf
Original file line number Diff line number Diff line change
@@ -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;
Empty file added test/index.php
Empty file.
89 changes: 89 additions & 0 deletions test/mime.types
Original file line number Diff line number Diff line change
@@ -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;
}
50 changes: 50 additions & 0 deletions test/nginx.conf
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
15 changes: 15 additions & 0 deletions test/php-fpm.conf
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4e10095

Please sign in to comment.