Skip to content

Commit c699f22

Browse files
authored
Add constLabels support via cli arg/env var
1 parent c3dd65c commit c699f22

File tree

8 files changed

+306
-118
lines changed

8 files changed

+306
-118
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ Usage of ./nginx-prometheus-exporter:
8181
Perform SSL certificate verification. The default value can be overwritten by SSL_VERIFY environment variable. (default true)
8282
-nginx.timeout duration
8383
A timeout for scraping metrics from NGINX or NGINX Plus. The default value can be overwritten by TIMEOUT environment variable. (default 5s)
84+
-prometheus.const-labels value
85+
A comma separated list of constant labels that will be used in every metric. Format is label1=value1,label2=value2... The default value can be overwritten by CONST_LABELS environment variable.
8486
-web.listen-address string
8587
An address or unix domain socket path to listen on for web interface and telemetry. The default value can be overwritten by LISTEN_ADDRESS environment variable. (default ":9113")
8688
-web.telemetry-path string

collector/helper.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package collector
22

3-
import "github.com/prometheus/client_golang/prometheus"
3+
import (
4+
"github.com/prometheus/client_golang/prometheus"
5+
)
46

57
const nginxUp = 1
68
const nginxDown = 0
79

8-
func newGlobalMetric(namespace string, metricName string, docString string) *prometheus.Desc {
9-
return prometheus.NewDesc(namespace+"_"+metricName, docString, nil, nil)
10+
func newGlobalMetric(namespace string, metricName string, docString string, constLabels map[string]string) *prometheus.Desc {
11+
return prometheus.NewDesc(namespace+"_"+metricName, docString, nil, constLabels)
1012
}
1113

1214
func newUpMetric(namespace string) prometheus.Gauge {
@@ -16,3 +18,17 @@ func newUpMetric(namespace string) prometheus.Gauge {
1618
Help: "Status of the last metric scrape",
1719
})
1820
}
21+
22+
// MergeLabels merges two maps of labels.
23+
func MergeLabels(a map[string]string, b map[string]string) map[string]string {
24+
c := make(map[string]string)
25+
26+
for k, v := range a {
27+
c[k] = v
28+
}
29+
for k, v := range b {
30+
c[k] = v
31+
}
32+
33+
return c
34+
}

collector/helper_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package collector
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestMergeLabels(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
mapA, mapB, want map[string]string
12+
}{
13+
{
14+
name: "base case",
15+
mapA: map[string]string{"a": "is here"},
16+
mapB: map[string]string{"b": "is here"},
17+
want: map[string]string{"a": "is here", "b": "is here"},
18+
},
19+
{
20+
name: "overwrite key case",
21+
mapA: map[string]string{"a": "is here"},
22+
mapB: map[string]string{"b": "is here", "a": "is now here"},
23+
want: map[string]string{"a": "is now here", "b": "is here"},
24+
},
25+
{
26+
name: "empty maps case",
27+
mapA: nil,
28+
mapB: nil,
29+
want: map[string]string{},
30+
},
31+
}
32+
for _, tt := range tests {
33+
t.Run(tt.name, func(t *testing.T) {
34+
if got := MergeLabels(tt.mapA, tt.mapB); !reflect.DeepEqual(got, tt.want) {
35+
t.Errorf("mergeLabels() = %v, want %v", got, tt.want)
36+
}
37+
})
38+
}
39+
}

collector/nginx.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ type NginxCollector struct {
1717
}
1818

1919
// NewNginxCollector creates an NginxCollector.
20-
func NewNginxCollector(nginxClient *client.NginxClient, namespace string) *NginxCollector {
20+
func NewNginxCollector(nginxClient *client.NginxClient, namespace string, constLabels map[string]string) *NginxCollector {
2121
return &NginxCollector{
2222
nginxClient: nginxClient,
2323
metrics: map[string]*prometheus.Desc{
24-
"connections_active": newGlobalMetric(namespace, "connections_active", "Active client connections"),
25-
"connections_accepted": newGlobalMetric(namespace, "connections_accepted", "Accepted client connections"),
26-
"connections_handled": newGlobalMetric(namespace, "connections_handled", "Handled client connections"),
27-
"connections_reading": newGlobalMetric(namespace, "connections_reading", "Connections where NGINX is reading the request header"),
28-
"connections_writing": newGlobalMetric(namespace, "connections_writing", "Connections where NGINX is writing the response back to the client"),
29-
"connections_waiting": newGlobalMetric(namespace, "connections_waiting", "Idle client connections"),
30-
"http_requests_total": newGlobalMetric(namespace, "http_requests_total", "Total http requests"),
24+
"connections_active": newGlobalMetric(namespace, "connections_active", "Active client connections", constLabels),
25+
"connections_accepted": newGlobalMetric(namespace, "connections_accepted", "Accepted client connections", constLabels),
26+
"connections_handled": newGlobalMetric(namespace, "connections_handled", "Handled client connections", constLabels),
27+
"connections_reading": newGlobalMetric(namespace, "connections_reading", "Connections where NGINX is reading the request header", constLabels),
28+
"connections_writing": newGlobalMetric(namespace, "connections_writing", "Connections where NGINX is writing the response back to the client", constLabels),
29+
"connections_waiting": newGlobalMetric(namespace, "connections_waiting", "Idle client connections", constLabels),
30+
"http_requests_total": newGlobalMetric(namespace, "http_requests_total", "Total http requests", constLabels),
3131
},
3232
upMetric: newUpMetric(namespace),
3333
}

0 commit comments

Comments
 (0)