lib/pushmetrics: fix a panic caused by pushing metrics during the graceful shutdown process of vmstorage nodes. (#5549)

Co-authored-by: zhangdongdong <zhangdongdong@kuaishou.com>
Co-authored-by: Roman Khavronenko <roman@victoriametrics.com>
Signed-off-by: hagen1778 <roman@victoriametrics.com>
This commit is contained in:
zhdd99 2024-01-09 13:01:03 +01:00 committed by hagen1778
parent b79d4cc988
commit fe2d9f6646
No known key found for this signature in database
GPG key ID: 3BF75F3741CA9640

View file

@ -28,6 +28,11 @@ func init() {
flagutil.RegisterSecretFlag("pushmetrics.url") flagutil.RegisterSecretFlag("pushmetrics.url")
} }
var (
// create a custom context for the pushmetrics module to close the metric reporting goroutine when the vmstorage process is shutdown.
pushMetricsCtx, cancelPushMetric = context.WithCancel(context.Background())
)
// Init must be called after logger.Init // Init must be called after logger.Init
func Init() { func Init() {
extraLabels := strings.Join(*pushExtraLabel, ",") extraLabels := strings.Join(*pushExtraLabel, ",")
@ -37,8 +42,12 @@ func Init() {
Headers: *pushHeader, Headers: *pushHeader,
DisableCompression: *disableCompression, DisableCompression: *disableCompression,
} }
if err := metrics.InitPushExtWithOptions(context.Background(), pu, *pushInterval, appmetrics.WritePrometheusMetrics, opts); err != nil { if err := metrics.InitPushExtWithOptions(pushMetricsCtx, pu, *pushInterval, appmetrics.WritePrometheusMetrics, opts); err != nil {
logger.Fatalf("cannot initialize pushmetrics: %s", err) logger.Fatalf("cannot initialize pushmetrics: %s", err)
} }
} }
} }
func StopPushMetrics() {
cancelPushMetric()
}