Skip to content

Commit 10075fe

Browse files
author
gudvyach
committed
Add slab metrics in nginx plus collector
1 parent 9237ce6 commit 10075fe

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

collector/nginx_plus.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type NginxPlusCollector struct {
3535
logger *slog.Logger
3636
cacheZoneMetrics map[string]*prometheus.Desc
3737
workerMetrics map[string]*prometheus.Desc
38+
slabsMetrics map[string]*prometheus.Desc
3839
nginxClient *plusclient.NginxClient
3940
streamServerZoneMetrics map[string]*prometheus.Desc
4041
streamZoneSyncMetrics map[string]*prometheus.Desc
@@ -563,6 +564,14 @@ func NewNginxPlusCollector(nginxClient *plusclient.NginxClient, namespace string
563564
"http_requests_total": newWorkerMetric(namespace, "http_requests_total", "The total number of client requests received by the worker process", constLabels),
564565
"http_requests_current": newWorkerMetric(namespace, "http_requests_current", "The current number of client requests that are currently being processed by the worker process", constLabels),
565566
},
567+
slabsMetrics: map[string]*prometheus.Desc{
568+
"pages_used": newSlabPagesMetric(namespace, "pages_used", "Current number of used memory pages", constLabels),
569+
"pages_free": newSlabPagesMetric(namespace, "pages_free", "Current number of free memory pages", constLabels),
570+
"slots_free": newSlabMetric(namespace, "free", "Current number of free memory slots", constLabels),
571+
"slots_used": newSlabMetric(namespace, "used", "Current number of used memory slots", constLabels),
572+
"slots_reqs": newSlabMetric(namespace, "reqs", "Total number of attempts to allocate memory of specified size", constLabels),
573+
"slots_fails": newSlabMetric(namespace, "fails", "Total number of unsuccessful attempts to allocate memory of specified size", constLabels),
574+
},
566575
}
567576
}
568577

@@ -616,6 +625,9 @@ func (c *NginxPlusCollector) Describe(ch chan<- *prometheus.Desc) {
616625
for _, m := range c.workerMetrics {
617626
ch <- m
618627
}
628+
for _, m := range c.slabsMetrics {
629+
ch <- m
630+
}
619631
}
620632

621633
// 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) {
12461258
ch <- prometheus.MustNewConstMetric(c.workerMetrics["http_requests_total"], prometheus.CounterValue, float64(worker.HTTP.HTTPRequests.Total), workerID, workerPID)
12471259
ch <- prometheus.MustNewConstMetric(c.workerMetrics["http_requests_current"], prometheus.GaugeValue, float64(worker.HTTP.HTTPRequests.Current), workerID, workerPID)
12481260
}
1261+
1262+
for name, slab := range stats.Slabs {
1263+
ch <- prometheus.MustNewConstMetric(c.slabsMetrics["pages_used"], prometheus.GaugeValue, float64(slab.Pages.Used), name)
1264+
ch <- prometheus.MustNewConstMetric(c.slabsMetrics["pages_free"], prometheus.GaugeValue, float64(slab.Pages.Free), name)
1265+
1266+
for memory, slot := range slab.Slots {
1267+
ch <- prometheus.MustNewConstMetric(c.slabsMetrics["slots_free"], prometheus.GaugeValue, float64(slot.Free), name, memory)
1268+
ch <- prometheus.MustNewConstMetric(c.slabsMetrics["slots_used"], prometheus.GaugeValue, float64(slot.Used), name, memory)
1269+
ch <- prometheus.MustNewConstMetric(c.slabsMetrics["slots_reqs"], prometheus.GaugeValue, float64(slot.Reqs), name, memory)
1270+
ch <- prometheus.MustNewConstMetric(c.slabsMetrics["slots_fails"], prometheus.GaugeValue, float64(slot.Fails), name, memory)
1271+
}
1272+
}
12491273
}
12501274

12511275
var upstreamServerStates = map[string]float64{
@@ -1331,3 +1355,11 @@ func newCacheZoneMetric(namespace string, metricName string, docString string, v
13311355
func newWorkerMetric(namespace string, metricName string, docString string, constLabels prometheus.Labels) *prometheus.Desc {
13321356
return prometheus.NewDesc(prometheus.BuildFQName(namespace, "worker", metricName), docString, []string{"id", "pid"}, constLabels)
13331357
}
1358+
1359+
func newSlabPagesMetric(namespace string, metricName string, docString string, constLabels prometheus.Labels) *prometheus.Desc {
1360+
return prometheus.NewDesc(prometheus.BuildFQName(namespace, "slab", metricName), docString, []string{"zone"}, constLabels)
1361+
}
1362+
1363+
func newSlabMetric(namespace string, metricName string, docString string, constLabels prometheus.Labels) *prometheus.Desc {
1364+
return prometheus.NewDesc(prometheus.BuildFQName(namespace, "slab", metricName), docString, []string{"zone", "slot"}, constLabels)
1365+
}

0 commit comments

Comments
 (0)