Skip to content

Commit

Permalink
Merge pull request #838 from go-graphite/njpm/aliasByNode-name-tag
Browse files Browse the repository at this point in the history
fix(functions): fix `"name"` tag overrides in various functions
  • Loading branch information
Civil authored Jul 31, 2024
2 parents d9996b2 + 8b60211 commit 3bd2c2b
Show file tree
Hide file tree
Showing 19 changed files with 87 additions and 100 deletions.
6 changes: 3 additions & 3 deletions expr/expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ func TestEvalExpression(t *testing.T) {
},
},
[]*types.MetricData{
types.MakeMetricData("devops.service.server1.filter.received.reduce.asPercent.count", []float64{25, 200, 200}, 1, now32),
types.MakeMetricData("devops.service.server2.filter.received.reduce.asPercent.count", []float64{25, 100, 400}, 1, now32),
types.MakeMetricData("devops.service.server1.filter.received.reduce.asPercent.count", []float64{25, 200, 200}, 1, now32).SetNameTag("devops.service.server1.filter.received.valid.count"),
types.MakeMetricData("devops.service.server2.filter.received.reduce.asPercent.count", []float64{25, 100, 400}, 1, now32).SetNameTag("devops.service.server2.filter.received.valid.count"),
},
},
{
Expand All @@ -272,7 +272,7 @@ func TestEvalExpression(t *testing.T) {
},
},
[]*types.MetricData{
types.MakeMetricData("devops.service.server2.filter.received.reduce.asPercent.count", []float64{25, 100, 400}, 1, now32),
types.MakeMetricData("devops.service.server2.filter.received.reduce.asPercent.count", []float64{25, 100, 400}, 1, now32).SetNameTag("devops.service.server2.filter.received.valid.count"),
},
},
{
Expand Down
4 changes: 2 additions & 2 deletions expr/functions/alias/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func (f *alias) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr
if allowFormatStr {
name = strings.ReplaceAll(name, "${expr}", arg.Name)
}
r := arg.CopyName(name)

r := arg.CopyLinkTags()
r.Name = name
results[i] = r
}

Expand Down
6 changes: 3 additions & 3 deletions expr/functions/alias/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestAlias(t *testing.T) {
},
},
[]*types.MetricData{types.MakeMetricData("renamed",
[]float64{1, 2, 3, 4, 5}, 1, now32)},
[]float64{1, 2, 3, 4, 5}, 1, now32).SetNameTag("metric1")},
},
{
"alias(metric2, \"some format ${expr} str ${expr} and another ${expr\", true)",
Expand All @@ -68,7 +68,7 @@ func TestAlias(t *testing.T) {
[]float64{1, 2, 3, 4, 5},
1,
now32,
),
).SetNameTag("metric2"),
},
},
{
Expand All @@ -93,7 +93,7 @@ func TestAlias(t *testing.T) {
[]float64{1, 2, 3, 4, 5},
1,
now32,
),
).SetNameTag("metric2"),
},
},
}
Expand Down
3 changes: 2 additions & 1 deletion expr/functions/aliasByMetric/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func (f *aliasByMetric) Do(ctx context.Context, eval interfaces.Evaluator, e par
metric := types.ExtractNameTag(a.Name)
part := strings.Split(metric, ".")
name := part[len(part)-1]
ret := a.CopyName(name)
ret := a.CopyLinkTags()
ret.Name = name
ret.PathExpression = name
return ret
})
Expand Down
2 changes: 1 addition & 1 deletion expr/functions/aliasByMetric/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestAliasByMetric(t *testing.T) {
map[parser.MetricRequest][]*types.MetricData{
{Metric: "metric1.foo.bar.baz", From: 0, Until: 1}: {types.MakeMetricData("metric1.foo.bar.baz", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
[]*types.MetricData{types.MakeMetricData("baz", []float64{1, 2, 3, 4, 5}, 1, now32)},
[]*types.MetricData{types.MakeMetricData("baz", []float64{1, 2, 3, 4, 5}, 1, now32).SetNameTag("metric1.foo.bar.baz")},
},
}

Expand Down
4 changes: 2 additions & 2 deletions expr/functions/aliasByNode/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (f *aliasByNode) Do(ctx context.Context, eval interfaces.Evaluator, e parse
results := make([]*types.MetricData, len(args))

for i, a := range args {
name := helper.AggKey(a, nodesOrTags)
r := a.CopyName(name)
r := a.CopyLinkTags()
r.Name = helper.AggKey(a, nodesOrTags)
results[i] = r
}

Expand Down
30 changes: 15 additions & 15 deletions expr/functions/aliasByNode/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,94 +56,94 @@ func TestAliasByNode(t *testing.T) {
types.MakeMetricData("a.b.c.d.e", []float64{8, 2, 4}, 1, now32),
},
},
[]*types.MetricData{types.MakeMetricData("2", []float64{8, 2, 4}, 1, now32)},
[]*types.MetricData{types.MakeMetricData("2", []float64{8, 2, 4}, 1, now32).SetNameTag("a.b.c.d.e")},
},
{
Target: "aliasByNode(aliasSub(a.b.c.d.e, '(.*)', '0.1.2.@.4'), 2)",
M: map[parser.MetricRequest][]*types.MetricData{
{Metric: "a.b.c.d.e", From: 0, Until: 1}: {types.MakeMetricData("a.b.c.d.e", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
Want: []*types.MetricData{types.MakeMetricData("2", []float64{1, 2, 3, 4, 5}, 1, now32)},
Want: []*types.MetricData{types.MakeMetricData("2", []float64{1, 2, 3, 4, 5}, 1, now32).SetNameTag("a.b.c.d.e")},
},
{
Target: "aliasByNode(aliasSub(transformNull(metric1.foo.bar.ba*, 0), 'baz', 'word'), 2, 3)",
M: map[parser.MetricRequest][]*types.MetricData{
{Metric: "metric1.foo.bar.ba*", From: 0, Until: 1}: {types.MakeMetricData("metric1.foo.bar.baz", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
Want: []*types.MetricData{types.MakeMetricData("bar.word", []float64{1, 2, 3, 4, 5}, 1, now32).SetTag("transformNull", "0")},
Want: []*types.MetricData{types.MakeMetricData("bar.word", []float64{1, 2, 3, 4, 5}, 1, now32).SetTag("transformNull", "0").SetNameTag("metric1.foo.bar.baz")},
},
{
Target: "aliasByNode(metric1.foo.bar.baz,1)",
M: map[parser.MetricRequest][]*types.MetricData{
{Metric: "metric1.foo.bar.baz", From: 0, Until: 1}: {types.MakeMetricData("metric1.foo.bar.baz", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
Want: []*types.MetricData{types.MakeMetricData("foo", []float64{1, 2, 3, 4, 5}, 1, now32)},
Want: []*types.MetricData{types.MakeMetricData("foo", []float64{1, 2, 3, 4, 5}, 1, now32).SetNameTag("metric1.foo.bar.baz")},
},
{
Target: "aliasByNode(metric1.foo.bar.baz,1,3)",
M: map[parser.MetricRequest][]*types.MetricData{
{Metric: "metric1.foo.bar.baz", From: 0, Until: 1}: {types.MakeMetricData("metric1.foo.bar.baz", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
Want: []*types.MetricData{types.MakeMetricData("foo.baz",
[]float64{1, 2, 3, 4, 5}, 1, now32)},
[]float64{1, 2, 3, 4, 5}, 1, now32).SetNameTag("metric1.foo.bar.baz")},
},
{
Target: "aliasByNode(metric1.foo.bar.baz,1,-2)",
M: map[parser.MetricRequest][]*types.MetricData{
{Metric: "metric1.foo.bar.baz", From: 0, Until: 1}: {types.MakeMetricData("metric1.foo.bar.baz", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
Want: []*types.MetricData{types.MakeMetricData("foo.bar",
[]float64{1, 2, 3, 4, 5}, 1, now32)},
[]float64{1, 2, 3, 4, 5}, 1, now32).SetNameTag("metric1.foo.bar.baz")},
},
{
Target: `aliasByTags(*, "foo")`,
M: map[parser.MetricRequest][]*types.MetricData{
{Metric: "*", From: 0, Until: 1}: {types.MakeMetricData("metric1.foo.bar.baz;foo=bar;baz=bam", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
Want: []*types.MetricData{types.MakeMetricData("bar", []float64{1, 2, 3, 4, 5}, 1, now32).SetTag("foo", "bar").SetTag("baz", "bam")},
Want: []*types.MetricData{types.MakeMetricData("bar", []float64{1, 2, 3, 4, 5}, 1, now32).SetTag("foo", "bar").SetTag("baz", "bam").SetNameTag("metric1.foo.bar.baz")},
},
{
Target: `aliasByTags(*, "foo", "name")`,
M: map[parser.MetricRequest][]*types.MetricData{
{Metric: "*", From: 0, Until: 1}: {types.MakeMetricData("metric1;foo=bar", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
Want: []*types.MetricData{types.MakeMetricData("bar.metric1", []float64{1, 2, 3, 4, 5}, 1, now32).SetTag("foo", "bar")},
Want: []*types.MetricData{types.MakeMetricData("bar.metric1", []float64{1, 2, 3, 4, 5}, 1, now32).SetTag("foo", "bar").SetNameTag("metric1")},
},
{
Target: `aliasByTags(*, 2, "blah", "foo", 1)`,
M: map[parser.MetricRequest][]*types.MetricData{
{Metric: "*", From: 0, Until: 1}: {types.MakeMetricData("base.metric1;foo=bar;baz=bam", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
Want: []*types.MetricData{types.MakeMetricData(".bar.metric1", []float64{1, 2, 3, 4, 5}, 1, now32).SetTag("foo", "bar").SetTag("baz", "bam")},
Want: []*types.MetricData{types.MakeMetricData(".bar.metric1", []float64{1, 2, 3, 4, 5}, 1, now32).SetTag("foo", "bar").SetTag("baz", "bam").SetNameTag("base.metric1")},
},
{
Target: `aliasByTags(*, 2, "baz", "foo", 1)`,
M: map[parser.MetricRequest][]*types.MetricData{
{Metric: "*", From: 0, Until: 1}: {types.MakeMetricData("base.metric1;foo=bar;baz=bam", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
Want: []*types.MetricData{types.MakeMetricData("bam.bar.metric1", []float64{1, 2, 3, 4, 5}, 1, now32).SetTag("foo", "bar").SetTag("baz", "bam")},
Want: []*types.MetricData{types.MakeMetricData("bam.bar.metric1", []float64{1, 2, 3, 4, 5}, 1, now32).SetTag("foo", "bar").SetTag("baz", "bam").SetNameTag("base.metric1")},
},
{
Target: `aliasByTags(perSecond(*), 'name')`,
M: map[parser.MetricRequest][]*types.MetricData{
{Metric: "*", From: 0, Until: 1}: {types.MakeMetricData("base.metric1;foo=bar;baz=bam", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
Want: []*types.MetricData{types.MakeMetricData("base.metric1", []float64{math.NaN(), 1, 1, 1, 1}, 1, now32).SetTag("foo", "bar").SetTag("baz", "bam").SetTag("perSecond", "1")},
Want: []*types.MetricData{types.MakeMetricData("base.metric1", []float64{math.NaN(), 1, 1, 1, 1}, 1, now32).SetTag("foo", "bar").SetTag("baz", "bam").SetTag("perSecond", "1").SetNameTag("base.metric1")},
},
{
Target: "aliasByNode(metric1.fo*.bar.baz,1,3)",
M: map[parser.MetricRequest][]*types.MetricData{
{Metric: "metric1.fo*.bar.baz", From: 0, Until: 1}: {types.MakeMetricData("metric1.foo==.bar.baz", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
Want: []*types.MetricData{types.MakeMetricData("foo==.baz",
[]float64{1, 2, 3, 4, 5}, 1, now32)},
[]float64{1, 2, 3, 4, 5}, 1, now32).SetNameTag("metric1.foo==.bar.baz")},
},
{
Target: `aliasByTags(*, 2, "baz", "foo", 1)`,
M: map[parser.MetricRequest][]*types.MetricData{
{Metric: "*", From: 0, Until: 1}: {types.MakeMetricData("base.metric1;foo=bar=;baz=bam==", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
Want: []*types.MetricData{types.MakeMetricData("bam==.bar=.metric1", []float64{1, 2, 3, 4, 5}, 1, now32).SetTag("foo", "bar=").SetTag("baz", "bam==")},
Want: []*types.MetricData{types.MakeMetricData("bam==.bar=.metric1", []float64{1, 2, 3, 4, 5}, 1, now32).SetTag("foo", "bar=").SetTag("baz", "bam==").SetNameTag("base.metric1")},
},
// extract nodes with sumSeries
{
Expand All @@ -155,7 +155,7 @@ func TestAliasByNode(t *testing.T) {
types.MakeMetricData("metric.c2.b", []float64{3, math.NaN(), 4, 5, 6, math.NaN()}, 1, now32),
},
},
Want: []*types.MetricData{types.MakeMetricData("{a,b}*.b", []float64{6, math.NaN(), 9, 8, 15, 11}, 1, now32).SetTag("aggregatedBy", "sum")},
Want: []*types.MetricData{types.MakeMetricData("{a,b}*.b", []float64{6, math.NaN(), 9, 8, 15, 11}, 1, now32).SetTag("aggregatedBy", "sum").SetNameTag("metric.{a,b}*.b")},
},
// extract tags from seriesByTag
{
Expand All @@ -170,7 +170,7 @@ func TestAliasByNode(t *testing.T) {
},
// Want: []*types.MetricData{types.MakeMetricData("value____.metric", []float64{6, math.NaN(), 9, 8, 15, 11}, 1, now32)},
Want: []*types.MetricData{
types.MakeMetricData("value21.metric", []float64{6, math.NaN(), 9, 8, 15, 11}, 1, now32).SetTag("aggregatedBy", "sum").SetTag("tag2", "value21")},
types.MakeMetricData("value21.metric", []float64{6, math.NaN(), 9, 8, 15, 11}, 1, now32).SetTag("aggregatedBy", "sum").SetTag("tag2", "value21").SetNameTag("metric")},
},
// TODO msaf1980: tests with extractTagsFromArgs = true
}
Expand Down
13 changes: 2 additions & 11 deletions expr/functions/aliasQuery/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,8 @@ func (f *aliasQuery) Do(ctx context.Context, eval interfaces.Evaluator, e parser
if err != nil {
return nil, err
}

n := fmt.Sprintf(newName, v)

var r *types.MetricData
if series.Name == n {
r = series.CopyLinkTags()
r.Tags["name"] = r.Name
} else {
r = series.CopyName(n)
}

r := series.CopyLinkTags()
r.Name = fmt.Sprintf(newName, v)
results[i] = r
}

Expand Down
4 changes: 2 additions & 2 deletions expr/functions/aliasQuery/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func TestAliasQuery(t *testing.T) {
},
},
Want: []*types.MetricData{
types.MakeMetricData("Channel 200 MHz", []float64{1, 2, 3, 4, 5}, 1, now),
types.MakeMetricData("Channel 400 MHz", []float64{10, 20, 30, 40, 50}, 1, now),
types.MakeMetricData("Channel 200 MHz", []float64{1, 2, 3, 4, 5}, 1, now).SetNameTag("channel.power.1"),
types.MakeMetricData("Channel 400 MHz", []float64{10, 20, 30, 40, 50}, 1, now).SetNameTag("channel.power.2"),
},
},
}
Expand Down
13 changes: 2 additions & 11 deletions expr/functions/aliasSub/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,8 @@ func (f *aliasSub) Do(ctx context.Context, eval interfaces.Evaluator, e parser.E
results := make([]*types.MetricData, len(args))

for i, a := range args {
var r *types.MetricData

oldName := a.Name
// not safe for tagged metrics
name := re.ReplaceAllString(oldName, replace)
if oldName == name {
r = a.CopyLinkTags()
r.Tags["name"] = r.Name
} else {
r = a.CopyName(name)
}
r := a.CopyLinkTags()
r.Name = re.ReplaceAllString(a.Name, replace)
results[i] = r
}

Expand Down
10 changes: 5 additions & 5 deletions expr/functions/aliasSub/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,31 @@ func TestAliasSub(t *testing.T) {
{Metric: "metric1.foo.bar.baz", From: 0, Until: 1}: {types.MakeMetricData("metric1.foo.bar.baz", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
[]*types.MetricData{types.MakeMetricData("metric1.replaced.bar.baz",
[]float64{1, 2, 3, 4, 5}, 1, now32)},
[]float64{1, 2, 3, 4, 5}, 1, now32).SetNameTag("metric1.foo.bar.baz")},
},
{
"aliasSub(metric1.TCP100,\"^.*TCP(\\d+)\",\"$1\")",
map[parser.MetricRequest][]*types.MetricData{
{Metric: "metric1.TCP100", From: 0, Until: 1}: {types.MakeMetricData("metric1.TCP100", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
[]*types.MetricData{types.MakeMetricData("100",
[]float64{1, 2, 3, 4, 5}, 1, now32)},
[]float64{1, 2, 3, 4, 5}, 1, now32).SetNameTag("metric1.TCP100")},
},
{
"aliasSub(metric1.TCP100,\"^.*TCP(\\d+)\", \"\\1\")",
map[parser.MetricRequest][]*types.MetricData{
{Metric: "metric1.TCP100", From: 0, Until: 1}: {types.MakeMetricData("metric1.TCP100", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
[]*types.MetricData{types.MakeMetricData("100",
[]float64{1, 2, 3, 4, 5}, 1, now32)},
[]float64{1, 2, 3, 4, 5}, 1, now32).SetNameTag("metric1.TCP100")},
},
{
"aliasSub(metric1.foo.bar.baz, \"foo\", \"replaced\")",
map[parser.MetricRequest][]*types.MetricData{
{Metric: "metric1.foo.bar.baz", From: 0, Until: 1}: {types.MakeMetricData("metric1.foo.bar.baz", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
[]*types.MetricData{types.MakeMetricData("metric1.replaced.bar.baz",
[]float64{1, 2, 3, 4, 5}, 1, now32)},
[]float64{1, 2, 3, 4, 5}, 1, now32).SetNameTag("metric1.foo.bar.baz")},
},
// #290
{
Expand All @@ -66,7 +66,7 @@ func TestAliasSub(t *testing.T) {
{Metric: "*", From: 0, Until: 1}: {types.MakeMetricData("diffSeries(dns.snake.sql_updated, dns.snake.zone_updated)", []float64{1, 2, 3, 4, 5}, 1, now32)},
},
[]*types.MetricData{types.MakeMetricData("diffSeries(dns.snake.sql_updated, snake diff to sql updated)",
[]float64{1, 2, 3, 4, 5}, 1, now32).SetNameTag("diffSeries(dns.snake.sql_updated, snake diff to sql updated)")},
[]float64{1, 2, 3, 4, 5}, 1, now32).SetNameTag("dns.snake.sql_updated")},
},
}

Expand Down
3 changes: 2 additions & 1 deletion expr/functions/delay/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func (f *delay) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Expr
for i, series := range seriesList {
length := len(series.Values)

result := series.CopyName("delay(" + series.Name + "," + stepsStr + ")")
result := series.CopyLink()
result.Name = "delay(" + series.Name + "," + stepsStr + ")"
result.Tags["delay"] = stepsStr

var newValues []float64
Expand Down
6 changes: 3 additions & 3 deletions expr/functions/delay/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ func TestDelay(t *testing.T) {
{Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{1, 2, 3, math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32)},
},
[]*types.MetricData{types.MakeMetricData("delay(metric1,3)",
[]float64{math.NaN(), math.NaN(), math.NaN(), 1, 2, 3, math.NaN()}, 1, now32).SetTag("delay", "3").SetNameTag("delay(metric1,3)")},
[]float64{math.NaN(), math.NaN(), math.NaN(), 1, 2, 3, math.NaN()}, 1, now32).SetTag("delay", "3").SetNameTag("metric1")},
},
{
"delay(metric1,-3)",
map[parser.MetricRequest][]*types.MetricData{
{Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{math.NaN(), math.NaN(), math.NaN(), 1, 2, 3, math.NaN()}, 1, now32)},
},
[]*types.MetricData{types.MakeMetricData("delay(metric1,-3)",
[]float64{1, 2, 3, math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32).SetTag("delay", "-3").SetNameTag("delay(metric1,-3)")},
[]float64{1, 2, 3, math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32).SetTag("delay", "-3").SetNameTag("metric1")},
},
{
"delay(metric1,0)",
map[parser.MetricRequest][]*types.MetricData{
{Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{1, 2, 3, math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32)},
},
[]*types.MetricData{types.MakeMetricData("delay(metric1,0)",
[]float64{1, 2, 3, math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32).SetTag("delay", "0").SetNameTag("delay(metric1,0)")},
[]float64{1, 2, 3, math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32).SetTag("delay", "0").SetNameTag("metric1")},
},
}

Expand Down
3 changes: 2 additions & 1 deletion expr/functions/moving/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ func (f *moving) Do(ctx context.Context, eval interfaces.Evaluator, e parser.Exp
result := make([]*types.MetricData, len(adjustedArgs))

for j, a := range adjustedArgs {
r := a.CopyName(e.Target() + "(" + a.Name + "," + argstr + ")")
r := a.CopyLink()
r.Name = e.Target() + "(" + a.Name + "," + argstr + ")"
r.Tags[e.Target()] = argstr

if e.Arg(1).Type() == parser.EtString {
Expand Down
Loading

0 comments on commit 3bd2c2b

Please sign in to comment.