forked from grafana-cold-storage/metrictank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc_aliasbynode.go
More file actions
64 lines (57 loc) · 1.4 KB
/
Copy pathfunc_aliasbynode.go
File metadata and controls
64 lines (57 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package expr
import (
"strings"
"github.com/grafana/metrictank/api/models"
)
type FuncAliasByNode struct {
in GraphiteFunc
nodes []expr
}
func NewAliasByNode() GraphiteFunc {
return &FuncAliasByNode{}
}
func (s *FuncAliasByNode) Signature() ([]Arg, []Arg) {
return []Arg{
ArgSeriesList{val: &s.in},
ArgStringsOrInts{val: &s.nodes},
}, []Arg{ArgSeries{}}
}
func (s *FuncAliasByNode) Context(context Context) Context {
return context
}
func (s *FuncAliasByNode) Exec(cache map[Req][]models.Series) ([]models.Series, error) {
series, err := s.in.Exec(cache)
if err != nil {
return nil, err
}
for i, serie := range series {
// Extract metric may not find a target if `seriesByTag` was used.
// If so, then we can try to grab the "name" tag.
metric := extractMetric(serie.Target)
if len(metric) == 0 {
metric = serie.Tags["name"]
}
// Trim off tags (if they are there) and split on '.'
parts := strings.Split(strings.SplitN(metric, ";", 2)[0], ".")
var name []string
for _, n := range s.nodes {
if n.etype == etInt {
idx := int(n.int)
if idx < 0 {
idx += len(parts)
}
if idx >= len(parts) || idx < 0 {
continue
}
name = append(name, parts[idx])
} else if n.etype == etString {
s := n.str
name = append(name, serie.Tags[s])
}
}
n := strings.Join(name, ".")
series[i].Target = n
series[i].QueryPatt = n
}
return series, nil
}