diff --git a/app/vmalert/datasource/init.go b/app/vmalert/datasource/init.go index 9c9546dea..d1d3b5109 100644 --- a/app/vmalert/datasource/init.go +++ b/app/vmalert/datasource/init.go @@ -11,6 +11,7 @@ import ( var ( addr = flag.String("datasource.url", "", "Victoria Metrics or VMSelect url. Required parameter."+ " E.g. http://127.0.0.1:8428") + appendTypePrefix = flag.Bool("datasource.appendTypePrefix", false, "Whether to add type prefix to -datasource.url based on the query type. Set to true if sending different query types to VMSelect URL.") basicAuthUsername = flag.String("datasource.basicAuth.username", "", "Optional basic auth username for -datasource.url") basicAuthPassword = flag.String("datasource.basicAuth.password", "", "Optional basic auth password for -datasource.url") @@ -41,5 +42,5 @@ func Init() (Querier, error) { } tr.MaxIdleConns = *maxIdleConnections c := &http.Client{Transport: tr} - return NewVMStorage(*addr, *basicAuthUsername, *basicAuthPassword, *lookBack, *queryStep, c), nil + return NewVMStorage(*addr, *basicAuthUsername, *basicAuthPassword, *lookBack, *queryStep, *appendTypePrefix, c), nil } diff --git a/app/vmalert/datasource/vm.go b/app/vmalert/datasource/vm.go index 0c0626fbc..e06b3af3d 100644 --- a/app/vmalert/datasource/vm.go +++ b/app/vmalert/datasource/vm.go @@ -74,26 +74,31 @@ func (r graphiteResponse) metrics() []Metric { // VMStorage represents vmstorage entity with ability to read and write metrics type VMStorage struct { - c *http.Client - datasourceURL string - basicAuthUser string - basicAuthPass string - lookBack time.Duration - queryStep time.Duration + c *http.Client + datasourceURL string + basicAuthUser string + basicAuthPass string + appendTypePrefix bool + lookBack time.Duration + queryStep time.Duration } const queryPath = "/api/v1/query" const graphitePath = "/render" +const prometheusPrefix = "/prometheus" +const graphitePrefix = "/graphite" + // NewVMStorage is a constructor for VMStorage -func NewVMStorage(baseURL, basicAuthUser, basicAuthPass string, lookBack time.Duration, queryStep time.Duration, c *http.Client) *VMStorage { +func NewVMStorage(baseURL, basicAuthUser, basicAuthPass string, lookBack time.Duration, queryStep time.Duration, appendTypePrefix bool, c *http.Client) *VMStorage { return &VMStorage{ - c: c, - basicAuthUser: basicAuthUser, - basicAuthPass: basicAuthPass, - datasourceURL: strings.TrimSuffix(baseURL, "/"), - lookBack: lookBack, - queryStep: queryStep, + c: c, + basicAuthUser: basicAuthUser, + basicAuthPass: basicAuthPass, + datasourceURL: strings.TrimSuffix(baseURL, "/"), + appendTypePrefix: appendTypePrefix, + lookBack: lookBack, + queryStep: queryStep, } } @@ -137,6 +142,9 @@ func (s *VMStorage) queryDataSource( } func (s *VMStorage) setPrometheusReqParams(r *http.Request, query string) { + if s.appendTypePrefix { + r.URL.Path += prometheusPrefix + } r.URL.Path += queryPath q := r.URL.Query() q.Set("query", query) @@ -151,6 +159,9 @@ func (s *VMStorage) setPrometheusReqParams(r *http.Request, query string) { } func (s *VMStorage) setGraphiteReqParams(r *http.Request, query string) { + if s.appendTypePrefix { + r.URL.Path += graphitePrefix + } r.URL.Path += graphitePath q := r.URL.Query() q.Set("format", "json") diff --git a/app/vmalert/datasource/vm_test.go b/app/vmalert/datasource/vm_test.go index 1afe0d8e3..2c6148133 100644 --- a/app/vmalert/datasource/vm_test.go +++ b/app/vmalert/datasource/vm_test.go @@ -69,7 +69,7 @@ func TestVMSelectQuery(t *testing.T) { srv := httptest.NewServer(mux) defer srv.Close() - am := NewVMStorage(srv.URL, basicAuthName, basicAuthPass, time.Minute, 0, srv.Client()) + am := NewVMStorage(srv.URL, basicAuthName, basicAuthPass, time.Minute, 0, false, srv.Client()) if _, err := am.Query(ctx, query, NewPrometheusType()); err == nil { t.Fatalf("expected connection error got nil") } diff --git a/app/vmalert/remoteread/init.go b/app/vmalert/remoteread/init.go index 7a0aa32ac..165db403d 100644 --- a/app/vmalert/remoteread/init.go +++ b/app/vmalert/remoteread/init.go @@ -35,5 +35,5 @@ func Init() (datasource.Querier, error) { return nil, fmt.Errorf("failed to create transport: %w", err) } c := &http.Client{Transport: tr} - return datasource.NewVMStorage(*addr, *basicAuthUsername, *basicAuthPassword, 0, 0, c), nil + return datasource.NewVMStorage(*addr, *basicAuthUsername, *basicAuthPassword, 0, 0, false, c), nil }