Skip to content

Commit

Permalink
Fix panic when setting only_counter for default relabelings (#279)
Browse files Browse the repository at this point in the history
Deduplicate labels used in `Collection.init()` in the same way as in
`processSource`.
Side-Effect: When overriding `method` one needs to manually add the
split/whitelist config from `DefaultRelabelings` in their own config
file.
Resolve #216
  • Loading branch information
e1mo authored Oct 11, 2022
1 parent 100ebdc commit 334a25a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
15 changes: 14 additions & 1 deletion features/issues.feature
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ Feature: Various issues reported in the bug tracker remain solved
Then the exporter should report value 3 for metric http_parse_errors_total{vhost="test"}
And the exporter should report value 2 for metric http_parse_errors_total{vhost="test2"}

Scenario: Issue 216: Cannot set only_counter for status, daemon panics
Given a running exporter listening with configuration file "test-config-issue216.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" "-" 10 10
172.17.0.1 - - [23/Jun/2016:16:04:20 +0000] "POST / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" 10 10
172.17.0.1 - - [23/Jun/2016:16:04:20 +0000] "GET / HTTP/1.1" 500 612 "-" "curl/7.29.0" "-" 10 10
172.17.0.1 - - [23/Jun/2016:16:04:20 +0000] "GET / HTTP/1.1" 418 612 "-" "curl/7.29.0" "-" 10 10
"""
Then the process should be running
And the exporter should report value 1 for metric nginx_http_response_count_total{method="GET",status="200"}
And the exporter should report value 1 for metric nginx_http_response_count_total{method="other",status="200"}
And the exporter should report value 4 for metric nginx_http_response_time_seconds_hist_count

Scenario: Issue 224: Missing float values
Given a running exporter listening with configuration file "test-config-issue217.yaml"
When the following HTTP request is logged to "access.log"
Expand All @@ -101,4 +115,3 @@ Feature: Various issues reported in the bug tracker remain solved
"""
Then the exporter should report value 0 for metric test_parse_errors_total
Then the exporter should report value 4 for metric test_http_upstream_time_seconds_sum{method="GET",status="200"}

7 changes: 7 additions & 0 deletions features/steps/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,10 @@ def step_impl(context, val, metric, endpoint='/metrics'):
if not "%s %s" % (metric, val) in lines:
raise AssertionError(
'expected metric "%s" could not be verified. Actual metrics:\n%s' % (metric, text))


@then("the process should be running")
def step_impl(context):
rc = context.process.poll()
if rc is not None:
raise AssertionError(f"The process has exited with return code {rc}")
24 changes: 24 additions & 0 deletions features/test-config-issue216.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
listen {
port = 4040
}

namespace "nginx" {
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\" $request_time $upstream_response_time"

relabel "status" {
from = "status"
only_counter = true
}
relabel "method" {
from = "request"
only_counter = true
split = 1
whitelist = [ "GET" ]
}
}
26 changes: 6 additions & 20 deletions pkg/metrics/collection_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,22 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

func inLabels(label string, labels []string) bool {
for _, l := range labels {
if label == l {
return true
}
}
return false
}

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

labels := cfg.OrderedLabelNames
counterLabels := labels

for i := range cfg.RelabelConfigs {
if !cfg.RelabelConfigs[i].OnlyCounter {
labels = append(labels, cfg.RelabelConfigs[i].TargetLabel)
}
counterLabels = append(counterLabels, cfg.RelabelConfigs[i].TargetLabel)
}
relabelings := relabeling.NewRelabelings(cfg.RelabelConfigs)
relabelings = append(relabelings, relabeling.DefaultRelabelings...)
relabelings = relabeling.UniqueRelabelings(relabelings)

for _, r := range relabeling.DefaultRelabelings {
if !inLabels(r.TargetLabel, labels) {
for _, r := range relabelings {
if !r.OnlyCounter {
labels = append(labels, r.TargetLabel)
}
if !inLabels(r.TargetLabel, counterLabels) {
counterLabels = append(counterLabels, r.TargetLabel)
}
counterLabels = append(counterLabels, r.TargetLabel)
}

m.CountTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Expand Down

0 comments on commit 334a25a

Please sign in to comment.