Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ func init() {

func main() {
var (
showVersion = flag.Bool("version", false, "Print version information.")
listenAddress = flag.String("web.listen-address", ":9237", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
configFile = flag.String("config.file", os.Getenv("CONFIG"), "SQL Exporter configuration file name.")
showVersion = flag.Bool("version", false, "Print version information.")
listenAddress = flag.String("web.listen-address", ":9237", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
configFile = flag.String("config.file", os.Getenv("CONFIG"), "SQL Exporter configuration file name.")
dbConnectivityAsHealthCheck = flag.Bool("db.connectivity-as-healthz", false, "Use database connectivity check as healthz probe")
)

flag.Parse()
Expand Down Expand Up @@ -66,7 +67,45 @@ func main() {

// setup and start webserver
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { http.Error(w, "OK", http.StatusOK) })

if *dbConnectivityAsHealthCheck {
http.HandleFunc("/healthz",
func(w http.ResponseWriter, r *http.Request) {
for _, job := range exporter.jobs {

if job == nil {
continue
}

for _, connection := range job.conns {

if connection == nil {
continue
}

if connection.conn != nil {
if err := connection.conn.Ping(); err != nil {
continue
}
// if we meet at least one successful connection we consider the app healthy
http.Error(w, "OK", http.StatusOK)
return
}

if err := connection.connect(job); err == nil {
// if we meet at least one successful connection we consider the app healthy
http.Error(w, "OK", http.StatusOK)
return
}
}
}
// no successful connections found, fail the /healthz request
http.Error(w, err.Error(), http.StatusInternalServerError)
})
} else {
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { http.Error(w, "OK", http.StatusOK) })
}

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>SQL Exporter</title></head>
Expand Down