Vonng / pg_exporter

Advanced PostgreSQL & Pgbouncer Metrics Exporter for Prometheus
https://pigsty.io
Apache License 2.0
180 stars 45 forks source link

Crash when address already bound #45

Closed ringerc closed 5 months ago

ringerc commented 5 months ago

Instead of a graceful exit the scraper panics with a segfault if the listening address is already bound

level=error timestamp=2024-06-12T01:21:25.773821082Z caller=utils.go:76 msg="http server failed: listen tcp :9630: bind: address already in use"
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x88f2d5]

goroutine 1 [running]:
database/sql.(*DB).Close(0x0)
    /home/craig/apps/go-1.22/src/database/sql/sql.go:910 +0x35
github.com/Vonng/pg_exporter/exporter.(*Exporter).Close(0xc000228000)
    /home/craig/projects/EDB/pg_exporter/exporter/exporter.go:209 +0x188
github.com/Vonng/pg_exporter/exporter.Run()
    /home/craig/projects/EDB/pg_exporter/exporter/main.go:219 +0xd17
main.main()
    /home/craig/projects/EDB/pg_exporter/main.go:22 +0xf
Vonng commented 5 months ago

The usual exit log would be like:

$ pg_exporter --web.listen-address=:9630 --url='postgres://dbuser_monitor:DBUser.Monitor@:5432/postgres?sslmode=disable&host=/var/run/postgresql'
level=info timestamp=2024-06-15T05:55:26.864156054Z caller=utils.go:56 msg="retrieve target url postgres://dbuser_monitor:xxxxx@:5432/postgres?sslmode=disable&host=/var/run/postgresql from command line"
level=info timestamp=2024-06-15T05:55:26.86419695Z caller=utils.go:56 msg="fallback on default config path: /etc/pg_exporter.yml"
level=info timestamp=2024-06-15T05:55:26.881175487Z caller=utils.go:56 msg="server [postgres] version changed: from [0] to [160002]"
level=info timestamp=2024-06-15T05:55:26.882476185Z caller=utils.go:56 msg="pg_exporter for postgres://dbuser_monitor:xxxxx@:5432/postgres?sslmode=disable&host=/var/run/postgresql start, listen on http://:9630/metrics"
level=error timestamp=2024-06-15T05:55:26.882562236Z caller=utils.go:76 msg="http server failed: listen tcp :9630: bind: address already in use"
level=info timestamp=2024-06-15T05:55:26.882602071Z caller=utils.go:56 msg="pg exporter closed"

This error seems related to the database/sql driver Close connection part. Could you add some more context info about this error?