Skip to content

Commit 8048f63

Browse files
author
Vladimir Smirnov
committed
Make aliasByTags to work correctly with other functions
Fixes #422
1 parent 151eb74 commit 8048f63

File tree

4 files changed

+47
-22
lines changed

4 files changed

+47
-22
lines changed

cmd/mockbackend/aliasByTags2.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
listeners:
2+
- address: ":9070"
3+
expressions:
4+
"seriesByTag('hostname=gateway1', 'ifName=~GigabitEthernet0/0/0', 'name=~interface_(in|out)_octets')":
5+
pathExpression: "seriesByTag('hostname=gateway1', 'ifName=~GigabitEthernet0/0/0', 'name=~interface_(in|out)_octets')"
6+
data:
7+
- metricName: "metric.interface_in_octets;ifName=GigabitEthernet0/0/0;hostname=gateway1"
8+
values: [1.0, .NaN, 2.0, 3.0, 4.0, 5.0]
9+
- metricName: "metric.interface_out_octets;ifName=GigabitEthernet0/0/0;hostname=gateway1"
10+
values: [2.0, .NaN, 3.0, .NaN, 5.0, 6.0]

cmd/mockbackend/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ type Metric struct {
4444
Values []float64 `yaml:"values"`
4545
}
4646

47+
type metricForJson struct {
48+
MetricName string
49+
Values []string
50+
}
51+
52+
func (m *Metric) MarshalJSON() ([]byte, error) {
53+
m2 := metricForJson{
54+
MetricName: m.MetricName,
55+
Values: make([]string, len(m.Values)),
56+
}
57+
58+
for i, v := range m.Values {
59+
m2.Values[i] = fmt.Sprintf("%v", v)
60+
}
61+
62+
return json.Marshal(m2)
63+
}
64+
4765
type Response struct {
4866
PathExpression string `yaml:"pathExpression"`
4967
Data []Metric `yaml:"data"`

expr/functions/aliasByTags/function.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package aliasByTags
22

33
import (
4-
"fmt"
54
"github.com/go-graphite/carbonapi/expr/helper"
65
"github.com/go-graphite/carbonapi/expr/interfaces"
76
"github.com/go-graphite/carbonapi/expr/types"
@@ -10,8 +9,6 @@ import (
109
"strings"
1110
)
1211

13-
const NAME = "name"
14-
1512
type aliasByTags struct {
1613
interfaces.FunctionBase
1714
}
@@ -29,37 +26,22 @@ func New(configFile string) []interfaces.FunctionMetadata {
2926
return res
3027
}
3128

32-
func metricToTagMap(s string) map[string]string {
33-
r := make(map[string]string)
34-
for _, p := range strings.Split(s, ";") {
35-
if strings.Contains(p, "=") {
36-
tagValue := strings.SplitN(p, "=", 2)
37-
r[tagValue[0]] = tagValue[1]
38-
} else {
39-
r[NAME] = p
40-
}
41-
}
42-
return r
43-
}
44-
4529
func (f *aliasByTags) Do(e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
4630
args, err := helper.GetSeriesArg(e.Args()[0], from, until, values)
4731
if err != nil {
48-
fmt.Println("getSeriesArg missing argument")
4932
return nil, err
5033
}
5134

5235
tags, err := e.GetNodeOrTagArgs(1)
5336
if err != nil {
54-
fmt.Println("GetNodeOrTagArgs missing argument")
5537
return nil, err
5638
}
5739

5840
var results []*types.MetricData
5941

6042
for _, a := range args {
6143
var matched []string
62-
metricTags := metricToTagMap(a.Name)
44+
metricTags := a.Tags
6345
nodes := strings.Split(metricTags["name"], ".")
6446
for _, tag := range tags {
6547
if tag.IsTag {

expr/functions/aliasByTags/function_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package aliasByTags
22

33
import (
4+
"math"
45
"testing"
56
"time"
67

@@ -9,16 +10,23 @@ import (
910
"github.com/go-graphite/carbonapi/expr/types"
1011
"github.com/go-graphite/carbonapi/pkg/parser"
1112
th "github.com/go-graphite/carbonapi/tests"
13+
14+
"github.com/go-graphite/carbonapi/expr/functions/perSecond"
1215
)
1316

1417
func init() {
1518
md := New("")
16-
evaluator := th.EvaluatorFromFunc(md[0].F)
17-
metadata.SetEvaluator(evaluator)
18-
helper.SetEvaluator(evaluator)
1919
for _, m := range md {
2020
metadata.RegisterFunction(m.Name, m.F)
2121
}
22+
psFunc := perSecond.New("")
23+
for _, m := range psFunc {
24+
metadata.RegisterFunction(m.Name, m.F)
25+
}
26+
27+
evaluator := th.EvaluatorFromFuncWithMetadata(metadata.FunctionMD.Functions)
28+
metadata.SetEvaluator(evaluator)
29+
helper.SetEvaluator(evaluator)
2230
}
2331

2432
func TestAliasByTags(t *testing.T) {
@@ -53,6 +61,13 @@ func TestAliasByTags(t *testing.T) {
5361
},
5462
[]*types.MetricData{types.MakeMetricData("bam.bar.metric1", []float64{1, 2, 3, 4, 5}, 1, now32)},
5563
},
64+
{
65+
`aliasByTags(perSecond(*), 'name')`,
66+
map[parser.MetricRequest][]*types.MetricData{
67+
{"*", 0, 1}: {types.MakeMetricData("base.metric1;foo=bar;baz=bam", []float64{1, 2, 3, 4, 5}, 1, now32)},
68+
},
69+
[]*types.MetricData{types.MakeMetricData("base.metric1", []float64{math.NaN(), 1, 1, 1, 1}, 1, now32)},
70+
},
5671
}
5772

5873
for _, tt := range tests {

0 commit comments

Comments
 (0)