normalizer.go 3.0 KB

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