blob: 148d84b3a1a6c488dfbe9b7bbcc71b991fc7c20a [file] [log] [blame]
Girish Gowdra631ef3d2020-06-15 10:45:52 -07001// Copyright (c) 2017 Uber Technologies, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rpcmetrics
16
17// NameNormalizer is used to convert the endpoint names to strings
18// that can be safely used as tags in the metrics.
19type NameNormalizer interface {
20 Normalize(name string) string
21}
22
23// DefaultNameNormalizer converts endpoint names so that they contain only characters
24// from the safe charset [a-zA-Z0-9-./_]. All other characters are replaced with '-'.
25var DefaultNameNormalizer = &SimpleNameNormalizer{
26 SafeSets: []SafeCharacterSet{
27 &Range{From: 'a', To: 'z'},
28 &Range{From: 'A', To: 'Z'},
29 &Range{From: '0', To: '9'},
30 &Char{'-'},
31 &Char{'_'},
32 &Char{'/'},
33 &Char{'.'},
34 },
35 Replacement: '-',
36}
37
38// SimpleNameNormalizer uses a set of safe character sets.
39type SimpleNameNormalizer struct {
40 SafeSets []SafeCharacterSet
41 Replacement byte
42}
43
44// SafeCharacterSet determines if the given character is "safe"
45type SafeCharacterSet interface {
46 IsSafe(c byte) bool
47}
48
49// Range implements SafeCharacterSet
50type Range struct {
51 From, To byte
52}
53
54// IsSafe implements SafeCharacterSet
55func (r *Range) IsSafe(c byte) bool {
56 return c >= r.From && c <= r.To
57}
58
59// Char implements SafeCharacterSet
60type Char struct {
61 Val byte
62}
63
64// IsSafe implements SafeCharacterSet
65func (ch *Char) IsSafe(c byte) bool {
66 return c == ch.Val
67}
68
69// Normalize checks each character in the string against SafeSets,
70// and if it's not safe substitutes it with Replacement.
71func (n *SimpleNameNormalizer) Normalize(name string) string {
72 var retMe []byte
73 nameBytes := []byte(name)
74 for i, b := range nameBytes {
75 if n.safeByte(b) {
76 if retMe != nil {
77 retMe[i] = b
78 }
79 } else {
80 if retMe == nil {
81 retMe = make([]byte, len(nameBytes))
82 copy(retMe[0:i], nameBytes[0:i])
83 }
84 retMe[i] = n.Replacement
85 }
86 }
87 if retMe == nil {
88 return name
89 }
90 return string(retMe)
91}
92
93// safeByte checks if b against all safe charsets.
94func (n *SimpleNameNormalizer) safeByte(b byte) bool {
95 for i := range n.SafeSets {
96 if n.SafeSets[i].IsSafe(b) {
97 return true
98 }
99 }
100 return false
101}