From 0e1dbcd039546070d594ef95aa95e68961e1515b Mon Sep 17 00:00:00 2001 From: Alexander Rickardsson Date: Mon, 18 Oct 2021 09:24:52 +0200 Subject: [PATCH] vmalert: add disablePathAppend to remote read (#1712) * vmalert: add disablePathAppend to remoteRead * docs: add docs for remoteRead.disablePathAppend --- app/vmalert/README.md | 8 +++-- app/vmalert/datasource/vm.go | 33 ++++++++++-------- app/vmalert/datasource/vm_prom_api.go | 8 +++-- app/vmalert/datasource/vm_test.go | 50 +++++++++++++++++++++++++-- app/vmalert/remoteread/init.go | 5 +-- docs/vmalert.md | 8 +++-- 6 files changed, 85 insertions(+), 27 deletions(-) diff --git a/app/vmalert/README.md b/app/vmalert/README.md index d62cf6ae40..9e481694bf 100644 --- a/app/vmalert/README.md +++ b/app/vmalert/README.md @@ -351,12 +351,12 @@ See full description for these flags in `./vmalert --help`. ## Monitoring -`vmalert` exports various metrics in Prometheus exposition format at `http://vmalert-host:8880/metrics` page. -We recommend setting up regular scraping of this page either through `vmagent` or by Prometheus so that the exported +`vmalert` exports various metrics in Prometheus exposition format at `http://vmalert-host:8880/metrics` page. +We recommend setting up regular scraping of this page either through `vmagent` or by Prometheus so that the exported metrics may be analyzed later. Use official [Grafana dashboard](https://grafana.com/grafana/dashboards/14950) for `vmalert` overview. -If you have suggestions for improvements or have found a bug - please open an issue on github or add +If you have suggestions for improvements or have found a bug - please open an issue on github or add a review to the dashboard. @@ -496,6 +496,8 @@ The shortlist of configuration flags is the following: Optional bearer auth token to use for -remoteRead.url. -remoteRead.bearerTokenFile string Optional path to bearer token file to use for -remoteRead.url. + -remoteRead.disablePathAppend + Whether to disable automatic appending of '/api/v1/query' path to the configured -remoteRead.url. -remoteRead.ignoreRestoreErrors Whether to ignore errors from remote storage when restoring alerts state on startup. (default true) -remoteRead.lookback duration diff --git a/app/vmalert/datasource/vm.go b/app/vmalert/datasource/vm.go index 34a83d3412..5d3dc62671 100644 --- a/app/vmalert/datasource/vm.go +++ b/app/vmalert/datasource/vm.go @@ -24,18 +24,20 @@ type VMStorage struct { evaluationInterval time.Duration extraLabels []string extraParams []Param + disablePathAppend bool } // Clone makes clone of VMStorage, shares http client. func (s *VMStorage) Clone() *VMStorage { return &VMStorage{ - c: s.c, - authCfg: s.authCfg, - datasourceURL: s.datasourceURL, - lookBack: s.lookBack, - queryStep: s.queryStep, - appendTypePrefix: s.appendTypePrefix, - dataSourceType: s.dataSourceType, + c: s.c, + authCfg: s.authCfg, + datasourceURL: s.datasourceURL, + lookBack: s.lookBack, + queryStep: s.queryStep, + appendTypePrefix: s.appendTypePrefix, + dataSourceType: s.dataSourceType, + disablePathAppend: s.disablePathAppend, } } @@ -57,15 +59,16 @@ func (s *VMStorage) BuildWithParams(params QuerierParams) Querier { } // NewVMStorage is a constructor for VMStorage -func NewVMStorage(baseURL string, authCfg *promauth.Config, lookBack time.Duration, queryStep time.Duration, appendTypePrefix bool, c *http.Client) *VMStorage { +func NewVMStorage(baseURL string, authCfg *promauth.Config, lookBack time.Duration, queryStep time.Duration, appendTypePrefix bool, c *http.Client, disablePathAppend bool) *VMStorage { return &VMStorage{ - c: c, - authCfg: authCfg, - datasourceURL: strings.TrimSuffix(baseURL, "/"), - appendTypePrefix: appendTypePrefix, - lookBack: lookBack, - queryStep: queryStep, - dataSourceType: NewPrometheusType(), + c: c, + authCfg: authCfg, + datasourceURL: strings.TrimSuffix(baseURL, "/"), + appendTypePrefix: appendTypePrefix, + lookBack: lookBack, + queryStep: queryStep, + dataSourceType: NewPrometheusType(), + disablePathAppend: disablePathAppend, } } diff --git a/app/vmalert/datasource/vm_prom_api.go b/app/vmalert/datasource/vm_prom_api.go index dcffe2dc1c..2faba4d27e 100644 --- a/app/vmalert/datasource/vm_prom_api.go +++ b/app/vmalert/datasource/vm_prom_api.go @@ -118,7 +118,9 @@ func (s *VMStorage) setPrometheusInstantReqParams(r *http.Request, query string, if s.appendTypePrefix { r.URL.Path += prometheusPrefix } - r.URL.Path += prometheusInstantPath + if !s.disablePathAppend { + r.URL.Path += prometheusInstantPath + } q := r.URL.Query() if s.lookBack > 0 { timestamp = timestamp.Add(-s.lookBack) @@ -136,7 +138,9 @@ func (s *VMStorage) setPrometheusRangeReqParams(r *http.Request, query string, s if s.appendTypePrefix { r.URL.Path += prometheusPrefix } - r.URL.Path += prometheusRangePath + if !s.disablePathAppend { + r.URL.Path += prometheusRangePath + } q := r.URL.Query() q.Add("start", fmt.Sprintf("%d", start.Unix())) q.Add("end", fmt.Sprintf("%d", end.Unix())) diff --git a/app/vmalert/datasource/vm_test.go b/app/vmalert/datasource/vm_test.go index 801974d9c9..ba406a46c8 100644 --- a/app/vmalert/datasource/vm_test.go +++ b/app/vmalert/datasource/vm_test.go @@ -83,7 +83,7 @@ func TestVMInstantQuery(t *testing.T) { if err != nil { t.Fatalf("unexpected: %s", err) } - s := NewVMStorage(srv.URL, authCfg, time.Minute, 0, false, srv.Client()) + s := NewVMStorage(srv.URL, authCfg, time.Minute, 0, false, srv.Client(), false) p := NewPrometheusType() pq := s.BuildWithParams(QuerierParams{DataSourceType: &p, EvaluationInterval: 15 * time.Second}) @@ -193,7 +193,7 @@ func TestVMRangeQuery(t *testing.T) { if err != nil { t.Fatalf("unexpected: %s", err) } - s := NewVMStorage(srv.URL, authCfg, time.Minute, 0, false, srv.Client()) + s := NewVMStorage(srv.URL, authCfg, time.Minute, 0, false, srv.Client(), false) p := NewPrometheusType() pq := s.BuildWithParams(QuerierParams{DataSourceType: &p, EvaluationInterval: 15 * time.Second}) @@ -252,6 +252,17 @@ func TestRequestParams(t *testing.T) { checkEqualString(t, prometheusInstantPath, r.URL.Path) }, }, + { + "prometheus path with disablePathAppend", + false, + &VMStorage{ + dataSourceType: NewPrometheusType(), + disablePathAppend: true, + }, + func(t *testing.T, r *http.Request) { + checkEqualString(t, "", r.URL.Path) + }, + }, { "prometheus prefix", false, @@ -263,6 +274,18 @@ func TestRequestParams(t *testing.T) { checkEqualString(t, prometheusPrefix+prometheusInstantPath, r.URL.Path) }, }, + { + "prometheus prefix with disablePathAppend", + false, + &VMStorage{ + dataSourceType: NewPrometheusType(), + appendTypePrefix: true, + disablePathAppend: true, + }, + func(t *testing.T, r *http.Request) { + checkEqualString(t, prometheusPrefix, r.URL.Path) + }, + }, { "prometheus range path", true, @@ -273,6 +296,17 @@ func TestRequestParams(t *testing.T) { checkEqualString(t, prometheusRangePath, r.URL.Path) }, }, + { + "prometheus range path with disablePathAppend", + true, + &VMStorage{ + dataSourceType: NewPrometheusType(), + disablePathAppend: true, + }, + func(t *testing.T, r *http.Request) { + checkEqualString(t, "", r.URL.Path) + }, + }, { "prometheus range prefix", true, @@ -284,6 +318,18 @@ func TestRequestParams(t *testing.T) { checkEqualString(t, prometheusPrefix+prometheusRangePath, r.URL.Path) }, }, + { + "prometheus range prefix with disablePathAppend", + true, + &VMStorage{ + dataSourceType: NewPrometheusType(), + appendTypePrefix: true, + disablePathAppend: true, + }, + func(t *testing.T, r *http.Request) { + checkEqualString(t, prometheusPrefix, r.URL.Path) + }, + }, { "graphite path", false, diff --git a/app/vmalert/remoteread/init.go b/app/vmalert/remoteread/init.go index 73a2993b5d..12a9024723 100644 --- a/app/vmalert/remoteread/init.go +++ b/app/vmalert/remoteread/init.go @@ -12,7 +12,7 @@ import ( var ( addr = flag.String("remoteRead.url", "", "Optional URL to VictoriaMetrics or vmselect that will be used to restore alerts "+ "state. This configuration makes sense only if `vmalert` was configured with `remoteWrite.url` before and has been successfully persisted its state. "+ - "E.g. http://127.0.0.1:8428") + "E.g. http://127.0.0.1:8428. See also -remoteRead.disablePathAppend") basicAuthUsername = flag.String("remoteRead.basicAuth.username", "", "Optional basic auth username for -remoteRead.url") basicAuthPassword = flag.String("remoteRead.basicAuth.password", "", "Optional basic auth password for -remoteRead.url") basicAuthPasswordFile = flag.String("remoteRead.basicAuth.passwordFile", "", "Optional path to basic auth password to use for -remoteRead.url") @@ -26,6 +26,7 @@ var ( "By default system CA is used") tlsServerName = flag.String("remoteRead.tlsServerName", "", "Optional TLS server name to use for connections to -remoteRead.url. "+ "By default the server name from -remoteRead.url is used") + disablePathAppend = flag.Bool("remoteRead.disablePathAppend", false, "Whether to disable automatic appending of '/api/v1' path to the configured -remoteRead.url.") ) // Init creates a Querier from provided flag values. @@ -43,5 +44,5 @@ func Init() (datasource.QuerierBuilder, error) { return nil, fmt.Errorf("failed to configure auth: %w", err) } c := &http.Client{Transport: tr} - return datasource.NewVMStorage(*addr, authCfg, 0, 0, false, c), nil + return datasource.NewVMStorage(*addr, authCfg, 0, 0, false, c, *disablePathAppend), nil } diff --git a/docs/vmalert.md b/docs/vmalert.md index 2dc811a6da..d630b557de 100644 --- a/docs/vmalert.md +++ b/docs/vmalert.md @@ -355,12 +355,12 @@ See full description for these flags in `./vmalert --help`. ## Monitoring -`vmalert` exports various metrics in Prometheus exposition format at `http://vmalert-host:8880/metrics` page. -We recommend setting up regular scraping of this page either through `vmagent` or by Prometheus so that the exported +`vmalert` exports various metrics in Prometheus exposition format at `http://vmalert-host:8880/metrics` page. +We recommend setting up regular scraping of this page either through `vmagent` or by Prometheus so that the exported metrics may be analyzed later. Use official [Grafana dashboard](https://grafana.com/grafana/dashboards/14950) for `vmalert` overview. -If you have suggestions for improvements or have found a bug - please open an issue on github or add +If you have suggestions for improvements or have found a bug - please open an issue on github or add a review to the dashboard. @@ -500,6 +500,8 @@ The shortlist of configuration flags is the following: Optional bearer auth token to use for -remoteRead.url. -remoteRead.bearerTokenFile string Optional path to bearer token file to use for -remoteRead.url. + -remoteRead.disablePathAppend + Whether to disable automatic appending of '/api/v1/query' path to the configured -remoteRead.url. -remoteRead.ignoreRestoreErrors Whether to ignore errors from remote storage when restoring alerts state on startup. (default true) -remoteRead.lookback duration