From 0d25732143e2777409f7d44d2b34eb4eb79546b7 Mon Sep 17 00:00:00 2001 From: itcsoft54 Date: Sat, 21 Nov 2020 12:14:15 +0100 Subject: [PATCH] fix: high cardinality of pid_hash (#124) fixes #110 #76 BREAKING CHANGE: `pid_hash` is being removed in favour of `child` to avoid high cardinality explosion. In turn this means processes and their state changes can't be identified anymore. If you're using this behaviour please open an issue. --- phpfpm/exporter.go | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/phpfpm/exporter.go b/phpfpm/exporter.go index ca43d24..b15df46 100644 --- a/phpfpm/exporter.go +++ b/phpfpm/exporter.go @@ -16,9 +16,8 @@ package phpfpm import ( "sync" - + "fmt" "github.com/prometheus/client_golang/prometheus" - hashids "github.com/speps/go-hashids" ) const ( @@ -140,31 +139,31 @@ func NewExporter(pm PoolManager) *Exporter { processRequests: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "process_requests"), "The number of requests the process has served.", - []string{"pool", "pid_hash", "scrape_uri"}, + []string{"pool", "child", "scrape_uri"}, nil), processLastRequestMemory: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "process_last_request_memory"), "The max amount of memory the last request consumed.", - []string{"pool", "pid_hash", "scrape_uri"}, + []string{"pool", "child", "scrape_uri"}, nil), processLastRequestCPU: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "process_last_request_cpu"), "The %cpu the last request consumed.", - []string{"pool", "pid_hash", "scrape_uri"}, + []string{"pool", "child", "scrape_uri"}, nil), processRequestDuration: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "process_request_duration"), "The duration in microseconds of the requests.", - []string{"pool", "pid_hash", "scrape_uri"}, + []string{"pool", "child", "scrape_uri"}, nil), processState: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "process_state"), "The state of the process (Idle, Running, ...).", - []string{"pool", "pid_hash", "state", "scrape_uri"}, + []string{"pool", "child", "state", "scrape_uri"}, nil), } } @@ -211,13 +210,13 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) { ch <- prometheus.MustNewConstMetric(e.maxChildrenReached, prometheus.CounterValue, float64(pool.MaxChildrenReached), pool.Name, pool.Address) ch <- prometheus.MustNewConstMetric(e.slowRequests, prometheus.CounterValue, float64(pool.SlowRequests), pool.Name, pool.Address) - for _, process := range pool.Processes { - pidHash := calculateProcessHash(process) - ch <- prometheus.MustNewConstMetric(e.processState, prometheus.GaugeValue, 1, pool.Name, pidHash, process.State, pool.Address) - ch <- prometheus.MustNewConstMetric(e.processRequests, prometheus.CounterValue, float64(process.Requests), pool.Name, pidHash, pool.Address) - ch <- prometheus.MustNewConstMetric(e.processLastRequestMemory, prometheus.GaugeValue, float64(process.LastRequestMemory), pool.Name, pidHash, pool.Address) - ch <- prometheus.MustNewConstMetric(e.processLastRequestCPU, prometheus.GaugeValue, process.LastRequestCPU, pool.Name, pidHash, pool.Address) - ch <- prometheus.MustNewConstMetric(e.processRequestDuration, prometheus.GaugeValue, float64(process.RequestDuration), pool.Name, pidHash, pool.Address) + for childNumber, process := range pool.Processes { + childName := fmt.Sprintf("%d", childNumber) + ch <- prometheus.MustNewConstMetric(e.processState, prometheus.GaugeValue, 1, pool.Name, childName, process.State, pool.Address) + ch <- prometheus.MustNewConstMetric(e.processRequests, prometheus.CounterValue, float64(process.Requests), pool.Name, childName, pool.Address) + ch <- prometheus.MustNewConstMetric(e.processLastRequestMemory, prometheus.GaugeValue, float64(process.LastRequestMemory), pool.Name, childName, pool.Address) + ch <- prometheus.MustNewConstMetric(e.processLastRequestCPU, prometheus.GaugeValue, process.LastRequestCPU, pool.Name, childName, pool.Address) + ch <- prometheus.MustNewConstMetric(e.processRequestDuration, prometheus.GaugeValue, float64(process.RequestDuration), pool.Name, childName, pool.Address) } } } @@ -242,14 +241,3 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) { ch <- e.processLastRequestCPU ch <- e.processRequestDuration } - -// calculateProcessHash generates a unique identifier for a process to ensure uniqueness across multiple systems/containers -func calculateProcessHash(pp PoolProcess) string { - hd := hashids.NewData() - hd.Salt = "php-fpm_exporter" - hd.MinLength = 12 - h, _ := hashids.NewWithData(hd) - e, _ := h.Encode([]int{int(pp.StartTime), int(pp.PID)}) - - return e -}