-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.go
79 lines (68 loc) · 1.7 KB
/
metric.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package metrics
import (
"bytes"
)
// Metric is an interface that represents system metric, provides platform-specific escaping
// and is faster than using fmt.Sprintf() as it usually uses bytes.Buffer
type Metric interface {
// Metric is a composer that creates a new metric by cloning the current metric and adding
// values to it, is useful for cases with multiple submetric of the same metric
// m := s.Metric("base")
// m.Metric("roundtrip")
// m.Metric("bytes")
Metric(p ...string) Metric
String() string
}
func NewMetric(prefix string, p ...string) Metric {
m := &metric{b: &bytes.Buffer{}}
if len(prefix) != 0 {
m.writeString(prefix, true)
}
m.writeStrings(p...)
return m
}
type metric struct {
b *bytes.Buffer
}
func (m *metric) Metric(p ...string) Metric {
n := m.makeCopy()
n.b.WriteRune('.')
n.writeStrings(p...)
return n
}
func (m *metric) String() string {
return m.b.String()
}
func (m *metric) makeCopy() *metric {
orig := m.b.Bytes()
new := make([]byte, len(orig))
copy(new, orig)
return &metric{b: bytes.NewBuffer(new)}
}
func (m *metric) writeString(s string, addDelimiter bool) {
writeEscaped(m.b, s, addDelimiter)
}
func (m *metric) writeStrings(p ...string) Metric {
for i, _ := range p {
m.writeString(p[i], i != len(p)-1)
}
return m
}
func escape(s string) string {
b := &bytes.Buffer{}
writeEscaped(b, s, false)
return b.String()
}
func writeEscaped(b *bytes.Buffer, s string, addDelim bool) {
b.Grow(len(s) + 1)
for i := 0; i < len(s); i++ {
if ('A' <= s[i] && s[i] <= 'Z') || ('a' <= s[i] && s[i] <= 'z') || ('0' <= s[i] && s[i] <= '9') || s[i] == '_' || s[i] == '.' {
b.WriteByte(s[i])
} else {
b.WriteByte('_')
}
}
if addDelim {
b.WriteRune('.')
}
}