app/vmalert: fix datasource.roundDigits Parameter (#4341)

app/vmalert: fix querybuild clone and extraParams merge logic

See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4340
This commit is contained in:
gsakun 2023-06-01 15:44:11 +08:00 committed by GitHub
parent 0bca7d49b9
commit 20dc3db71e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View file

@ -62,6 +62,7 @@ func (s *VMStorage) Clone() *VMStorage {
queryStep: s.queryStep, queryStep: s.queryStep,
appendTypePrefix: s.appendTypePrefix, appendTypePrefix: s.appendTypePrefix,
dataSourceType: s.dataSourceType, dataSourceType: s.dataSourceType,
extraParams: s.extraParams,
} }
} }
@ -69,7 +70,16 @@ func (s *VMStorage) Clone() *VMStorage {
func (s *VMStorage) ApplyParams(params QuerierParams) *VMStorage { func (s *VMStorage) ApplyParams(params QuerierParams) *VMStorage {
s.dataSourceType = toDatasourceType(params.DataSourceType) s.dataSourceType = toDatasourceType(params.DataSourceType)
s.evaluationInterval = params.EvaluationInterval s.evaluationInterval = params.EvaluationInterval
s.extraParams = params.QueryParams if len(params.QueryParams) != 0 {
for k, vl := range params.QueryParams {
if s.extraParams.Has(k) {
s.extraParams.Del(k)
}
for _, v := range vl {
s.extraParams.Add(k, v)
}
}
}
s.debug = params.Debug s.debug = params.Debug
if params.Headers != nil { if params.Headers != nil {
for key, value := range params.Headers { for key, value := range params.Headers {

View file

@ -378,6 +378,9 @@ func TestRequestParams(t *testing.T) {
} }
query := "up" query := "up"
timestamp := time.Date(2001, 2, 3, 4, 5, 6, 0, time.UTC) timestamp := time.Date(2001, 2, 3, 4, 5, 6, 0, time.UTC)
storage := VMStorage{
extraParams: url.Values{"round_digits": {"10"}},
}
testCases := []struct { testCases := []struct {
name string name string
queryRange bool queryRange bool
@ -574,6 +577,17 @@ func TestRequestParams(t *testing.T) {
checkEqualString(t, exp, r.URL.RawQuery) checkEqualString(t, exp, r.URL.RawQuery)
}, },
}, },
{
"custom params overrides the original params",
false,
storage.Clone().ApplyParams(QuerierParams{
QueryParams: url.Values{"round_digits": {"2"}},
}),
func(t *testing.T, r *http.Request) {
exp := fmt.Sprintf("query=%s&round_digits=2&time=%d", query, timestamp.Unix())
checkEqualString(t, exp, r.URL.RawQuery)
},
},
{ {
"graphite extra params", "graphite extra params",
false, false,