From 1953059386daf0344ce50d40c8475d3548d28002 Mon Sep 17 00:00:00 2001 From: chen-jan Date: Mon, 11 Oct 2021 02:29:30 +0000 Subject: [PATCH] Log shutdown gracefully (cherry picked from commit da020715f9fad2e7100ab9cc51dd433e56c2f1c6) --- 0001-Log-shutdown-gracefully-428.patch | 85 ++++++++++++++++++++++++++ pushgateway.spec | 6 +- 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 0001-Log-shutdown-gracefully-428.patch diff --git a/0001-Log-shutdown-gracefully-428.patch b/0001-Log-shutdown-gracefully-428.patch new file mode 100644 index 0000000..fc51bdf --- /dev/null +++ b/0001-Log-shutdown-gracefully-428.patch @@ -0,0 +1,85 @@ +From 92c8d1e028bc6a102bb578e03ba19776bff379ed Mon Sep 17 00:00:00 2001 +From: marie +Date: Thu, 7 Oct 2021 06:10:11 -0400 +Subject: [PATCH] Log shutdown gracefully (#428) + +Log graceful shutdown at info level only + +reference: +https://github.com/prometheus/pushgateway/commit/92c8d1e028bc6a102bb578e03ba19776bff379ed + +Signed-off-by: mjip +--- + main.go | 29 ++++++++++++++++++----------- + 1 file changed, 18 insertions(+), 11 deletions(-) + +diff --git a/main.go b/main.go +index bdf61e8..2927d71 100644 +--- a/main.go ++++ b/main.go +@@ -14,6 +14,7 @@ + package main + + import ( ++ "context" + "fmt" + "net" + "net/http" +@@ -25,7 +26,6 @@ import ( + "path/filepath" + "strings" + "syscall" +- "time" + + "github.com/go-kit/kit/log" + "github.com/go-kit/kit/log/level" +@@ -191,13 +191,20 @@ func main() { + + mux.Handle(apiPath+"/v1/", http.StripPrefix(apiPath+"/v1", av1)) + +- go closeListenerOnQuit(l, quitCh, logger) +- err = (&http.Server{Addr: *listenAddress, Handler: mux}).Serve(l) +- level.Error(logger).Log("msg", "HTTP server stopped", "err", err) +- // To give running connections a chance to submit their payload, we wait +- // for 1sec, but we don't want to wait long (e.g. until all connections +- // are done) to not delay the shutdown. +- time.Sleep(time.Second) ++ server := &http.Server{ ++ Addr: *listenAddress, ++ Handler: mux, ++ } ++ go shutdownServerOnQuit(server, quitCh, logger) ++ err = (server).Serve(l) ++ ++ // In the case of a graceful shutdown, do not log the error. ++ if err == http.ErrServerClosed { ++ level.Info(logger).Log("msg", "HTTP server stopped") ++ } else { ++ level.Error(logger).Log("msg", "HTTP server stopped", "err", err) ++ } ++ + if err := ms.Shutdown(); err != nil { + level.Error(logger).Log("msg", "problem shutting down metric storage", "err", err) + } +@@ -237,9 +244,9 @@ func computeRoutePrefix(prefix string, externalURL *url.URL) string { + return prefix + } + +-// closeListenerOnQuite closes the provided listener upon closing the provided ++// shutdownServerOnQuit shutdowns the provided server upon closing the provided + // quitCh or upon receiving a SIGINT or SIGTERM. +-func closeListenerOnQuit(l net.Listener, quitCh <-chan struct{}, logger log.Logger) { ++func shutdownServerOnQuit(server *http.Server, quitCh <-chan struct{}, logger log.Logger) error { + notifier := make(chan os.Signal, 1) + signal.Notify(notifier, os.Interrupt, syscall.SIGTERM) + +@@ -251,5 +258,5 @@ func closeListenerOnQuit(l net.Listener, quitCh <-chan struct{}, logger log.Logg + level.Warn(logger).Log("msg", "received termination request via web service, exiting gracefully...") + break + } +- l.Close() ++ return server.Shutdown(context.Background()) + } +-- +2.30.0 + diff --git a/pushgateway.spec b/pushgateway.spec index e04cc2f..8a9b1ca 100644 --- a/pushgateway.spec +++ b/pushgateway.spec @@ -8,7 +8,7 @@ Name: pushgateway Version: 1.4.1 -Release: 1 +Release: 2 Summary: Prometheus pushgateway. License: ASL 2.0 URL: https://github.com/prometheus/%{name} @@ -17,6 +17,7 @@ Source1: %{name}.service Source2: %{name}.default Source3: vendor.tar.gz Patch0: 0001-fix-promu-dependence-and-fix-not-found-web.patch +Patch1: 0001-Log-shutdown-gracefully-428.patch BuildRequires: golang >= 1.13 BuildRequires: promu @@ -68,5 +69,8 @@ exit 0 %dir %attr(755, prometheus, prometheus)%{_sharedstatedir}/prometheus %changelog +* Mon Oct 11 2021 chenchen - 1.4.1-2 +- Logr shutdownr gracefully(#428 + * Wed Jul 14 2021 baizhonggui - 1.4.1-1 - Package init