Skip to content

Commit

Permalink
Add metrics from NGINX Plus API version 9
Browse files Browse the repository at this point in the history
Signed-off-by: Haywood Shannon <[email protected]>
  • Loading branch information
haywoodsh committed Nov 13, 2023
1 parent d57f771 commit 0f8b7c9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,17 @@ Name | Type | Description | Labels
`nginxplus_cache_bypass_responses_written` | Counter | Total number of cache bypasses written to cache | `cache`
`nginxplus_cache_bypass_bytes_written` | Counter | Total number of bytes written to cache from cache bypasses | `cache`

#### [Worker](hhttps://nginx.org/en/docs/http/ngx_http_api_module.html#workers)

Name | Type | Description | Labels
----|-------|----|------------|
`nginxplus_worker_connection_accepted` | Counter | The total number of accepted client connections | `id`, `pid` |
`nginxplus_worker_connection_dropped` | Counter | The total number of accepted client connections | `id`, `pid` |
`nginxplus_worker_connection_active` | Gauge | The current number of active client connections | `id`, `pid` |
`nginxplus_worker_connection_idle` | Gauge | The current number of idle client connection | `id`, `pid` |
`nginxplus_worker_http_requests_total` | Counter | The total number of client requests received by the worker process | `id`, `pid` |
`nginxplus_worker_http_requests_current` | Gauge | The current number of client requests that are currently being processed by the worker process | `id`, `pid` |

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_status_module.html#status_zone) and to see upstream related metrics you
Expand Down
74 changes: 72 additions & 2 deletions collector/nginx_plus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package collector

import (
"fmt"
"strconv"
"sync"

"github.com/go-kit/log"
Expand All @@ -26,6 +27,8 @@ type LabelUpdater interface {
DeleteStreamServerZoneLabels(zoneNames []string)
UpdateCacheZoneLabels(cacheLabelValues map[string][]string)
DeleteCacheZoneLabels(cacheNames []string)
UpdateWorkerLabels(workerLabelValues map[string][]string)
DeleteWorkerLabels(workerNames []string)
}

// NginxPlusCollector collects NGINX Plus metrics. It implements prometheus.Collector interface.
Expand Down Expand Up @@ -57,6 +60,8 @@ type NginxPlusCollector struct {
cacheZoneLabels map[string][]string
variableLabelsMutex sync.RWMutex
logger log.Logger
workerLabels map[string][]string
workerMetrics map[string]*prometheus.Desc
}

// UpdateUpstreamServerPeerLabels updates the Upstream Server Peer Labels
Expand Down Expand Up @@ -221,6 +226,30 @@ func (c *NginxPlusCollector) getStreamUpstreamServerPeerLabelValues(peer string)
return c.streamUpstreamServerPeerLabels[peer]
}

// UpdateWorkerLabels updates the Worker Labels
func (c *NginxPlusCollector) UpdateWorkerLabels(workerLabelValues map[string][]string) {
c.variableLabelsMutex.Lock()
for k, v := range workerLabelValues {
c.workerLabels[k] = v
}
c.variableLabelsMutex.Unlock()
}

// DeleteWorkerLabels deletes the Worker Labels
func (c *NginxPlusCollector) DeleteWorkerLabels(id []string) {
c.variableLabelsMutex.Lock()
for _, k := range id {
delete(c.workerLabels, k)
}
c.variableLabelsMutex.Unlock()
}

func (c *NginxPlusCollector) getWorkerLabelValues(id string) []string {
c.variableLabelsMutex.RLock()
defer c.variableLabelsMutex.RUnlock()
return c.workerLabels[id]
}

// VariableLabelNames holds all the variable label names for the different metrics
type VariableLabelNames struct {
UpstreamServerVariableLabelNames []string
Expand All @@ -230,11 +259,12 @@ type VariableLabelNames struct {
StreamServerZoneVariableLabelNames []string
StreamUpstreamServerVariableLabelNames []string
CacheZoneLabelNames []string
WorkerPIDVariableLabelNames []string
}

// NewVariableLabels creates a new struct for VariableNames for the collector
// NewVariableLabelNames NewVariableLabels creates a new struct for VariableNames for the collector
func NewVariableLabelNames(upstreamServerVariableLabelNames []string, serverZoneVariableLabelNames []string, upstreamServerPeerVariableLabelNames []string,
streamUpstreamServerVariableLabelNames []string, streamServerZoneLabels []string, streamUpstreamServerPeerVariableLabelNames []string, cacheZoneLabelNames []string,
streamUpstreamServerVariableLabelNames []string, streamServerZoneLabels []string, streamUpstreamServerPeerVariableLabelNames []string, cacheZoneLabelNames []string, workerPIDVariableLabelNames []string,
) VariableLabelNames {
return VariableLabelNames{
UpstreamServerVariableLabelNames: upstreamServerVariableLabelNames,
Expand All @@ -244,6 +274,7 @@ func NewVariableLabelNames(upstreamServerVariableLabelNames []string, serverZone
StreamServerZoneVariableLabelNames: streamServerZoneLabels,
StreamUpstreamServerPeerVariableLabelNames: streamUpstreamServerPeerVariableLabelNames,
CacheZoneLabelNames: cacheZoneLabelNames,
WorkerPIDVariableLabelNames: workerPIDVariableLabelNames,
}
}

Expand Down Expand Up @@ -543,6 +574,14 @@ func NewNginxPlusCollector(nginxClient *plusclient.NginxClient, namespace string
"bypass_responses_written": newCacheZoneMetric(namespace, "bypass_responses_written", "Total number of cache bypasses written to cache", constLabels),
"bypass_bytes_written": newCacheZoneMetric(namespace, "bypass_bytes_written", "Total number of bytes written to cache from cache bypasses", constLabels),
},
workerMetrics: map[string]*prometheus.Desc{
"connection_accepted": newWorkerMetric(namespace, "connection_accepted", "The total number of accepted client connections", variableLabelNames.WorkerPIDVariableLabelNames, constLabels),
"connection_dropped": newWorkerMetric(namespace, "connection_dropped", "The total number of dropped client connections", variableLabelNames.WorkerPIDVariableLabelNames, constLabels),
"connection_active": newWorkerMetric(namespace, "connection_active", "The current number of active client connections", variableLabelNames.WorkerPIDVariableLabelNames, constLabels),
"connection_idle": newWorkerMetric(namespace, "connection_idle", "The current number of idle client connections", variableLabelNames.WorkerPIDVariableLabelNames, constLabels),
"http_requests_total": newWorkerMetric(namespace, "http_requests_total", "The total number of client requests received by the worker process", variableLabelNames.WorkerPIDVariableLabelNames, constLabels),
"http_requests_current": newWorkerMetric(namespace, "http_requests_current", "The current number of client requests that are currently being processed by the worker process", variableLabelNames.WorkerPIDVariableLabelNames, constLabels),
},
}
}

Expand Down Expand Up @@ -593,6 +632,9 @@ func (c *NginxPlusCollector) Describe(ch chan<- *prometheus.Desc) {
for _, m := range c.cacheZoneMetrics {
ch <- m
}
for _, m := range c.workerMetrics {
ch <- m
}
}

// Collect fetches metrics from NGINX Plus and sends them to the provided channel.
Expand Down Expand Up @@ -1207,6 +1249,28 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(c.cacheZoneMetrics["bypass_responses_written"], prometheus.CounterValue, float64(zone.Bypass.ResponsesWritten), name)
ch <- prometheus.MustNewConstMetric(c.cacheZoneMetrics["bypass_bytes_written"], prometheus.CounterValue, float64(zone.Bypass.BytesWritten), name)
}

for _, worker := range stats.Workers {
labelValues := []string{strconv.FormatInt(int64(worker.ID), 10), strconv.FormatInt(int64(worker.ProcessID), 10)}
varLabelValues := c.getWorkerLabelValues(strconv.FormatInt(int64(worker.ID), 10))

if c.variableLabelNames.WorkerPIDVariableLabelNames != nil && len(varLabelValues) != len(c.variableLabelNames.WorkerPIDVariableLabelNames) {
level.Warn(c.logger).Log("wrong number of labels for worker %v. For labels %v, got values: %v. Empty labels will be used instead",
strconv.FormatInt(int64(worker.ID), 10), c.variableLabelNames.WorkerPIDVariableLabelNames, varLabelValues)
for range c.variableLabelNames.WorkerPIDVariableLabelNames {
labelValues = append(labelValues, "")
}
} else {
labelValues = append(labelValues, varLabelValues...)
}

ch <- prometheus.MustNewConstMetric(c.workerMetrics["connection_accepted"], prometheus.CounterValue, float64(worker.Connections.Accepted), labelValues...)
ch <- prometheus.MustNewConstMetric(c.workerMetrics["connection_dropped"], prometheus.CounterValue, float64(worker.Connections.Dropped), labelValues...)
ch <- prometheus.MustNewConstMetric(c.workerMetrics["connection_active"], prometheus.GaugeValue, float64(worker.Connections.Active), labelValues...)
ch <- prometheus.MustNewConstMetric(c.workerMetrics["connection_idle"], prometheus.GaugeValue, float64(worker.Connections.Idle), labelValues...)
ch <- prometheus.MustNewConstMetric(c.workerMetrics["http_requests_total"], prometheus.CounterValue, float64(worker.HTTP.HTTPRequests.Total), labelValues...)
ch <- prometheus.MustNewConstMetric(c.workerMetrics["http_requests_current"], prometheus.GaugeValue, float64(worker.HTTP.HTTPRequests.Current), labelValues...)
}
}

var upstreamServerStates = map[string]float64{
Expand Down Expand Up @@ -1281,3 +1345,9 @@ func newStreamLimitConnectionMetric(namespace string, metricName string, docStri
func newCacheZoneMetric(namespace string, metricName string, docString string, constLabels prometheus.Labels) *prometheus.Desc {
return prometheus.NewDesc(prometheus.BuildFQName(namespace, "cache", metricName), docString, []string{"zone"}, constLabels)
}

func newWorkerMetric(namespace string, metricName string, docString string, variableLabelNames []string, constLabels prometheus.Labels) *prometheus.Desc {
labels := []string{"id", "pid"}
labels = append(labels, variableLabelNames...)
return prometheus.NewDesc(prometheus.BuildFQName(namespace, "worker", metricName), docString, labels, constLabels)
}

0 comments on commit 0f8b7c9

Please sign in to comment.