From b6158e8956110b24e8b39072dd6d9c9d0fd649bb Mon Sep 17 00:00:00 2001 From: Julien Date: Thu, 3 Oct 2024 10:26:05 +0200 Subject: [PATCH 1/2] Notify web UI when starting up and shutting down Signed-off-by: Julien --- cmd/prometheus/main.go | 3 +++ web/api/notifications.go | 2 ++ 2 files changed, 5 insertions(+) diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index d8369770b..8ad1db637 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -503,6 +503,7 @@ func main() { notifs := api.NewNotifications(cfg.maxNotificationsSubscribers, prometheus.DefaultRegisterer) cfg.web.NotificationsSub = notifs.Sub cfg.web.NotificationsGetter = notifs.Get + notifs.AddNotification(api.StartingUp) if err := cfg.setFeatureListOptions(logger); err != nil { fmt.Fprintln(os.Stderr, fmt.Errorf("Error parsing feature list: %w", err)) @@ -989,6 +990,7 @@ func main() { func(err error) { close(cancel) webHandler.SetReady(web.Stopping) + notifs.AddNotification(api.ShuttingDown) }, ) } @@ -1174,6 +1176,7 @@ func main() { reloadReady.Close() webHandler.SetReady(web.Ready) + notifs.DeleteNotification(api.StartingUp) level.Info(logger).Log("msg", "Server is ready to receive web requests.") <-cancel return nil diff --git a/web/api/notifications.go b/web/api/notifications.go index 976f0b076..a838fbd98 100644 --- a/web/api/notifications.go +++ b/web/api/notifications.go @@ -22,6 +22,8 @@ import ( const ( ConfigurationUnsuccessful = "Configuration reload has failed." + StartingUp = "Prometheus is starting and replaying the write-ahead log (WAL)." + ShuttingDown = "Prometheus is shutting down and gracefully stopping all operations." ) // Notification represents an individual notification message. From 21e0f83b68789f281a5c1639ccd5b30a486d7fc8 Mon Sep 17 00:00:00 2001 From: Julien Date: Fri, 4 Oct 2024 10:11:56 +0200 Subject: [PATCH 2/2] Move notifications in utils Signed-off-by: Julien --- cmd/prometheus/main.go | 14 +++++++------- {web/api => util/notifications}/notifications.go | 2 +- .../notifications}/notifications_test.go | 2 +- web/api/v1/api.go | 10 +++++----- web/web.go | 6 +++--- 5 files changed, 17 insertions(+), 17 deletions(-) rename {web/api => util/notifications}/notifications.go (99%) rename {web/api => util/notifications}/notifications_test.go (99%) diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index 8ad1db637..11d8caae6 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -76,9 +76,9 @@ import ( "github.com/prometheus/prometheus/tsdb/wlog" "github.com/prometheus/prometheus/util/documentcli" "github.com/prometheus/prometheus/util/logging" + "github.com/prometheus/prometheus/util/notifications" prom_runtime "github.com/prometheus/prometheus/util/runtime" "github.com/prometheus/prometheus/web" - "github.com/prometheus/prometheus/web/api" ) var ( @@ -500,10 +500,10 @@ func main() { logger := promlog.New(&cfg.promlogConfig) - notifs := api.NewNotifications(cfg.maxNotificationsSubscribers, prometheus.DefaultRegisterer) + notifs := notifications.NewNotifications(cfg.maxNotificationsSubscribers, prometheus.DefaultRegisterer) cfg.web.NotificationsSub = notifs.Sub cfg.web.NotificationsGetter = notifs.Get - notifs.AddNotification(api.StartingUp) + notifs.AddNotification(notifications.StartingUp) if err := cfg.setFeatureListOptions(logger); err != nil { fmt.Fprintln(os.Stderr, fmt.Errorf("Error parsing feature list: %w", err)) @@ -990,7 +990,7 @@ func main() { func(err error) { close(cancel) webHandler.SetReady(web.Stopping) - notifs.AddNotification(api.ShuttingDown) + notifs.AddNotification(notifications.ShuttingDown) }, ) } @@ -1091,10 +1091,10 @@ func main() { callback := func(success bool) { if success { - notifs.DeleteNotification(api.ConfigurationUnsuccessful) + notifs.DeleteNotification(notifications.ConfigurationUnsuccessful) return } - notifs.AddNotification(api.ConfigurationUnsuccessful) + notifs.AddNotification(notifications.ConfigurationUnsuccessful) } g.Add( @@ -1176,7 +1176,7 @@ func main() { reloadReady.Close() webHandler.SetReady(web.Ready) - notifs.DeleteNotification(api.StartingUp) + notifs.DeleteNotification(notifications.StartingUp) level.Info(logger).Log("msg", "Server is ready to receive web requests.") <-cancel return nil diff --git a/web/api/notifications.go b/util/notifications/notifications.go similarity index 99% rename from web/api/notifications.go rename to util/notifications/notifications.go index a838fbd98..4888a0b66 100644 --- a/web/api/notifications.go +++ b/util/notifications/notifications.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package api +package notifications import ( "sync" diff --git a/web/api/notifications_test.go b/util/notifications/notifications_test.go similarity index 99% rename from web/api/notifications_test.go rename to util/notifications/notifications_test.go index 437ff1ec4..e487e9ce5 100644 --- a/web/api/notifications_test.go +++ b/util/notifications/notifications_test.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package api +package notifications import ( "sync" diff --git a/web/api/v1/api.go b/web/api/v1/api.go index 46666af90..95ab7ea2a 100644 --- a/web/api/v1/api.go +++ b/web/api/v1/api.go @@ -54,8 +54,8 @@ import ( "github.com/prometheus/prometheus/tsdb/index" "github.com/prometheus/prometheus/util/annotations" "github.com/prometheus/prometheus/util/httputil" + "github.com/prometheus/prometheus/util/notifications" "github.com/prometheus/prometheus/util/stats" - "github.com/prometheus/prometheus/web/api" ) type status string @@ -214,8 +214,8 @@ type API struct { gatherer prometheus.Gatherer isAgent bool statsRenderer StatsRenderer - notificationsGetter func() []api.Notification - notificationsSub func() (<-chan api.Notification, func(), bool) + notificationsGetter func() []notifications.Notification + notificationsSub func() (<-chan notifications.Notification, func(), bool) remoteWriteHandler http.Handler remoteReadHandler http.Handler @@ -249,8 +249,8 @@ func NewAPI( corsOrigin *regexp.Regexp, runtimeInfo func() (RuntimeInfo, error), buildInfo *PrometheusVersion, - notificationsGetter func() []api.Notification, - notificationsSub func() (<-chan api.Notification, func(), bool), + notificationsGetter func() []notifications.Notification, + notificationsSub func() (<-chan notifications.Notification, func(), bool), gatherer prometheus.Gatherer, registerer prometheus.Registerer, statsRenderer StatsRenderer, diff --git a/web/web.go b/web/web.go index 724ca9105..5e1d3d230 100644 --- a/web/web.go +++ b/web/web.go @@ -59,7 +59,7 @@ import ( "github.com/prometheus/prometheus/template" "github.com/prometheus/prometheus/util/httputil" "github.com/prometheus/prometheus/util/netconnlimit" - "github.com/prometheus/prometheus/web/api" + "github.com/prometheus/prometheus/util/notifications" api_v1 "github.com/prometheus/prometheus/web/api/v1" "github.com/prometheus/prometheus/web/ui" ) @@ -267,8 +267,8 @@ type Options struct { RuleManager *rules.Manager Notifier *notifier.Manager Version *PrometheusVersion - NotificationsGetter func() []api.Notification - NotificationsSub func() (<-chan api.Notification, func(), bool) + NotificationsGetter func() []notifications.Notification + NotificationsSub func() (<-chan notifications.Notification, func(), bool) Flags map[string]string ListenAddresses []string