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>
This commit is contained in:
zhdd99 2024-01-09 20:01:03 +08:00 committed by Aliaksandr Valialkin
parent ec37e6353a
commit 7883248bf3
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB
2 changed files with 17 additions and 1 deletions

View file

@ -144,6 +144,10 @@ func main() {
} }
logger.Infof("successfully shut down http service in %.3f seconds", time.Since(startTime).Seconds()) logger.Infof("successfully shut down http service in %.3f seconds", time.Since(startTime).Seconds())
// close the metric reporting goroutine
pushmetrics.StopPushMetrics()
logger.Infof("successfully stop push metric in %.3f seconds", time.Since(startTime).Seconds())
logger.Infof("gracefully shutting down the service") logger.Infof("gracefully shutting down the service")
startTime = time.Now() startTime = time.Now()
stopStaleSnapshotsRemover() stopStaleSnapshotsRemover()

View file

@ -24,12 +24,24 @@ 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, ",")
for _, pu := range *pushURL { for _, pu := range *pushURL {
if err := metrics.InitPushExt(pu, *pushInterval, extraLabels, appmetrics.WritePrometheusMetrics); err != nil { opts := &metrics.PushOptions{
ExtraLabels: extraLabels,
}
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()
}