Skip to content

Commit

Permalink
Add constLabels support via cli arg/env var
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean-Coakley committed Jan 10, 2020
1 parent c3dd65c commit c699f22
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 118 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -81,6 +81,8 @@ Usage of ./nginx-prometheus-exporter:
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
22 changes: 19 additions & 3 deletions collector/helper.go
@@ -1,12 +1,14 @@
package collector

import "github.com/prometheus/client_golang/prometheus"
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 +18,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
@@ -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
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

0 comments on commit c699f22

Please sign in to comment.