From a8835b443f270b2aaf663cd27485f4f3b50700a1 Mon Sep 17 00:00:00 2001 From: Roman Khavronenko Date: Fri, 2 Jun 2023 11:38:55 +0200 Subject: [PATCH] vmalert: fix nil map assignment (#4392) * vmalert: fix nil map assignment The storage instance with nil map params was created for remote-read purposes. And before change https://github.com/VictoriaMetrics/VictoriaMetrics/pull/4341/commits/7a9ae9de0d2221cf7759e49ebcde0a06508b0f68 this map was ignored in ApplyParams. Now, it started to be used and vmalert panics in runtime. The fix properly inits map for at `NewVMStorage` and verifies it is not nil on assignment in `ApplyParams`. Signed-off-by: hagen1778 * vmalert: add to changelog Signed-off-by: hagen1778 * vmalert: properly clone Storage params Signed-off-by: hagen1778 * vmalert: properly clone Storage params Signed-off-by: hagen1778 * vmalert: properly clone Storage params Signed-off-by: hagen1778 --------- Signed-off-by: hagen1778 (cherry picked from commit de948120889529af03c1dbc644eb7305d46bae19) Signed-off-by: hagen1778 --- app/vmalert/datasource/vm.go | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/app/vmalert/datasource/vm.go b/app/vmalert/datasource/vm.go index 99d39ee135..30f5ef1f26 100644 --- a/app/vmalert/datasource/vm.go +++ b/app/vmalert/datasource/vm.go @@ -54,7 +54,7 @@ type keyValue struct { // Clone makes clone of VMStorage, shares http client. func (s *VMStorage) Clone() *VMStorage { - return &VMStorage{ + ns := &VMStorage{ c: s.c, authCfg: s.authCfg, datasourceURL: s.datasourceURL, @@ -64,20 +64,35 @@ func (s *VMStorage) Clone() *VMStorage { dataSourceType: s.dataSourceType, evaluationInterval: s.evaluationInterval, - extraParams: s.extraParams, - extraHeaders: s.extraHeaders, + + // init map so it can be populated below + extraParams: url.Values{}, debug: s.debug, } + if len(s.extraHeaders) > 0 { + ns.extraHeaders = make([]keyValue, len(s.extraHeaders)) + copy(ns.extraHeaders, s.extraHeaders) + } + for k, v := range s.extraParams { + ns.extraParams[k] = v + } + + return ns } // ApplyParams - changes given querier params. func (s *VMStorage) ApplyParams(params QuerierParams) *VMStorage { s.dataSourceType = toDatasourceType(params.DataSourceType) s.evaluationInterval = params.EvaluationInterval - for k, vl := range params.QueryParams { - for _, v := range vl { // custom query params are prior to default ones - s.extraParams.Set(k, v) + if params.QueryParams != nil { + if s.extraParams == nil { + s.extraParams = url.Values{} + } + for k, vl := range params.QueryParams { + for _, v := range vl { // custom query params are prior to default ones + s.extraParams.Set(k, v) + } } } if params.Headers != nil { @@ -105,6 +120,7 @@ func NewVMStorage(baseURL string, authCfg *promauth.Config, lookBack time.Durati lookBack: lookBack, queryStep: queryStep, dataSourceType: datasourcePrometheus, + extraParams: url.Values{}, } }