Skip to content

Commit 2358dcc

Browse files
authored
Remove unnecessary variable labels for workers (#777)
1 parent 4fca542 commit 2358dcc

File tree

3 files changed

+30
-72
lines changed

3 files changed

+30
-72
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ Flags:
300300
| `nginxplus_upstream_server_ssl_handshakes` | Counter | Successful SSL handshakes | `server`, `upstream` |
301301
| `nginxplus_upstream_server_ssl_handshakes_failed` | Counter | Failed SSL handshakes | `server`, `upstream` |
302302
| `nginxplus_upstream_server_ssl_session_reuses` | Counter | Session reuses during SSL handshake | `server`, `upstream` |
303-
| `nginxplus_upstream_keepalive` | Gauge | Idle keepalive connections | `upstream` |
303+
| `nginxplus_upstream_keepalive` | Gauge | Idle keepalive connections | `upstream` |
304304
| `nginxplus_upstream_zombies` | Gauge | Servers removed from the group but still processing active client requests | `upstream` |
305305

306306
#### [Stream Upstreams](https://nginx.org/en/docs/http/ngx_http_api_module.html#def_nginx_stream_upstream)
@@ -394,7 +394,7 @@ Flags:
394394
| `nginxplus_stream_limit_connection_rejected` | Counter | Total number of connections that were rejected | `zone` |
395395
| `nginxplus_stream_limit_connection_rejected_dry_run` | Counter | Total number of connections accounted as rejected in the dry run mode | `zone` |
396396

397-
#### [Cache](https://nginx.org/en/docs/http/ngx_http_api_module.html#http_caches_http_cache_zone_name)
397+
#### [Cache](https://nginx.org/en/docs/http/ngx_http_api_module.html#def_nginx_http_cache)
398398

399399
| Name | Type | Description | Labels |
400400
| ------------------------------------------- | ------- | ----------------------------------------------------------------------- | ------- |
@@ -420,16 +420,16 @@ Flags:
420420
| `nginxplus_cache_bypass_responses_written` | Counter | Total number of cache bypasses written to cache | `cache` |
421421
| `nginxplus_cache_bypass_bytes_written` | Counter | Total number of bytes written to cache from cache bypasses | `cache` |
422422

423-
#### [Worker](hhttps://nginx.org/en/docs/http/ngx_http_api_module.html#workers)
423+
#### [Worker](https://nginx.org/en/docs/http/ngx_http_api_module.html#def_nginx_worker)
424424

425-
| Name | Type | Description | Labels |
426-
| ---------------------------------------- | ------- | ---------------------------------------------------------------------------------------------- | ----------- |
427-
| `nginxplus_worker_connection_accepted` | Counter | The total number of accepted client connections | `id`, `pid` |
428-
| `nginxplus_worker_connection_dropped` | Counter | The total number of accepted client connections | `id`, `pid` |
429-
| `nginxplus_worker_connection_active` | Gauge | The current number of active client connections | `id`, `pid` |
430-
| `nginxplus_worker_connection_idle` | Gauge | The current number of idle client connection | `id`, `pid` |
431-
| `nginxplus_worker_http_requests_total` | Counter | The total number of client requests received by the worker process | `id`, `pid` |
432-
| `nginxplus_worker_http_requests_current` | Gauge | The current number of client requests that are currently being processed by the worker process | `id`, `pid` |
425+
| Name | Type | Description | Labels |
426+
| ---------------------------------------- | ------- | ------------------------------------------------------------------------ | ----------- |
427+
| `nginxplus_worker_connection_accepted` | Counter | The total number of accepted client connections | `id`, `pid` |
428+
| `nginxplus_worker_connection_dropped` | Counter | The total number of dropped client connections | `id`, `pid` |
429+
| `nginxplus_worker_connection_active` | Gauge | The current number of active client connections | `id`, `pid` |
430+
| `nginxplus_worker_connection_idle` | Gauge | The current number of idle client connection | `id`, `pid` |
431+
| `nginxplus_worker_http_requests_total` | Counter | The total number of client requests received | `id`, `pid` |
432+
| `nginxplus_worker_http_requests_current` | Gauge | The current number of client requests that are currently being processed | `id`, `pid` |
433433

434434
Connect to the `/metrics` page of the running exporter to see the complete list of metrics along with their
435435
descriptions. Note: to see server zones related metrics you must configure [status

collector/nginx_plus.go

Lines changed: 18 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ type LabelUpdater interface {
2727
DeleteStreamServerZoneLabels(zoneNames []string)
2828
UpdateCacheZoneLabels(cacheLabelValues map[string][]string)
2929
DeleteCacheZoneLabels(cacheNames []string)
30-
UpdateWorkerLabels(workerLabelValues map[string][]string)
31-
DeleteWorkerLabels(workerNames []string)
3230
}
3331

3432
// NginxPlusCollector collects NGINX Plus metrics. It implements prometheus.Collector interface.
@@ -56,7 +54,6 @@ type NginxPlusCollector struct {
5654
serverZoneLabels map[string][]string
5755
streamServerZoneLabels map[string][]string
5856
upstreamServerPeerLabels map[string][]string
59-
workerLabels map[string][]string
6057
cacheZoneLabels map[string][]string
6158
totalMetrics map[string]*prometheus.Desc
6259
variableLabelNames VariableLabelNames
@@ -226,30 +223,6 @@ func (c *NginxPlusCollector) getStreamUpstreamServerPeerLabelValues(peer string)
226223
return c.streamUpstreamServerPeerLabels[peer]
227224
}
228225

229-
// UpdateWorkerLabels updates the Worker Labels.
230-
func (c *NginxPlusCollector) UpdateWorkerLabels(workerLabelValues map[string][]string) {
231-
c.variableLabelsMutex.Lock()
232-
for k, v := range workerLabelValues {
233-
c.workerLabels[k] = v
234-
}
235-
c.variableLabelsMutex.Unlock()
236-
}
237-
238-
// DeleteWorkerLabels deletes the Worker Labels.
239-
func (c *NginxPlusCollector) DeleteWorkerLabels(id []string) {
240-
c.variableLabelsMutex.Lock()
241-
for _, k := range id {
242-
delete(c.workerLabels, k)
243-
}
244-
c.variableLabelsMutex.Unlock()
245-
}
246-
247-
func (c *NginxPlusCollector) getWorkerLabelValues(id string) []string {
248-
c.variableLabelsMutex.RLock()
249-
defer c.variableLabelsMutex.RUnlock()
250-
return c.workerLabels[id]
251-
}
252-
253226
// VariableLabelNames holds all the variable label names for the different metrics.
254227
type VariableLabelNames struct {
255228
UpstreamServerVariableLabelNames []string
@@ -259,12 +232,11 @@ type VariableLabelNames struct {
259232
StreamServerZoneVariableLabelNames []string
260233
StreamUpstreamServerVariableLabelNames []string
261234
CacheZoneLabelNames []string
262-
WorkerPIDVariableLabelNames []string
263235
}
264236

265237
// NewVariableLabelNames NewVariableLabels creates a new struct for VariableNames for the collector.
266238
func NewVariableLabelNames(upstreamServerVariableLabelNames []string, serverZoneVariableLabelNames []string, upstreamServerPeerVariableLabelNames []string,
267-
streamUpstreamServerVariableLabelNames []string, streamServerZoneLabels []string, streamUpstreamServerPeerVariableLabelNames []string, cacheZoneLabelNames []string, workerPIDVariableLabelNames []string,
239+
streamUpstreamServerVariableLabelNames []string, streamServerZoneLabels []string, streamUpstreamServerPeerVariableLabelNames []string, cacheZoneLabelNames []string,
268240
) VariableLabelNames {
269241
return VariableLabelNames{
270242
UpstreamServerVariableLabelNames: upstreamServerVariableLabelNames,
@@ -274,7 +246,6 @@ func NewVariableLabelNames(upstreamServerVariableLabelNames []string, serverZone
274246
StreamServerZoneVariableLabelNames: streamServerZoneLabels,
275247
StreamUpstreamServerPeerVariableLabelNames: streamUpstreamServerPeerVariableLabelNames,
276248
CacheZoneLabelNames: cacheZoneLabelNames,
277-
WorkerPIDVariableLabelNames: workerPIDVariableLabelNames,
278249
}
279250
}
280251

@@ -575,12 +546,12 @@ func NewNginxPlusCollector(nginxClient *plusclient.NginxClient, namespace string
575546
"bypass_bytes_written": newCacheZoneMetric(namespace, "bypass_bytes_written", "Total number of bytes written to cache from cache bypasses", constLabels),
576547
},
577548
workerMetrics: map[string]*prometheus.Desc{
578-
"connection_accepted": newWorkerMetric(namespace, "connection_accepted", "The total number of accepted client connections", variableLabelNames.WorkerPIDVariableLabelNames, constLabels),
579-
"connection_dropped": newWorkerMetric(namespace, "connection_dropped", "The total number of dropped client connections", variableLabelNames.WorkerPIDVariableLabelNames, constLabels),
580-
"connection_active": newWorkerMetric(namespace, "connection_active", "The current number of active client connections", variableLabelNames.WorkerPIDVariableLabelNames, constLabels),
581-
"connection_idle": newWorkerMetric(namespace, "connection_idle", "The current number of idle client connections", variableLabelNames.WorkerPIDVariableLabelNames, constLabels),
582-
"http_requests_total": newWorkerMetric(namespace, "http_requests_total", "The total number of client requests received by the worker process", variableLabelNames.WorkerPIDVariableLabelNames, constLabels),
583-
"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),
549+
"connection_accepted": newWorkerMetric(namespace, "connection_accepted", "The total number of accepted client connections", constLabels),
550+
"connection_dropped": newWorkerMetric(namespace, "connection_dropped", "The total number of dropped client connections", constLabels),
551+
"connection_active": newWorkerMetric(namespace, "connection_active", "The current number of active client connections", constLabels),
552+
"connection_idle": newWorkerMetric(namespace, "connection_idle", "The current number of idle client connections", constLabels),
553+
"http_requests_total": newWorkerMetric(namespace, "http_requests_total", "The total number of client requests received by the worker process", constLabels),
554+
"http_requests_current": newWorkerMetric(namespace, "http_requests_current", "The current number of client requests that are currently being processed by the worker process", constLabels),
584555
},
585556
}
586557
}
@@ -1249,26 +1220,15 @@ func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
12491220
ch <- prometheus.MustNewConstMetric(c.cacheZoneMetrics["bypass_bytes_written"], prometheus.CounterValue, float64(zone.Bypass.BytesWritten), name)
12501221
}
12511222

1252-
for _, worker := range stats.Workers {
1253-
labelValues := []string{strconv.FormatInt(int64(worker.ID), 10), strconv.FormatInt(int64(worker.ProcessID), 10)}
1254-
varLabelValues := c.getWorkerLabelValues(strconv.FormatInt(int64(worker.ID), 10))
1255-
1256-
if c.variableLabelNames.WorkerPIDVariableLabelNames != nil && len(varLabelValues) != len(c.variableLabelNames.WorkerPIDVariableLabelNames) {
1257-
level.Debug(c.logger).Log("wrong number of labels for worker %v. For labels %v, got values: %v. Empty labels will be used instead",
1258-
strconv.FormatInt(int64(worker.ID), 10), c.variableLabelNames.WorkerPIDVariableLabelNames, varLabelValues)
1259-
for range c.variableLabelNames.WorkerPIDVariableLabelNames {
1260-
labelValues = append(labelValues, "")
1261-
}
1262-
} else {
1263-
labelValues = append(labelValues, varLabelValues...)
1264-
}
1265-
1266-
ch <- prometheus.MustNewConstMetric(c.workerMetrics["connection_accepted"], prometheus.CounterValue, float64(worker.Connections.Accepted), labelValues...)
1267-
ch <- prometheus.MustNewConstMetric(c.workerMetrics["connection_dropped"], prometheus.CounterValue, float64(worker.Connections.Dropped), labelValues...)
1268-
ch <- prometheus.MustNewConstMetric(c.workerMetrics["connection_active"], prometheus.GaugeValue, float64(worker.Connections.Active), labelValues...)
1269-
ch <- prometheus.MustNewConstMetric(c.workerMetrics["connection_idle"], prometheus.GaugeValue, float64(worker.Connections.Idle), labelValues...)
1270-
ch <- prometheus.MustNewConstMetric(c.workerMetrics["http_requests_total"], prometheus.CounterValue, float64(worker.HTTP.HTTPRequests.Total), labelValues...)
1271-
ch <- prometheus.MustNewConstMetric(c.workerMetrics["http_requests_current"], prometheus.GaugeValue, float64(worker.HTTP.HTTPRequests.Current), labelValues...)
1223+
for id, worker := range stats.Workers {
1224+
workerID := strconv.FormatInt(int64(id), 10)
1225+
workerPID := strconv.FormatUint(worker.ProcessID, 10)
1226+
ch <- prometheus.MustNewConstMetric(c.workerMetrics["connection_accepted"], prometheus.CounterValue, float64(worker.Connections.Accepted), workerID, workerPID)
1227+
ch <- prometheus.MustNewConstMetric(c.workerMetrics["connection_dropped"], prometheus.CounterValue, float64(worker.Connections.Dropped), workerID, workerPID)
1228+
ch <- prometheus.MustNewConstMetric(c.workerMetrics["connection_active"], prometheus.GaugeValue, float64(worker.Connections.Active), workerID, workerPID)
1229+
ch <- prometheus.MustNewConstMetric(c.workerMetrics["connection_idle"], prometheus.GaugeValue, float64(worker.Connections.Idle), workerID, workerPID)
1230+
ch <- prometheus.MustNewConstMetric(c.workerMetrics["http_requests_total"], prometheus.CounterValue, float64(worker.HTTP.HTTPRequests.Total), workerID, workerPID)
1231+
ch <- prometheus.MustNewConstMetric(c.workerMetrics["http_requests_current"], prometheus.GaugeValue, float64(worker.HTTP.HTTPRequests.Current), workerID, workerPID)
12721232
}
12731233
}
12741234

@@ -1345,8 +1305,6 @@ func newCacheZoneMetric(namespace string, metricName string, docString string, c
13451305
return prometheus.NewDesc(prometheus.BuildFQName(namespace, "cache", metricName), docString, []string{"zone"}, constLabels)
13461306
}
13471307

1348-
func newWorkerMetric(namespace string, metricName string, docString string, variableLabelNames []string, constLabels prometheus.Labels) *prometheus.Desc {
1349-
labels := []string{"id", "pid"}
1350-
labels = append(labels, variableLabelNames...)
1351-
return prometheus.NewDesc(prometheus.BuildFQName(namespace, "worker", metricName), docString, labels, constLabels)
1308+
func newWorkerMetric(namespace string, metricName string, docString string, constLabels prometheus.Labels) *prometheus.Desc {
1309+
return prometheus.NewDesc(prometheus.BuildFQName(namespace, "worker", metricName), docString, []string{"id", "pid"}, constLabels)
13521310
}

exporter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func registerCollector(logger log.Logger, transport *http.Transport,
253253
level.Error(logger).Log("msg", "Could not create Nginx Plus Client", "error", err.Error())
254254
os.Exit(1)
255255
}
256-
variableLabelNames := collector.NewVariableLabelNames(nil, nil, nil, nil, nil, nil, nil, nil)
256+
variableLabelNames := collector.NewVariableLabelNames(nil, nil, nil, nil, nil, nil, nil)
257257
prometheus.MustRegister(collector.NewNginxPlusCollector(plusClient, "nginxplus", variableLabelNames, labels, logger))
258258
} else {
259259
ossClient := client.NewNginxClient(httpClient, addr)

0 commit comments

Comments
 (0)