diff --git a/features/issues.feature b/features/issues.feature index 18cc648..a80234a 100644 --- a/features/issues.feature +++ b/features/issues.feature @@ -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" @@ -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"} - diff --git a/features/steps/steps.py b/features/steps/steps.py index 34a258b..dfa81d9 100644 --- a/features/steps/steps.py +++ b/features/steps/steps.py @@ -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}") diff --git a/features/test-config-issue216.hcl b/features/test-config-issue216.hcl new file mode 100644 index 0000000..fc750d1 --- /dev/null +++ b/features/test-config-issue216.hcl @@ -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" ] + } +} diff --git a/pkg/metrics/collection_init.go b/pkg/metrics/collection_init.go index fa9abe1..edfaa04 100644 --- a/pkg/metrics/collection_init.go +++ b/pkg/metrics/collection_init.go @@ -6,15 +6,6 @@ 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() @@ -22,20 +13,15 @@ func (m *Collection) Init(cfg *config.NamespaceConfig) { 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{