From 84966e833d38523bea38a711349c1ebec879910a Mon Sep 17 00:00:00 2001 From: Martin Helmich Date: Mon, 4 May 2020 12:51:09 +0200 Subject: [PATCH] Fix custom endpoint, 3rd time's the charm (#117) --- config/structs.go | 10 ++++++++++ features/custom_endpoint.feature | 9 +++++++++ features/steps/steps.py | 11 ++++++++--- features/test-config-custom-endpoint.yaml | 11 +++++++++++ go.mod | 2 -- go.sum | 4 ++++ main.go | 6 ++++-- 7 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 features/custom_endpoint.feature create mode 100644 features/test-config-custom-endpoint.yaml diff --git a/config/structs.go b/config/structs.go index 1f78407..3e6eede 100644 --- a/config/structs.go +++ b/config/structs.go @@ -69,3 +69,13 @@ func (c *Config) StabilityWarnings() error { return nil } + +// MetricsEndpointOrDefault returns the configured metrics endpoint or the +// default value if no configuration was provided. +func (l *ListenConfig) MetricsEndpointOrDefault() string { + if l.MetricsEndpoint == "" { + return "/metrics" + } + + return l.MetricsEndpoint +} \ No newline at end of file diff --git a/features/custom_endpoint.feature b/features/custom_endpoint.feature new file mode 100644 index 0000000..c9239e8 --- /dev/null +++ b/features/custom_endpoint.feature @@ -0,0 +1,9 @@ +Feature: Custom endpoint + + Scenario: Custom endpoint can be used + Given a running exporter listening with configuration file "test-config-custom-endpoint.yaml" + 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" "-" + """ + Then the exporter should on "/custom" report value 1 for metric nginx_http_response_count_total{method="GET",status="200"} diff --git a/features/steps/steps.py b/features/steps/steps.py index 51b8990..91b8271 100644 --- a/features/steps/steps.py +++ b/features/steps/steps.py @@ -72,10 +72,14 @@ def step_impl(context, port): @then(u'the exporter should report value {val} for metric {metric}') -def step_impl(context, val, metric): +@then(u'the exporter should on "{endpoint}" report value {val} for metric {metric}') +def step_impl(context, val, metric, endpoint = '/metrics'): + endpoint = endpoint.lstrip("/") while True: try: - response = requests.get('http://localhost:4040/metrics') + url = 'http://localhost:4040/%s' % endpoint + print(url) + response = requests.get(url) text = response.text break except requests.ConnectionError: @@ -83,4 +87,5 @@ def step_impl(context, val, metric): lines = [l.strip("\n") for l in text.split("\n")] if not "%s %s" % (metric, val) in lines: - raise AssertionError('expected metric "%s" could not be verified. Actual metrics:\n%s' % (context.text, text)) + raise AssertionError('expected metric "%s" could not be verified. Actual metrics:\n%s' % (metric, text)) + diff --git a/features/test-config-custom-endpoint.yaml b/features/test-config-custom-endpoint.yaml new file mode 100644 index 0000000..ef3e8e9 --- /dev/null +++ b/features/test-config-custom-endpoint.yaml @@ -0,0 +1,11 @@ +listen: + port: 4040 + metrics_endpoint: /custom + +namespaces: + - name: 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\"" + histogram_buckets: [.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10] diff --git a/go.mod b/go.mod index b84ab2f..5ace85c 100644 --- a/go.mod +++ b/go.mod @@ -16,8 +16,6 @@ require ( github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect github.com/kr/pretty v0.2.0 // indirect github.com/kr/pty v1.1.8 // indirect - github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect - github.com/modern-go/reflect2 v1.0.1 // indirect github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.6.0 diff --git a/go.sum b/go.sum index 6007c61..71976f8 100644 --- a/go.sum +++ b/go.sum @@ -37,6 +37,7 @@ github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0 h1:oOuy+ugB+P/kBdUnG5QaMXSIyJ1q38wWSojYCb3z5VQ= github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= @@ -105,6 +106,7 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= github.com/prometheus/procfs v0.0.8 h1:+fpWZdT24pJBiqJdAwYBjPSk+5YmQzYNPYzQsdzLkt8= github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A= +github.com/prometheus/procfs v0.0.11 h1:DhHlBtkHWPYi8O2y31JkK0TF+DGM+51OopZjH/Ia5qI= github.com/prometheus/procfs v0.0.11/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/satyrius/gonx v1.3.1-0.20180709120835-47c52b995fe5 h1:nIAK+9DnhpSebWeiIqvPr0rqSDC3j9r1I2bp0OJAYVE= github.com/satyrius/gonx v1.3.1-0.20180709120835-47c52b995fe5/go.mod h1:+r8KNe5d2tjkZU+DfhERo0G6KxkGih+1qYF6tqLHwvk= @@ -151,6 +153,7 @@ golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 h1:LfCXLvNmTYH9kEmVgqbnsWfruoXZIrh4YBgqVHtDvw0= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f h1:gWF768j/LaZugp8dyS4UwsslYCYz9XgFxvlgsn0n9H8= golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -165,6 +168,7 @@ google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLY google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0 h1:qdOKuR/EIArgaWNjetjgTzgVTAZ+S/WXVrq9HW9zimw= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/main.go b/main.go index 033e2cb..b8de5bc 100644 --- a/main.go +++ b/main.go @@ -199,9 +199,11 @@ func main() { } listenAddr := fmt.Sprintf("%s:%d", cfg.Listen.Address, cfg.Listen.Port) - fmt.Printf("running HTTP server on address %s\n", listenAddr) + endpoint := cfg.Listen.MetricsEndpointOrDefault() - http.Handle(opts.MetricsEndpoint, promhttp.Handler()) + fmt.Printf("running HTTP server on address %s, serving metrics at %s\n", listenAddr, endpoint) + + http.Handle(endpoint, promhttp.Handler()) if err := http.ListenAndServe(listenAddr, nil); err != nil { fmt.Printf("error while starting HTTP server: %s", err.Error())