Skip to content
Open
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
phpfpm_exporter
.idea
/test
107 changes: 107 additions & 0 deletions phpfpm_exporter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"bufio"
"io"
"net"
"net/http/httptest"
"os"
"path"
"runtime"
"testing"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
)

const (
testSocket = "/tmp/phpfpmtest.sock"
phpfpmStatus = `pool: www
process manager: ondemand
start time: 04/Jan/2019:17:59:31 +0000
start since: 1277299
accepted conn: 51602
listen queue: 0
max listen queue: 14
listen queue len: 128
idle processes: 0
active processes: 1
total processes: 1
max active processes: 42
max children reached: 0
slow requests: 0
`
)

type haproxy struct {
*httptest.Server
response []byte
}

func expectMetrics(t *testing.T, c prometheus.Collector, fixture string) {
exp, err := os.Open(path.Join("test", fixture))
if err != nil {
t.Fatalf("Error opening fixture file %q: %v", fixture, err)
}
if err := testutil.CollectAndCompare(c, exp); err != nil {
t.Fatal("Unexpected metrics returned:", err)
}
}

func newHaproxyUnix(file, statsPayload string) (io.Closer, error) {
if err := os.Remove(file); err != nil && !os.IsNotExist(err) {
return nil, err
}
l, err := net.Listen("unix", file)
if err != nil {
return nil, err
}

go func() {
for {
c, err := l.Accept()
if err != nil {
return
}
go func(c net.Conn) {
defer c.Close()
r := bufio.NewScanner(c)
for r.Scan() {

//l, err := r.ReadString('\n')
//if err != nil {
// return
//}
switch r.Text() {
case "show stat\n":
c.Write([]byte(statsPayload))
return
default:
// invalid command
return
}
}
}(c)
}
}()
return l, nil
}

func TestUnixDomain(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("not on windows")
return
}
srv, err := newHaproxyUnix(testSocket, phpfpmStatus)
if err != nil {
t.Fatalf("can't start test server: %v", err)
}
defer srv.Close()

e, err := NewPhpfpmExporter([]string{testSocket}, "/status")
if err != nil {
t.Fatal(err)
}

expectMetrics(t, e, "unix_domain.metrics")
}
33 changes: 33 additions & 0 deletions test/unix_domain.metrics
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# HELP php_fpm_accepted_connections_total Number of request accepted by the pool.
# TYPE php_fpm_accepted_connections_total counter
php_fpm_accepted_connections_total{socket_path="/tmp/phpfpmtest.sock"} 3
# HELP php_fpm_active_processes Number of active processes.
# TYPE php_fpm_active_processes gauge
php_fpm_active_processes{socket_path="/tmp/phpfpmtest.sock"} 1
# HELP php_fpm_idle_processes Number of idle processes.
# TYPE php_fpm_idle_processes gauge
php_fpm_idle_processes{socket_path="/tmp/phpfpmtest.sock"} 1
# HELP php_fpm_listen_queue Number of request in the queue of pending connections.
# TYPE php_fpm_listen_queue gauge
php_fpm_listen_queue{socket_path="/tmp/phpfpmtest.sock"} 0
# HELP php_fpm_listen_queue_length The size of the socket queue of pending connections.
# TYPE php_fpm_listen_queue_length gauge
php_fpm_listen_queue_length{socket_path="/tmp/phpfpmtest.sock"} 0
# HELP php_fpm_max_active_processes Maximum number of active processes since FPM has started.
# TYPE php_fpm_max_active_processes gauge
php_fpm_max_active_processes{socket_path="/tmp/phpfpmtest.sock"} 1
# HELP php_fpm_max_children_reached Number of times, the process limit has been reached.
# TYPE php_fpm_max_children_reached gauge
php_fpm_max_children_reached{socket_path="/tmp/phpfpmtest.sock"} 0
# HELP php_fpm_max_listen_queue Maximum number of requests in the queue of pending connections since FPM has started.
# TYPE php_fpm_max_listen_queue gauge
php_fpm_max_listen_queue{socket_path="/tmp/phpfpmtest.sock"} 0
# HELP php_fpm_slow_requests Enable php-fpm slow-log before you consider this. If this value is non-zero you may have slow php processes.
# TYPE php_fpm_slow_requests gauge
php_fpm_slow_requests{socket_path="/tmp/phpfpmtest.sock"} 0
# HELP php_fpm_start_time_seconds Unix time when FPM has started or reloaded.
# TYPE php_fpm_start_time_seconds gauge
php_fpm_start_time_seconds{socket_path="/tmp/phpfpmtest.sock"} 1.547860664e+09
# HELP php_fpm_up Whether scraping PHP-FPM's metrics was successful.
# TYPE php_fpm_up gauge
php_fpm_up{socket_path="/tmp/phpfpmtest.sock"} 1