This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
func_aliassub.go
58 lines (48 loc) · 1.37 KB
/
func_aliassub.go
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
package expr
import (
"regexp"
"github.com/grafana/metrictank/api/models"
)
var groupPython = regexp.MustCompile(`\\(\d+)`)
type FuncAliasSub struct {
in GraphiteFunc
search *regexp.Regexp
replace string
}
func NewAliasSub() GraphiteFunc {
return &FuncAliasSub{}
}
func (s *FuncAliasSub) Signature() ([]Arg, []Arg) {
return []Arg{
ArgSeriesList{val: &s.in},
ArgRegex{key: "search", val: &s.search},
ArgString{key: "replace", val: &s.replace},
}, []Arg{ArgSeries{}}
}
func (s *FuncAliasSub) Context(context Context) Context {
return context
}
func (s *FuncAliasSub) Exec(dataMap DataMap) ([]models.Series, error) {
// support native graphite (python) groups like \3 by turning them into ${3}
replace := groupPython.ReplaceAllString(s.replace, "$${$1}")
series, err := s.in.Exec(dataMap)
if err != nil {
return nil, err
}
out := make([]models.Series, 0, len(series))
for _, serie := range series {
// TODO - graphite doesn't attempt to extract the
// metric/expression from the series. MT probably shouldn't either.
// This will almost certainly break some dashboards
metric := extractMetric(serie.Target)
if metric == "" {
metric = serie.Target
}
name := s.search.ReplaceAllString(metric, replace)
serie.Target = name
serie.QueryPatt = name
serie.Tags = serie.CopyTagsWith("name", name)
out = append(out, serie)
}
return out, err
}