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 1 commit
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
17 changes: 15 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,16 @@ func newUpMetric(namespace string) prometheus.Gauge {
Help: "Status of the last metric scrape",
})
}

func mergeLabels(a map[string]string, b map[string]string) map[string]string {
Dean-Coakley marked this conversation as resolved.
Show resolved Hide resolved
c := make(map[string]string)

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

return c
}
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