From 8048f6307d0d477af3e8b4a864f6150b422d6fb4 Mon Sep 17 00:00:00 2001 From: Vladimir Smirnov Date: Sun, 6 Oct 2019 17:57:04 +0200 Subject: [PATCH] Make aliasByTags to work correctly with other functions Fixes #422 --- cmd/mockbackend/aliasByTags2.yaml | 10 ++++++++++ cmd/mockbackend/main.go | 18 ++++++++++++++++++ expr/functions/aliasByTags/function.go | 20 +------------------- expr/functions/aliasByTags/function_test.go | 21 ++++++++++++++++++--- 4 files changed, 47 insertions(+), 22 deletions(-) create mode 100644 cmd/mockbackend/aliasByTags2.yaml diff --git a/cmd/mockbackend/aliasByTags2.yaml b/cmd/mockbackend/aliasByTags2.yaml new file mode 100644 index 000000000..ddfd6f767 --- /dev/null +++ b/cmd/mockbackend/aliasByTags2.yaml @@ -0,0 +1,10 @@ +listeners: + - address: ":9070" + expressions: + "seriesByTag('hostname=gateway1', 'ifName=~GigabitEthernet0/0/0', 'name=~interface_(in|out)_octets')": + pathExpression: "seriesByTag('hostname=gateway1', 'ifName=~GigabitEthernet0/0/0', 'name=~interface_(in|out)_octets')" + data: + - metricName: "metric.interface_in_octets;ifName=GigabitEthernet0/0/0;hostname=gateway1" + values: [1.0, .NaN, 2.0, 3.0, 4.0, 5.0] + - metricName: "metric.interface_out_octets;ifName=GigabitEthernet0/0/0;hostname=gateway1" + values: [2.0, .NaN, 3.0, .NaN, 5.0, 6.0] diff --git a/cmd/mockbackend/main.go b/cmd/mockbackend/main.go index 05338b800..ec67254dc 100644 --- a/cmd/mockbackend/main.go +++ b/cmd/mockbackend/main.go @@ -44,6 +44,24 @@ type Metric struct { Values []float64 `yaml:"values"` } +type metricForJson struct { + MetricName string + Values []string +} + +func (m *Metric) MarshalJSON() ([]byte, error) { + m2 := metricForJson{ + MetricName: m.MetricName, + Values: make([]string, len(m.Values)), + } + + for i, v := range m.Values { + m2.Values[i] = fmt.Sprintf("%v", v) + } + + return json.Marshal(m2) +} + type Response struct { PathExpression string `yaml:"pathExpression"` Data []Metric `yaml:"data"` diff --git a/expr/functions/aliasByTags/function.go b/expr/functions/aliasByTags/function.go index 2a3f8209c..b9297279d 100644 --- a/expr/functions/aliasByTags/function.go +++ b/expr/functions/aliasByTags/function.go @@ -1,7 +1,6 @@ package aliasByTags import ( - "fmt" "github.com/go-graphite/carbonapi/expr/helper" "github.com/go-graphite/carbonapi/expr/interfaces" "github.com/go-graphite/carbonapi/expr/types" @@ -10,8 +9,6 @@ import ( "strings" ) -const NAME = "name" - type aliasByTags struct { interfaces.FunctionBase } @@ -29,29 +26,14 @@ func New(configFile string) []interfaces.FunctionMetadata { return res } -func metricToTagMap(s string) map[string]string { - r := make(map[string]string) - for _, p := range strings.Split(s, ";") { - if strings.Contains(p, "=") { - tagValue := strings.SplitN(p, "=", 2) - r[tagValue[0]] = tagValue[1] - } else { - r[NAME] = p - } - } - return r -} - func (f *aliasByTags) Do(e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) { args, err := helper.GetSeriesArg(e.Args()[0], from, until, values) if err != nil { - fmt.Println("getSeriesArg missing argument") return nil, err } tags, err := e.GetNodeOrTagArgs(1) if err != nil { - fmt.Println("GetNodeOrTagArgs missing argument") return nil, err } @@ -59,7 +41,7 @@ func (f *aliasByTags) Do(e parser.Expr, from, until int64, values map[parser.Met for _, a := range args { var matched []string - metricTags := metricToTagMap(a.Name) + metricTags := a.Tags nodes := strings.Split(metricTags["name"], ".") for _, tag := range tags { if tag.IsTag { diff --git a/expr/functions/aliasByTags/function_test.go b/expr/functions/aliasByTags/function_test.go index 7ce5746a4..eec0a4b54 100644 --- a/expr/functions/aliasByTags/function_test.go +++ b/expr/functions/aliasByTags/function_test.go @@ -1,6 +1,7 @@ package aliasByTags import ( + "math" "testing" "time" @@ -9,16 +10,23 @@ import ( "github.com/go-graphite/carbonapi/expr/types" "github.com/go-graphite/carbonapi/pkg/parser" th "github.com/go-graphite/carbonapi/tests" + + "github.com/go-graphite/carbonapi/expr/functions/perSecond" ) func init() { md := New("") - evaluator := th.EvaluatorFromFunc(md[0].F) - metadata.SetEvaluator(evaluator) - helper.SetEvaluator(evaluator) for _, m := range md { metadata.RegisterFunction(m.Name, m.F) } + psFunc := perSecond.New("") + for _, m := range psFunc { + metadata.RegisterFunction(m.Name, m.F) + } + + evaluator := th.EvaluatorFromFuncWithMetadata(metadata.FunctionMD.Functions) + metadata.SetEvaluator(evaluator) + helper.SetEvaluator(evaluator) } func TestAliasByTags(t *testing.T) { @@ -53,6 +61,13 @@ func TestAliasByTags(t *testing.T) { }, []*types.MetricData{types.MakeMetricData("bam.bar.metric1", []float64{1, 2, 3, 4, 5}, 1, now32)}, }, + { + `aliasByTags(perSecond(*), 'name')`, + map[parser.MetricRequest][]*types.MetricData{ + {"*", 0, 1}: {types.MakeMetricData("base.metric1;foo=bar;baz=bam", []float64{1, 2, 3, 4, 5}, 1, now32)}, + }, + []*types.MetricData{types.MakeMetricData("base.metric1", []float64{math.NaN(), 1, 1, 1, 1}, 1, now32)}, + }, } for _, tt := range tests {