Skip to content

Add slab metrics #1071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -432,6 +433,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
Expand Down
32 changes: 32 additions & 0 deletions collector/nginx_plus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
},
}
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
}