-
Notifications
You must be signed in to change notification settings - Fork 402
/
sanitizer.go
47 lines (38 loc) · 1.04 KB
/
sanitizer.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"bytes"
"strings"
"time"
"unicode"
)
// Sanitizer is a MetricDest that replaces nonalphanumeric characters with
// underscores.
type Sanitizer struct {
dest MetricDest
}
// NewSanitizer creates a Sanitizer that sends sanitized metrics to dest.
func NewSanitizer(dest MetricDest) *Sanitizer { return &Sanitizer{dest: dest} }
// Metric implements MetricDest
func (s *Sanitizer) Metric(application, instance string, key []byte, val float64, ts time.Time) error {
return s.dest.Metric(sanitize(application), sanitize(instance), sanitizeb(key), val, ts)
}
func sanitize(val string) string {
return strings.Replace(strings.Map(safechar, val), "..", ".", -1)
}
func sanitizeb(val []byte) []byte {
return bytes.Replace(bytes.Map(safechar, val), []byte(".."), []byte("."), -1)
}
func safechar(r rune) rune {
if unicode.IsLetter(r) || unicode.IsNumber(r) {
return r
}
switch r {
case '/':
return '.'
case '.', '-':
return r
}
return '_'
}