From 10075fec94d99e319b5f0673d2c1999d507065d2 Mon Sep 17 00:00:00 2001 From: gudvyach Date: Fri, 23 May 2025 11:40:06 +0300 Subject: [PATCH 1/3] Add slab metrics in nginx plus collector --- collector/nginx_plus.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/collector/nginx_plus.go b/collector/nginx_plus.go index 480911aa..98c1de85 100644 --- a/collector/nginx_plus.go +++ b/collector/nginx_plus.go @@ -35,6 +35,7 @@ type NginxPlusCollector struct { logger *slog.Logger cacheZoneMetrics map[string]*prometheus.Desc workerMetrics map[string]*prometheus.Desc + slabsMetrics map[string]*prometheus.Desc nginxClient *plusclient.NginxClient streamServerZoneMetrics map[string]*prometheus.Desc streamZoneSyncMetrics map[string]*prometheus.Desc @@ -563,6 +564,14 @@ func NewNginxPlusCollector(nginxClient *plusclient.NginxClient, namespace string "http_requests_total": newWorkerMetric(namespace, "http_requests_total", "The total number of client requests received by the worker process", constLabels), "http_requests_current": newWorkerMetric(namespace, "http_requests_current", "The current number of client requests that are currently being processed by the worker process", constLabels), }, + slabsMetrics: map[string]*prometheus.Desc{ + "pages_used": newSlabPagesMetric(namespace, "pages_used", "Current number of used memory pages", constLabels), + "pages_free": newSlabPagesMetric(namespace, "pages_free", "Current number of free memory pages", constLabels), + "slots_free": newSlabMetric(namespace, "free", "Current number of free memory slots", constLabels), + "slots_used": newSlabMetric(namespace, "used", "Current number of used memory slots", constLabels), + "slots_reqs": newSlabMetric(namespace, "reqs", "Total number of attempts to allocate memory of specified size", constLabels), + "slots_fails": newSlabMetric(namespace, "fails", "Total number of unsuccessful attempts to allocate memory of specified size", constLabels), + }, } } @@ -616,6 +625,9 @@ func (c *NginxPlusCollector) Describe(ch chan<- *prometheus.Desc) { for _, m := range c.workerMetrics { ch <- m } + for _, m := range c.slabsMetrics { + ch <- m + } } // Collect fetches metrics from NGINX Plus and sends them to the provided channel. @@ -1246,6 +1258,18 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) { ch <- prometheus.MustNewConstMetric(c.workerMetrics["http_requests_total"], prometheus.CounterValue, float64(worker.HTTP.HTTPRequests.Total), workerID, workerPID) ch <- prometheus.MustNewConstMetric(c.workerMetrics["http_requests_current"], prometheus.GaugeValue, float64(worker.HTTP.HTTPRequests.Current), workerID, workerPID) } + + for name, slab := range stats.Slabs { + ch <- prometheus.MustNewConstMetric(c.slabsMetrics["pages_used"], prometheus.GaugeValue, float64(slab.Pages.Used), name) + ch <- prometheus.MustNewConstMetric(c.slabsMetrics["pages_free"], prometheus.GaugeValue, float64(slab.Pages.Free), name) + + for memory, slot := range slab.Slots { + ch <- prometheus.MustNewConstMetric(c.slabsMetrics["slots_free"], prometheus.GaugeValue, float64(slot.Free), name, memory) + ch <- prometheus.MustNewConstMetric(c.slabsMetrics["slots_used"], prometheus.GaugeValue, float64(slot.Used), name, memory) + ch <- prometheus.MustNewConstMetric(c.slabsMetrics["slots_reqs"], prometheus.GaugeValue, float64(slot.Reqs), name, memory) + ch <- prometheus.MustNewConstMetric(c.slabsMetrics["slots_fails"], prometheus.GaugeValue, float64(slot.Fails), name, memory) + } + } } var upstreamServerStates = map[string]float64{ @@ -1331,3 +1355,11 @@ func newCacheZoneMetric(namespace string, metricName string, docString string, v func newWorkerMetric(namespace string, metricName string, docString string, constLabels prometheus.Labels) *prometheus.Desc { return prometheus.NewDesc(prometheus.BuildFQName(namespace, "worker", metricName), docString, []string{"id", "pid"}, constLabels) } + +func newSlabPagesMetric(namespace string, metricName string, docString string, constLabels prometheus.Labels) *prometheus.Desc { + return prometheus.NewDesc(prometheus.BuildFQName(namespace, "slab", metricName), docString, []string{"zone"}, constLabels) +} + +func newSlabMetric(namespace string, metricName string, docString string, constLabels prometheus.Labels) *prometheus.Desc { + return prometheus.NewDesc(prometheus.BuildFQName(namespace, "slab", metricName), docString, []string{"zone", "slot"}, constLabels) +} From f9ec4121e1c13a5fbaac5fda4368076a4c1989c1 Mon Sep 17 00:00:00 2001 From: gudvyach Date: Fri, 23 May 2025 11:40:30 +0300 Subject: [PATCH 2/3] Add slab metrics description to README --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index f087ec4d..c3120604 100644 --- a/README.md +++ b/README.md @@ -432,6 +432,17 @@ Flags: | `nginxplus_worker_http_requests_total` | Counter | The total number of client requests received | `id`, `pid` | | `nginxplus_worker_http_requests_current` | Gauge | The current number of client requests that are currently being processed | `id`, `pid` | +### [Slabs](https://nginx.org/en/docs/http/ngx_http_api_module.html#slabs_) + +| Name | Type | Description | Labels | +| ---------------------------------------- | ------- | ------------------------------------------------------------------------------- | -------------- | +| `nginxplus_slab_fails` | Gauge | Total number of unsuccessful attempts to allocate memory of specified size | `slot`, `zone` | +| `nginxplus_slab_reqs` | Gauge | Total number of attempts to allocate memory of specified size | `slot`, `zone` | +| `nginxplus_slab_free` | Gauge | Current number of free memory slots | `slot`, `zone` | +| `nginxplus_slab_used` | Gauge | Current number of used memory slots | `slot`, `zone` | +| `nginxplus_slab_pages_free` | Gauge | Current number of free memory pages | `zone` | +| `nginxplus_slab_pages_used` | Gauge | Current number of used memory pages | `zone` | + Connect to the `/metrics` page of the running exporter to see the complete list of metrics along with their descriptions. Note: to see server zones related metrics you must configure [status zones](https://nginx.org/en/docs/http/ngx_http_api_module.html#status_zone) and to see upstream related metrics you From 1c93cd9573664320257876a484fa6d8762d92420 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 23 May 2025 08:48:44 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c3120604..eb6b3401 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ NGINX Prometheus exporter makes it possible to monitor NGINX or NGINX Plus using - [Stream Connections Limiting](#stream-connections-limiting) - [Cache](#cache) - [Worker](#worker) + - [Slabs](#slabs) - [Troubleshooting](#troubleshooting) - [Releases](#releases) - [Docker images](#docker-images)