From 56a25146cb11c473d97cc0f4f3b10849fa3e06c7 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 5 Nov 2021 19:56:37 +0200 Subject: [PATCH] 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. --- app/vmalert/datasource/type.go | 28 +++++++++++++++------------- app/vmalert/datasource/vm.go | 10 +++++----- app/vmalert/datasource/vm_test.go | 6 +++--- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/app/vmalert/datasource/type.go b/app/vmalert/datasource/type.go index 10dfc0ca44..975a642e44 100644 --- a/app/vmalert/datasource/type.go +++ b/app/vmalert/datasource/type.go @@ -7,9 +7,6 @@ import ( "github.com/VictoriaMetrics/metricsql" ) -const graphiteType = "graphite" -const prometheusType = "prometheus" - // Type represents data source type type Type struct { name string @@ -17,12 +14,16 @@ type Type struct { // NewPrometheusType returns prometheus datasource type func NewPrometheusType() Type { - return Type{name: prometheusType} + return Type{ + name: "prometheus", + } } // NewGraphiteType returns graphite datasource type func NewGraphiteType() Type { - return Type{name: graphiteType} + return Type{ + name: "graphite", + } } // NewRawType returns datasource type from raw string @@ -44,19 +45,19 @@ func (t *Type) Set(d Type) { // String implements String interface with default value. func (t Type) String() string { if t.name == "" { - return prometheusType + return "prometheus" } return t.name } // ValidateExpr validates query expression with datasource ql. func (t *Type) ValidateExpr(expr string) error { - switch t.name { - case graphiteType: + switch t.String() { + case "graphite": if _, err := graphiteql.Parse(expr); err != nil { return fmt.Errorf("bad graphite expr: %q, err: %w", expr, err) } - case "", prometheusType: + case "prometheus": if _, err := metricsql.Parse(expr); err != nil { 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 { return err } + if s == "" { + s = "prometheus" + } switch s { - case "": - s = prometheusType - case graphiteType, prometheusType: + case "graphite", "prometheus": 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 return nil diff --git a/app/vmalert/datasource/vm.go b/app/vmalert/datasource/vm.go index 5d3dc62671..304d88cecd 100644 --- a/app/vmalert/datasource/vm.go +++ b/app/vmalert/datasource/vm.go @@ -80,10 +80,10 @@ func (s *VMStorage) Query(ctx context.Context, query string) ([]Metric, error) { } ts := time.Now() - switch s.dataSourceType.name { - case "", prometheusType: + switch s.dataSourceType.String() { + case "prometheus": s.setPrometheusInstantReqParams(req, query, ts) - case graphiteType: + case "graphite": s.setGraphiteReqParams(req, query, ts) default: 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 - if s.dataSourceType.name != prometheusType { + if s.dataSourceType.name != "prometheus" { parseFn = parseGraphiteResponse } 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 // Graphite type isn't supported. 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) } req, err := s.newRequestPOST() diff --git a/app/vmalert/datasource/vm_test.go b/app/vmalert/datasource/vm_test.go index 7dd584662b..946cedfe71 100644 --- a/app/vmalert/datasource/vm_test.go +++ b/app/vmalert/datasource/vm_test.go @@ -501,14 +501,14 @@ func TestRequestParams(t *testing.T) { if err != nil { t.Fatalf("unexpected error: %s", err) } - switch tc.vm.dataSourceType.name { - case "", prometheusType: + switch tc.vm.dataSourceType.String() { + case "prometheus": if tc.queryRange { tc.vm.setPrometheusRangeReqParams(req, query, timestamp, timestamp) } else { tc.vm.setPrometheusInstantReqParams(req, query, timestamp) } - case graphiteType: + case "graphite": tc.vm.setGraphiteReqParams(req, query, timestamp) } tc.checkFn(t, req)