normalizer.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // 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. package rpcmetrics
  15. // NameNormalizer is used to convert the endpoint names to strings
  16. // that can be safely used as tags in the metrics.
  17. type NameNormalizer interface {
  18. Normalize(name string) string
  19. }
  20. // DefaultNameNormalizer converts endpoint names so that they contain only characters
  21. // from the safe charset [a-zA-Z0-9-./_]. All other characters are replaced with '-'.
  22. var DefaultNameNormalizer = &SimpleNameNormalizer{
  23. SafeSets: []SafeCharacterSet{
  24. &Range{From: 'a', To: 'z'},
  25. &Range{From: 'A', To: 'Z'},
  26. &Range{From: '0', To: '9'},
  27. &Char{'-'},
  28. &Char{'_'},
  29. &Char{'/'},
  30. &Char{'.'},
  31. },
  32. Replacement: '-',
  33. }
  34. // SimpleNameNormalizer uses a set of safe character sets.
  35. type SimpleNameNormalizer struct {
  36. SafeSets []SafeCharacterSet
  37. Replacement byte
  38. }
  39. // SafeCharacterSet determines if the given character is "safe"
  40. type SafeCharacterSet interface {
  41. IsSafe(c byte) bool
  42. }
  43. // Range implements SafeCharacterSet
  44. type Range struct {
  45. From, To byte
  46. }
  47. // IsSafe implements SafeCharacterSet
  48. func (r *Range) IsSafe(c byte) bool {
  49. return c >= r.From && c <= r.To
  50. }
  51. // Char implements SafeCharacterSet
  52. type Char struct {
  53. Val byte
  54. }
  55. // IsSafe implements SafeCharacterSet
  56. func (ch *Char) IsSafe(c byte) bool {
  57. return c == ch.Val
  58. }
  59. // Normalize checks each character in the string against SafeSets,
  60. // and if it's not safe substitutes it with Replacement.
  61. func (n *SimpleNameNormalizer) Normalize(name string) string {
  62. var retMe []byte
  63. nameBytes := []byte(name)
  64. for i, b := range nameBytes {
  65. if n.safeByte(b) {
  66. if retMe != nil {
  67. retMe[i] = b
  68. }
  69. } else {
  70. if retMe == nil {
  71. retMe = make([]byte, len(nameBytes))
  72. copy(retMe[0:i], nameBytes[0:i])
  73. }
  74. retMe[i] = n.Replacement
  75. }
  76. }
  77. if retMe == nil {
  78. return name
  79. }
  80. return string(retMe)
  81. }
  82. // safeByte checks if b against all safe charsets.
  83. func (n *SimpleNameNormalizer) safeByte(b byte) bool {
  84. for i := range n.SafeSets {
  85. if n.SafeSets[i].IsSafe(b) {
  86. return true
  87. }
  88. }
  89. return false
  90. }