github.com/m3db/m3@v1.5.0/src/query/graphite/common/aliasing.go (about) 1 // Copyright (c) 2019 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package common 22 23 import ( 24 "fmt" 25 "regexp" 26 "strconv" 27 28 "github.com/m3db/m3/src/query/graphite/ts" 29 "github.com/m3db/m3/src/x/errors" 30 ) 31 32 var ( 33 backReferenceRe = regexp.MustCompile(`\\\d+`) 34 ) 35 36 // Alias takes one metric or a wildcard seriesList and a string in quotes. 37 // Prints the string instead of the metric name in the legend. 38 func Alias(_ *Context, series ts.SeriesList, a string) (ts.SeriesList, error) { 39 renamed := make([]*ts.Series, series.Len()) 40 for i := range series.Values { 41 renamed[i] = series.Values[i].RenamedTo(a) 42 } 43 series.Values = renamed 44 return series, nil 45 } 46 47 // AliasSub runs series names through a regex search/replace. 48 func AliasSub(_ *Context, input ts.SeriesList, search, replace string) (ts.SeriesList, error) { 49 regex, err := regexp.Compile(search) 50 if err != nil { 51 return ts.NewSeriesList(), err 52 } 53 54 output := make([]*ts.Series, input.Len()) 55 for idx, series := range input.Values { 56 name := series.Name() 57 if submatches := regex.FindStringSubmatch(name); submatches == nil { 58 // if the pattern doesn't match, we don't change the series name. 59 output[idx] = series 60 } else { 61 // go regexp package doesn't support back-references, so we need to work around it. 62 newName := regex.ReplaceAllString(name, replace) 63 newName = backReferenceRe.ReplaceAllStringFunc(newName, func(matched string) string { 64 index, retErr := strconv.Atoi(matched[1:]) 65 if retErr != nil { 66 err = retErr 67 return "" 68 } 69 if index >= len(submatches) { 70 err = errors.NewInvalidParamsError(fmt.Errorf("invalid group reference in %s", replace)) 71 return "" 72 } 73 return submatches[index] 74 }) 75 if err != nil { 76 return ts.NewSeriesList(), err 77 } 78 output[idx] = series.RenamedTo(newName) 79 } 80 } 81 82 input.Values = output 83 return input, nil 84 }