app/vmalert/datasource: use plain string literals instead of constants

This removes the unneeded level of indirection and improves code readability.

The "prometheus" and "graphite" constants aren't going to change in the future, so there is no sense in hiding them behind constants.
This commit is contained in:
Aliaksandr Valialkin 2021-11-05 19:56:37 +02:00
parent 31ac507bee
commit 56a25146cb
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1
3 changed files with 23 additions and 21 deletions

View File

@ -7,9 +7,6 @@ import (
"github.com/VictoriaMetrics/metricsql" "github.com/VictoriaMetrics/metricsql"
) )
const graphiteType = "graphite"
const prometheusType = "prometheus"
// Type represents data source type // Type represents data source type
type Type struct { type Type struct {
name string name string
@ -17,12 +14,16 @@ type Type struct {
// NewPrometheusType returns prometheus datasource type // NewPrometheusType returns prometheus datasource type
func NewPrometheusType() Type { func NewPrometheusType() Type {
return Type{name: prometheusType} return Type{
name: "prometheus",
}
} }
// NewGraphiteType returns graphite datasource type // NewGraphiteType returns graphite datasource type
func NewGraphiteType() Type { func NewGraphiteType() Type {
return Type{name: graphiteType} return Type{
name: "graphite",
}
} }
// NewRawType returns datasource type from raw string // NewRawType returns datasource type from raw string
@ -44,19 +45,19 @@ func (t *Type) Set(d Type) {
// String implements String interface with default value. // String implements String interface with default value.
func (t Type) String() string { func (t Type) String() string {
if t.name == "" { if t.name == "" {
return prometheusType return "prometheus"
} }
return t.name return t.name
} }
// ValidateExpr validates query expression with datasource ql. // ValidateExpr validates query expression with datasource ql.
func (t *Type) ValidateExpr(expr string) error { func (t *Type) ValidateExpr(expr string) error {
switch t.name { switch t.String() {
case graphiteType: case "graphite":
if _, err := graphiteql.Parse(expr); err != nil { if _, err := graphiteql.Parse(expr); err != nil {
return fmt.Errorf("bad graphite expr: %q, err: %w", expr, err) return fmt.Errorf("bad graphite expr: %q, err: %w", expr, err)
} }
case "", prometheusType: case "prometheus":
if _, err := metricsql.Parse(expr); err != nil { if _, err := metricsql.Parse(expr); err != nil {
return fmt.Errorf("bad prometheus expr: %q, err: %w", expr, err) return fmt.Errorf("bad prometheus expr: %q, err: %w", expr, err)
} }
@ -72,12 +73,13 @@ func (t *Type) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err := unmarshal(&s); err != nil { if err := unmarshal(&s); err != nil {
return err return err
} }
if s == "" {
s = "prometheus"
}
switch s { switch s {
case "": case "graphite", "prometheus":
s = prometheusType
case graphiteType, prometheusType:
default: default:
return fmt.Errorf("unknown datasource type=%q, want %q or %q", s, prometheusType, graphiteType) return fmt.Errorf("unknown datasource type=%q, want %q or %q", s, "prometheus", "graphite")
} }
t.name = s t.name = s
return nil return nil

View File

@ -80,10 +80,10 @@ func (s *VMStorage) Query(ctx context.Context, query string) ([]Metric, error) {
} }
ts := time.Now() ts := time.Now()
switch s.dataSourceType.name { switch s.dataSourceType.String() {
case "", prometheusType: case "prometheus":
s.setPrometheusInstantReqParams(req, query, ts) s.setPrometheusInstantReqParams(req, query, ts)
case graphiteType: case "graphite":
s.setGraphiteReqParams(req, query, ts) s.setGraphiteReqParams(req, query, ts)
default: default:
return nil, fmt.Errorf("engine not found: %q", s.dataSourceType.name) return nil, fmt.Errorf("engine not found: %q", s.dataSourceType.name)
@ -98,7 +98,7 @@ func (s *VMStorage) Query(ctx context.Context, query string) ([]Metric, error) {
}() }()
parseFn := parsePrometheusResponse parseFn := parsePrometheusResponse
if s.dataSourceType.name != prometheusType { if s.dataSourceType.name != "prometheus" {
parseFn = parseGraphiteResponse parseFn = parseGraphiteResponse
} }
return parseFn(req, resp) return parseFn(req, resp)
@ -108,7 +108,7 @@ func (s *VMStorage) Query(ctx context.Context, query string) ([]Metric, error) {
// For Prometheus type see https://prometheus.io/docs/prometheus/latest/querying/api/#range-queries // For Prometheus type see https://prometheus.io/docs/prometheus/latest/querying/api/#range-queries
// Graphite type isn't supported. // Graphite type isn't supported.
func (s *VMStorage) QueryRange(ctx context.Context, query string, start, end time.Time) ([]Metric, error) { func (s *VMStorage) QueryRange(ctx context.Context, query string, start, end time.Time) ([]Metric, error) {
if s.dataSourceType.name != prometheusType { if s.dataSourceType.name != "prometheus" {
return nil, fmt.Errorf("%q is not supported for QueryRange", s.dataSourceType.name) return nil, fmt.Errorf("%q is not supported for QueryRange", s.dataSourceType.name)
} }
req, err := s.newRequestPOST() req, err := s.newRequestPOST()

View File

@ -501,14 +501,14 @@ func TestRequestParams(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unexpected error: %s", err) t.Fatalf("unexpected error: %s", err)
} }
switch tc.vm.dataSourceType.name { switch tc.vm.dataSourceType.String() {
case "", prometheusType: case "prometheus":
if tc.queryRange { if tc.queryRange {
tc.vm.setPrometheusRangeReqParams(req, query, timestamp, timestamp) tc.vm.setPrometheusRangeReqParams(req, query, timestamp, timestamp)
} else { } else {
tc.vm.setPrometheusInstantReqParams(req, query, timestamp) tc.vm.setPrometheusInstantReqParams(req, query, timestamp)
} }
case graphiteType: case "graphite":
tc.vm.setGraphiteReqParams(req, query, timestamp) tc.vm.setGraphiteReqParams(req, query, timestamp)
} }
tc.checkFn(t, req) tc.checkFn(t, req)