Skip to content

Commit

Permalink
fix(prometheus): shutdown error & http error handle
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlongzt committed Oct 19, 2024
1 parent aa24f2a commit 050efc3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func main() {
forkExec()
return
}

logrus.Warn("process reaping disabled, not pid 1")
}
crontabFileName := flag.Args()[0]
Expand Down Expand Up @@ -184,7 +184,7 @@ func main() {

defer func() {
if err := promServerShutdownClosure(); err != nil {
logrus.Fatalf("prometheus http shutdown failed: %s", err.Error())
logrus.Errorf("prometheus http shutdown failed: %s", err.Error())
}
}()
}
Expand Down
17 changes: 13 additions & 4 deletions prometheus_metrics/prommetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package prometheus_metrics

import (
"context"
"errors"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -55,7 +56,7 @@ func NewPrometheusMetrics() PrometheusMetrics {
pm.CronsSuccessCounter = *prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: genMetricName("successful_executions"),
Help: "count of successul cron executions",
Help: "count of successful cron executions",
},
cronLabels,
)
Expand Down Expand Up @@ -140,17 +141,23 @@ func InitHTTPServer(listenAddr string, shutdownContext context.Context) (func()
http.Handle("/metrics", promhttp.Handler())

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
_, err := w.Write([]byte(`<html>
<head><title>Supercronic</title></head>
<body>
<h1>Supercronic</h1>
<p><a href='/metrics'>Metrics</a></p>
</body>
</html>`))
if err != nil {
logrus.Warnf("failed to write response on '/': %s", err.Error())
}
})

http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`OK`))
_, err := w.Write([]byte(`OK`))
if err != nil {
logrus.Warnf("failed to write response on '/health': %s", err.Error())
}
})

shutdownClosure := func() error {
Expand All @@ -164,7 +171,9 @@ func InitHTTPServer(listenAddr string, shutdownContext context.Context) (func()

go func() {
if err := promSrv.Serve(listener); err != nil {
logrus.Fatalf("prometheus http serve failed: %s", err.Error())
if !errors.Is(err, http.ErrServerClosed) {
logrus.Fatalf("prometheus http serve failed: %s", err.Error())
}
}
}()

Expand Down

0 comments on commit 050efc3

Please sign in to comment.