vmalert: add disablePathAppend to remote read (#1712)

* vmalert: add disablePathAppend to remoteRead

* docs: add docs for remoteRead.disablePathAppend
This commit is contained in:
Alexander Rickardsson 2021-10-18 09:24:52 +02:00 committed by Aliaksandr Valialkin
parent 63571e1334
commit 0e1dbcd039
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1
6 changed files with 85 additions and 27 deletions

View File

@ -351,12 +351,12 @@ See full description for these flags in `./vmalert --help`.
## Monitoring ## Monitoring
`vmalert` exports various metrics in Prometheus exposition format at `http://vmalert-host:8880/metrics` page. `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 We recommend setting up regular scraping of this page either through `vmagent` or by Prometheus so that the exported
metrics may be analyzed later. metrics may be analyzed later.
Use official [Grafana dashboard](https://grafana.com/grafana/dashboards/14950) for `vmalert` overview. 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. 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. Optional bearer auth token to use for -remoteRead.url.
-remoteRead.bearerTokenFile string -remoteRead.bearerTokenFile string
Optional path to bearer token file to use for -remoteRead.url. 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 -remoteRead.ignoreRestoreErrors
Whether to ignore errors from remote storage when restoring alerts state on startup. (default true) Whether to ignore errors from remote storage when restoring alerts state on startup. (default true)
-remoteRead.lookback duration -remoteRead.lookback duration

View File

@ -24,18 +24,20 @@ type VMStorage struct {
evaluationInterval time.Duration evaluationInterval time.Duration
extraLabels []string extraLabels []string
extraParams []Param extraParams []Param
disablePathAppend bool
} }
// Clone makes clone of VMStorage, shares http client. // Clone makes clone of VMStorage, shares http client.
func (s *VMStorage) Clone() *VMStorage { func (s *VMStorage) Clone() *VMStorage {
return &VMStorage{ return &VMStorage{
c: s.c, c: s.c,
authCfg: s.authCfg, authCfg: s.authCfg,
datasourceURL: s.datasourceURL, datasourceURL: s.datasourceURL,
lookBack: s.lookBack, lookBack: s.lookBack,
queryStep: s.queryStep, queryStep: s.queryStep,
appendTypePrefix: s.appendTypePrefix, appendTypePrefix: s.appendTypePrefix,
dataSourceType: s.dataSourceType, dataSourceType: s.dataSourceType,
disablePathAppend: s.disablePathAppend,
} }
} }
@ -57,15 +59,16 @@ func (s *VMStorage) BuildWithParams(params QuerierParams) Querier {
} }
// NewVMStorage is a constructor for VMStorage // 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{ return &VMStorage{
c: c, c: c,
authCfg: authCfg, authCfg: authCfg,
datasourceURL: strings.TrimSuffix(baseURL, "/"), datasourceURL: strings.TrimSuffix(baseURL, "/"),
appendTypePrefix: appendTypePrefix, appendTypePrefix: appendTypePrefix,
lookBack: lookBack, lookBack: lookBack,
queryStep: queryStep, queryStep: queryStep,
dataSourceType: NewPrometheusType(), dataSourceType: NewPrometheusType(),
disablePathAppend: disablePathAppend,
} }
} }

View File

@ -118,7 +118,9 @@ func (s *VMStorage) setPrometheusInstantReqParams(r *http.Request, query string,
if s.appendTypePrefix { if s.appendTypePrefix {
r.URL.Path += prometheusPrefix r.URL.Path += prometheusPrefix
} }
r.URL.Path += prometheusInstantPath if !s.disablePathAppend {
r.URL.Path += prometheusInstantPath
}
q := r.URL.Query() q := r.URL.Query()
if s.lookBack > 0 { if s.lookBack > 0 {
timestamp = timestamp.Add(-s.lookBack) timestamp = timestamp.Add(-s.lookBack)
@ -136,7 +138,9 @@ func (s *VMStorage) setPrometheusRangeReqParams(r *http.Request, query string, s
if s.appendTypePrefix { if s.appendTypePrefix {
r.URL.Path += prometheusPrefix r.URL.Path += prometheusPrefix
} }
r.URL.Path += prometheusRangePath if !s.disablePathAppend {
r.URL.Path += prometheusRangePath
}
q := r.URL.Query() q := r.URL.Query()
q.Add("start", fmt.Sprintf("%d", start.Unix())) q.Add("start", fmt.Sprintf("%d", start.Unix()))
q.Add("end", fmt.Sprintf("%d", end.Unix())) q.Add("end", fmt.Sprintf("%d", end.Unix()))

View File

@ -83,7 +83,7 @@ func TestVMInstantQuery(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unexpected: %s", err) 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() p := NewPrometheusType()
pq := s.BuildWithParams(QuerierParams{DataSourceType: &p, EvaluationInterval: 15 * time.Second}) pq := s.BuildWithParams(QuerierParams{DataSourceType: &p, EvaluationInterval: 15 * time.Second})
@ -193,7 +193,7 @@ func TestVMRangeQuery(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unexpected: %s", err) 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() p := NewPrometheusType()
pq := s.BuildWithParams(QuerierParams{DataSourceType: &p, EvaluationInterval: 15 * time.Second}) 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) 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", "prometheus prefix",
false, false,
@ -263,6 +274,18 @@ func TestRequestParams(t *testing.T) {
checkEqualString(t, prometheusPrefix+prometheusInstantPath, r.URL.Path) 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", "prometheus range path",
true, true,
@ -273,6 +296,17 @@ func TestRequestParams(t *testing.T) {
checkEqualString(t, prometheusRangePath, r.URL.Path) 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", "prometheus range prefix",
true, true,
@ -284,6 +318,18 @@ func TestRequestParams(t *testing.T) {
checkEqualString(t, prometheusPrefix+prometheusRangePath, r.URL.Path) 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", "graphite path",
false, false,

View File

@ -12,7 +12,7 @@ import (
var ( var (
addr = flag.String("remoteRead.url", "", "Optional URL to VictoriaMetrics or vmselect that will be used to restore alerts "+ 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. "+ "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") 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") 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") 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") "By default system CA is used")
tlsServerName = flag.String("remoteRead.tlsServerName", "", "Optional TLS server name to use for connections to -remoteRead.url. "+ 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") "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. // 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) return nil, fmt.Errorf("failed to configure auth: %w", err)
} }
c := &http.Client{Transport: tr} 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
} }

View File

@ -355,12 +355,12 @@ See full description for these flags in `./vmalert --help`.
## Monitoring ## Monitoring
`vmalert` exports various metrics in Prometheus exposition format at `http://vmalert-host:8880/metrics` page. `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 We recommend setting up regular scraping of this page either through `vmagent` or by Prometheus so that the exported
metrics may be analyzed later. metrics may be analyzed later.
Use official [Grafana dashboard](https://grafana.com/grafana/dashboards/14950) for `vmalert` overview. 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. 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. Optional bearer auth token to use for -remoteRead.url.
-remoteRead.bearerTokenFile string -remoteRead.bearerTokenFile string
Optional path to bearer token file to use for -remoteRead.url. 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 -remoteRead.ignoreRestoreErrors
Whether to ignore errors from remote storage when restoring alerts state on startup. (default true) Whether to ignore errors from remote storage when restoring alerts state on startup. (default true)
-remoteRead.lookback duration -remoteRead.lookback duration