Skip to content

Panic on metrics request when config file is empty #183

@hhromic

Description

@hhromic

Describe the bug
When the config file is empty, the application starts normally but then panics on a request to /metrics:

ts=2023-01-19T12:20:27.778Z caller=klog.go:108 level=warn func=Warningf msg="Starting SQL exporter (version=0.9.2, branch=HEAD, revision=e1a5775cca58bf1110516baebc6fe7854d18279b) (go=go1.19.4, platform=linux/amd64, user=root@46fa4bc8dec6, date=20221222-15:10:06)"
ts=2023-01-19T12:20:27.778Z caller=klog.go:84 level=debug func=Infof msg="Loading configuration from sql-exporter.yaml"
ts=2023-01-19T12:20:27.778Z caller=klog.go:96 level=warn func=Warning msg="Listening on :9399"
ts=2023-01-19T12:20:27.779Z caller=tls_config.go:232 level=info msg="Listening on" address=[::]:9399
ts=2023-01-19T12:20:27.779Z caller=tls_config.go:235 level=info msg="TLS is disabled." http2=false address=[::]:9399

// A request to `/metrics` is done here

2023/01/19 12:20:36 http: panic serving 172.17.0.1:43220: runtime error: invalid memory address or nil pointer dereference
goroutine 30 [running]:
net/http.(*conn).serve.func1()
        /usr/local/go/src/net/http/server.go:1850 +0xbf
panic({0x10e49a0, 0x1c74650})
        /usr/local/go/src/runtime/panic.go:890 +0x262
main.contextFor(0xc0004d8100, {0x1467240, 0xc0004b58c0})
        /app/cmd/sql_exporter/promhttp.go:77 +0x3c
main.ExporterHandlerFor.func1({0x7f2ebd6f93b8, 0xc0004b26e0}, 0xc0004d8100)
        /app/cmd/sql_exporter/promhttp.go:32 +0x72
net/http.HandlerFunc.ServeHTTP(0x11208a0?, {0x7f2ebd6f93b8?, 0xc0004b26e0?}, 0xff4bb8?)
        /usr/local/go/src/net/http/server.go:2109 +0x2f
github.com/prometheus/client_golang/prometheus/promhttp.InstrumentHandlerInFlight.func1({0x7f2ebd6f93b8, 0xc0004b26e0}, 0x1466100?)
        /go/pkg/mod/github.com/prometheus/client_golang@v1.14.0/prometheus/promhttp/instrument_server.go:60 +0xd4
net/http.HandlerFunc.ServeHTTP(0x1466188?, {0x7f2ebd6f93b8?, 0xc0004b26e0?}, 0x0?)
        /usr/local/go/src/net/http/server.go:2109 +0x2f
github.com/prometheus/client_golang/prometheus/promhttp.InstrumentHandlerCounter.func1({0x1466188?, 0xc000682000?}, 0xc0004d8100)
        /go/pkg/mod/github.com/prometheus/client_golang@v1.14.0/prometheus/promhttp/instrument_server.go:146 +0xb8
net/http.HandlerFunc.ServeHTTP(0xc00012daf0?, {0x1466188?, 0xc000682000?}, 0x0?)
        /usr/local/go/src/net/http/server.go:2109 +0x2f
net/http.(*ServeMux).ServeHTTP(0x0?, {0x1466188, 0xc000682000}, 0xc0004d8100)
        /usr/local/go/src/net/http/server.go:2487 +0x149
net/http.serverHandler.ServeHTTP({0xc0004b5fb0?}, {0x1466188, 0xc000682000}, 0xc0004d8100)
        /usr/local/go/src/net/http/server.go:2947 +0x30c
net/http.(*conn).serve(0xc0004c4b40, {0x1466da8, 0xc0004b5ec0})
        /usr/local/go/src/net/http/server.go:1991 +0x607
created by net/http.(*Server).Serve
        /usr/local/go/src/net/http/server.go:3102 +0x4db

To Reproduce
Steps to reproduce the behavior:

  • Start the exporter using a blank/empty config file
  • Do a request to /metrics
  • See panic

Expected behavior
The application should not start with a blank/empty config file as one of jobs or target is required.

Configuration
Just an empty file is sufficient to trigger this panic.

Additional context
It can be argued that a blank/empty config file is not a use-case, but the application should not panic regardless. A (mistakingly) invalid config file could be a very possible situation and a proper error messages is better than a panic.

Probably is related to #182 in the way the YAML unmarshalling is performed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions