Skip to content

Commit

Permalink
Make aliasByTags to work correctly with other functions
Browse files Browse the repository at this point in the history
Fixes #422
  • Loading branch information
Civil committed Oct 13, 2019
1 parent 151eb74 commit 8048f63
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
10 changes: 10 additions & 0 deletions cmd/mockbackend/aliasByTags2.yaml
Original file line number Diff line number Diff line change
@@ -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]
18 changes: 18 additions & 0 deletions cmd/mockbackend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
20 changes: 1 addition & 19 deletions expr/functions/aliasByTags/function.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -10,8 +9,6 @@ import (
"strings"
)

const NAME = "name"

type aliasByTags struct {
interfaces.FunctionBase
}
Expand All @@ -29,37 +26,22 @@ 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
}

var results []*types.MetricData

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 {
Expand Down
21 changes: 18 additions & 3 deletions expr/functions/aliasByTags/function_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aliasByTags

import (
"math"
"testing"
"time"

Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8048f63

Please sign in to comment.