Skip to content
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

Add constLabels support via cli arg/env var #77

Merged
merged 6 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ Usage of ./nginx-prometheus-exporter:
-nginx.retry-interval duration
An interval between retries to connect to the NGINX stub_status page/NGINX Plus API on start. The default value can be overwritten by NGINX_RETRY_INTERVAL environment variable. (default 5s)
-nginx.scrape-uri string
A URI or unix domain socket path for scraping NGINX or NGINX Plus metrics.
A URI or unix domain socket path for scraping NGINX or NGINX Plus metrics.
For NGINX, the stub_status page must be available through the URI. For NGINX Plus -- the API. The default value can be overwritten by SCRAPE_URI environment variable. (default "http://127.0.0.1:8080/stub_status")
-nginx.ssl-verify
Perform SSL certificate verification. The default value can be overwritten by SSL_VERIFY environment variable. (default true)
-nginx.timeout duration
A timeout for scraping metrics from NGINX or NGINX Plus. The default value can be overwritten by TIMEOUT environment variable. (default 5s)
-prometheus.const-labels value
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.
-web.listen-address string
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")
-web.telemetry-path string
Expand Down
18 changes: 16 additions & 2 deletions collector/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import "github.com/prometheus/client_golang/prometheus"
const nginxUp = 1
const nginxDown = 0

func newGlobalMetric(namespace string, metricName string, docString string) *prometheus.Desc {
return prometheus.NewDesc(namespace+"_"+metricName, docString, nil, nil)
func newGlobalMetric(namespace string, metricName string, docString string, constLabels map[string]string) *prometheus.Desc {
return prometheus.NewDesc(namespace+"_"+metricName, docString, nil, constLabels)
}

func newUpMetric(namespace string) prometheus.Gauge {
Expand All @@ -16,3 +16,17 @@ func newUpMetric(namespace string) prometheus.Gauge {
Help: "Status of the last metric scrape",
})
}

// MergeLabels merges two maps of labels.
func MergeLabels(a map[string]string, b map[string]string) map[string]string {
c := make(map[string]string)

for k, v := range a {
c[k] = v
}
for k, v := range b {
c[k] = v
}

return c
}
39 changes: 39 additions & 0 deletions collector/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package collector

import (
"reflect"
"testing"
)

func TestMergeLabels(t *testing.T) {
tests := []struct {
name string
mapA, mapB, want map[string]string
}{
{
name: "base case",
mapA: map[string]string{"a": "is here"},
mapB: map[string]string{"b": "is here"},
want: map[string]string{"a": "is here", "b": "is here"},
},
{
name: "overwrite key case",
mapA: map[string]string{"a": "is here"},
mapB: map[string]string{"b": "is here", "a": "is now here"},
want: map[string]string{"a": "is now here", "b": "is here"},
},
{
name: "empty maps case",
mapA: nil,
mapB: nil,
want: map[string]string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := MergeLabels(tt.mapA, tt.mapB); !reflect.DeepEqual(got, tt.want) {
t.Errorf("mergeLabels() = %v, want %v", got, tt.want)
}
})
}
}
16 changes: 8 additions & 8 deletions collector/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ type NginxCollector struct {
}

// NewNginxCollector creates an NginxCollector.
func NewNginxCollector(nginxClient *client.NginxClient, namespace string) *NginxCollector {
func NewNginxCollector(nginxClient *client.NginxClient, namespace string, constLabels map[string]string) *NginxCollector {
return &NginxCollector{
nginxClient: nginxClient,
metrics: map[string]*prometheus.Desc{
"connections_active": newGlobalMetric(namespace, "connections_active", "Active client connections"),
"connections_accepted": newGlobalMetric(namespace, "connections_accepted", "Accepted client connections"),
"connections_handled": newGlobalMetric(namespace, "connections_handled", "Handled client connections"),
"connections_reading": newGlobalMetric(namespace, "connections_reading", "Connections where NGINX is reading the request header"),
"connections_writing": newGlobalMetric(namespace, "connections_writing", "Connections where NGINX is writing the response back to the client"),
"connections_waiting": newGlobalMetric(namespace, "connections_waiting", "Idle client connections"),
"http_requests_total": newGlobalMetric(namespace, "http_requests_total", "Total http requests"),
"connections_active": newGlobalMetric(namespace, "connections_active", "Active client connections", constLabels),
"connections_accepted": newGlobalMetric(namespace, "connections_accepted", "Accepted client connections", constLabels),
"connections_handled": newGlobalMetric(namespace, "connections_handled", "Handled client connections", constLabels),
"connections_reading": newGlobalMetric(namespace, "connections_reading", "Connections where NGINX is reading the request header", constLabels),
"connections_writing": newGlobalMetric(namespace, "connections_writing", "Connections where NGINX is writing the response back to the client", constLabels),
"connections_waiting": newGlobalMetric(namespace, "connections_waiting", "Idle client connections", constLabels),
"http_requests_total": newGlobalMetric(namespace, "http_requests_total", "Total http requests", constLabels),
},
upMetric: newUpMetric(namespace),
}
Expand Down
Loading