Skip to content

Commit

Permalink
Merge pull request #12 from martin-helmich/bugfix/fix-label-mixup
Browse files Browse the repository at this point in the history
Provide stable sorting for label keys+values
  • Loading branch information
martin-helmich authored Aug 9, 2017
2 parents 98997d5 + ca2f136 commit e7bf233
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 16 deletions.
28 changes: 18 additions & 10 deletions config/structs.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package config

import (
"sort"
)

// StartupFlags is a struct containing options that can be passed via the
// command line
type StartupFlags struct {
Expand Down Expand Up @@ -44,22 +48,26 @@ type NamespaceConfig struct {
SourceFiles []string `hcl:"source_files"`
Format string `hcl:"format"`
Labels map[string]string `hcl:"labels"`

OrderedLabelNames []string
OrderedLabelValues []string
}

// LabelNames exports the names of all known additional labels
func (c *NamespaceConfig) LabelNames() []string {
// OrderLabels builds two lists of label keys and values, ordered by label name
func (c *NamespaceConfig) OrderLabels() {
keys := make([]string, 0, len(c.Labels))
values := make([]string, len(c.Labels))

for k := range c.Labels {
keys = append(keys, k)
}
return keys
}

// LabelValues exports the values of all known additional labels
func (c *NamespaceConfig) LabelValues() []string {
values := make([]string, 0, len(c.Labels))
for k := range c.Labels {
values = append(values, c.Labels[k])
sort.Strings(keys)

for i, k := range keys {
values[i] = c.Labels[k]
}
return values

c.OrderedLabelNames = keys
c.OrderedLabelValues = values
}
13 changes: 13 additions & 0 deletions features/custom_labels.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,16 @@ Feature: Config file allows custom labels
172.17.0.1 - - [23/Jun/2016:16:04:20 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
"""
Then the exporter should report value 1 for metric nginx_http_response_count_total{foo="bar",method="GET",status="200"}

Scenario: Labels are added correctly for multiple namespaces
Given a running exporter listening with configuration file "test-configuration-labels-multi.hcl"
When the following HTTP request is logged to "access.log"
"""
172.17.0.1 - - [23/Jun/2016:16:04:20 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
"""
And the following HTTP request is logged to "access-two.log"
"""
172.17.0.1 - - [23/Jun/2016:16:04:20 +0000] "POST / HTTP/1.1" 500 612 "-" "curl/7.29.0" "-"
"""
Then the exporter should report value 1 for metric testone_http_response_count_total{method="GET",status="200",test1="1-1",test2="1-2"}
And the exporter should report value 1 for metric testtwo_http_response_count_total{method="POST",status="500",test1="2-1",test2="2-2"}
9 changes: 7 additions & 2 deletions features/steps/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ def step_impl(context, filename):

@then(u'the exporter should report value {val} for metric {metric}')
def step_impl(context, val, metric):
response = requests.get('http://localhost:4040/metrics')
text = response.text
while True:
try:
response = requests.get('http://localhost:4040/metrics')
text = response.text
break
except request.ConnectionError:
continue

lines = [l.strip("\n") for l in text.split("\n")]
if not "%s %s" % (metric, val) in lines:
Expand Down
49 changes: 49 additions & 0 deletions features/test-configuration-labels-multi.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
port = 4040

namespace "testone" {
source_files = [".behave-sandbox/access.log"]
format = "$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" \"$http_x_forwarded_for\""
labels {
test1 = "1-1"
test2 = "1-2"
}
}

namespace "testtwo" {
source_files = [".behave-sandbox/access-two.log"]
format = "$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" \"$http_x_forwarded_for\""
labels {
test1 = "2-1"
test2 = "2-2"
}
}


namespace "testthree" {
source_files = [".behave-sandbox/access-three.log"]
format = "$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" \"$http_x_forwarded_for\""
labels {
test1 = "3-1"
test2 = "3-2"
}
}


namespace "testfour" {
source_files = [".behave-sandbox/access-four.log"]
format = "$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" \"$http_x_forwarded_for\""
labels {
test1 = "4-1"
test2 = "4-2"
}
}


namespace "testfive" {
source_files = [".behave-sandbox/access-five.log"]
format = "$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" \"$http_x_forwarded_for\""
labels {
test1 = "5-1"
test2 = "5-2"
}
}
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ type Metrics struct {

// Init initializes a metrics struct
func (m *Metrics) Init(cfg *config.NamespaceConfig) {
cfg.OrderLabels()

labels := []string{"method", "status"}
labels = append(labels, cfg.LabelNames()...)
labels = append(labels, cfg.OrderedLabelNames...)

m.countTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: cfg.Name,
Expand Down Expand Up @@ -145,8 +147,8 @@ func main() {
panic(err)
}

go func() {
staticLabelValues := nsCfg.LabelValues()
go func(nsCfg config.NamespaceConfig) {
staticLabelValues := nsCfg.OrderedLabelValues
labelValues := make([]string, len(staticLabelValues)+2)

for i := range staticLabelValues {
Expand Down Expand Up @@ -186,7 +188,7 @@ func main() {
metrics.responseSeconds.WithLabelValues(labelValues...).Observe(responseTime)
}
}
}()
}(nsCfg)
}
}(ns)
}
Expand Down

0 comments on commit e7bf233

Please sign in to comment.