Skip to content

Commit 84966e8

Browse files
Fix custom endpoint, 3rd time's the charm (#117)
1 parent b3b4fab commit 84966e8

File tree

7 files changed

+46
-7
lines changed

7 files changed

+46
-7
lines changed

config/structs.go

+10
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,13 @@ func (c *Config) StabilityWarnings() error {
6969

7070
return nil
7171
}
72+
73+
// MetricsEndpointOrDefault returns the configured metrics endpoint or the
74+
// default value if no configuration was provided.
75+
func (l *ListenConfig) MetricsEndpointOrDefault() string {
76+
if l.MetricsEndpoint == "" {
77+
return "/metrics"
78+
}
79+
80+
return l.MetricsEndpoint
81+
}

features/custom_endpoint.feature

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Feature: Custom endpoint
2+
3+
Scenario: Custom endpoint can be used
4+
Given a running exporter listening with configuration file "test-config-custom-endpoint.yaml"
5+
When the following HTTP request is logged to "access.log"
6+
"""
7+
172.17.0.1 - - [23/Jun/2016:16:04:20 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
8+
"""
9+
Then the exporter should on "/custom" report value 1 for metric nginx_http_response_count_total{method="GET",status="200"}

features/steps/steps.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,20 @@ def step_impl(context, port):
7272

7373

7474
@then(u'the exporter should report value {val} for metric {metric}')
75-
def step_impl(context, val, metric):
75+
@then(u'the exporter should on "{endpoint}" report value {val} for metric {metric}')
76+
def step_impl(context, val, metric, endpoint = '/metrics'):
77+
endpoint = endpoint.lstrip("/")
7678
while True:
7779
try:
78-
response = requests.get('http://localhost:4040/metrics')
80+
url = 'http://localhost:4040/%s' % endpoint
81+
print(url)
82+
response = requests.get(url)
7983
text = response.text
8084
break
8185
except requests.ConnectionError:
8286
continue
8387

8488
lines = [l.strip("\n") for l in text.split("\n")]
8589
if not "%s %s" % (metric, val) in lines:
86-
raise AssertionError('expected metric "%s" could not be verified. Actual metrics:\n%s' % (context.text, text))
90+
raise AssertionError('expected metric "%s" could not be verified. Actual metrics:\n%s' % (metric, text))
91+
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
listen:
2+
port: 4040
3+
metrics_endpoint: /custom
4+
5+
namespaces:
6+
- name: nginx
7+
source:
8+
files:
9+
- .behave-sandbox/access.log
10+
format: "$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" \"$http_x_forwarded_for\""
11+
histogram_buckets: [.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10]

go.mod

-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ require (
1616
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
1717
github.com/kr/pretty v0.2.0 // indirect
1818
github.com/kr/pty v1.1.8 // indirect
19-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
20-
github.com/modern-go/reflect2 v1.0.1 // indirect
2119
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
2220
github.com/pkg/errors v0.9.1 // indirect
2321
github.com/prometheus/client_golang v1.6.0

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi
3737
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
3838
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
3939
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
40+
github.com/golang/protobuf v1.4.0 h1:oOuy+ugB+P/kBdUnG5QaMXSIyJ1q38wWSojYCb3z5VQ=
4041
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
4142
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
4243
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
105106
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
106107
github.com/prometheus/procfs v0.0.8 h1:+fpWZdT24pJBiqJdAwYBjPSk+5YmQzYNPYzQsdzLkt8=
107108
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
109+
github.com/prometheus/procfs v0.0.11 h1:DhHlBtkHWPYi8O2y31JkK0TF+DGM+51OopZjH/Ia5qI=
108110
github.com/prometheus/procfs v0.0.11/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
109111
github.com/satyrius/gonx v1.3.1-0.20180709120835-47c52b995fe5 h1:nIAK+9DnhpSebWeiIqvPr0rqSDC3j9r1I2bp0OJAYVE=
110112
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
151153
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
152154
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 h1:LfCXLvNmTYH9kEmVgqbnsWfruoXZIrh4YBgqVHtDvw0=
153155
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
156+
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f h1:gWF768j/LaZugp8dyS4UwsslYCYz9XgFxvlgsn0n9H8=
154157
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
155158
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
156159
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
165168
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
166169
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
167170
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
171+
google.golang.org/protobuf v1.21.0 h1:qdOKuR/EIArgaWNjetjgTzgVTAZ+S/WXVrq9HW9zimw=
168172
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
169173
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
170174
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

main.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,11 @@ func main() {
199199
}
200200

201201
listenAddr := fmt.Sprintf("%s:%d", cfg.Listen.Address, cfg.Listen.Port)
202-
fmt.Printf("running HTTP server on address %s\n", listenAddr)
202+
endpoint := cfg.Listen.MetricsEndpointOrDefault()
203203

204-
http.Handle(opts.MetricsEndpoint, promhttp.Handler())
204+
fmt.Printf("running HTTP server on address %s, serving metrics at %s\n", listenAddr, endpoint)
205+
206+
http.Handle(endpoint, promhttp.Handler())
205207

206208
if err := http.ListenAndServe(listenAddr, nil); err != nil {
207209
fmt.Printf("error while starting HTTP server: %s", err.Error())

0 commit comments

Comments
 (0)