Skip to content

Commit 1a2619c

Browse files
author
anahan
committed
Add support for port-only listen format to phpfpm prom collector
1 parent 0e3dec7 commit 1a2619c

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

pkg/phpfpm/prom_collector.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package phpfpm
22

33
import (
4+
"strings"
45
"sync"
6+
"unicode"
57

68
"github.com/prometheus/client_golang/prometheus"
79
"go.uber.org/zap"
@@ -42,8 +44,47 @@ func (c *PromCollector) setAndCollect(gaugeVec *prometheus.GaugeVec, poolName st
4244
gauge.Collect(ch)
4345
}
4446

47+
func (c *PromCollector) getStatusListenPath(pool Pool) string {
48+
if pool.StatusListen != "" {
49+
return pool.StatusListen
50+
}
51+
52+
return pool.Listen
53+
}
54+
55+
func (c *PromCollector) isDigitOnlyStr(s string) bool {
56+
for _, r := range s {
57+
if !unicode.IsDigit(r) {
58+
return false
59+
}
60+
}
61+
62+
return true
63+
}
64+
65+
func (c *PromCollector) listenToNetAndAddr(listen string) (string, string) {
66+
network := "unix"
67+
const localhost = "127.0.0.1"
68+
semicolonPos := strings.IndexByte(listen, ':')
69+
70+
if semicolonPos != -1 {
71+
network = "tcp"
72+
if semicolonPos == 0 {
73+
listen = localhost + listen
74+
}
75+
}
76+
77+
if c.isDigitOnlyStr(listen) {
78+
network = "tcp"
79+
listen = localhost + ":" + listen
80+
}
81+
82+
return network, listen
83+
}
84+
4585
func (c *PromCollector) collectForPool(pool Pool, ch chan<- prometheus.Metric) {
46-
status, err := GetStats(pool.Listen, pool.StatusPath)
86+
net, addr := c.listenToNetAndAddr(c.getStatusListenPath(pool))
87+
status, err := GetStats(net, addr, pool.StatusPath)
4788
if err != nil {
4889
c.log.Error("can't collect metrics", zap.String("pool", pool.Name), zap.Error(err))
4990
return

pkg/phpfpm/status.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package phpfpm
33
import (
44
"encoding/json"
55
"io"
6-
"strings"
76
"time"
87

98
"github.com/tomasen/fcgi_client"
@@ -26,13 +25,8 @@ type Status struct {
2625
SlowRequests int `json:"slow requests"`
2726
}
2827

29-
func GetStats(listen, statusPath string) (*Status, error) {
30-
network := "tcp"
31-
if strings.Contains(listen, "/") && listen[0:1] != "[" {
32-
network = "unix"
33-
}
34-
35-
fcgi, err := fcgiclient.DialTimeout(network, listen, time.Second)
28+
func GetStats(net, addr, statusPath string) (*Status, error) {
29+
fcgi, err := fcgiclient.DialTimeout(net, addr, time.Second)
3630
if err != nil {
3731
return nil, err
3832
}

0 commit comments

Comments
 (0)