-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetric.go
160 lines (136 loc) · 3.14 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package statsd
import (
"fmt"
"math"
)
type MetricType uint8
const (
MetricTypeGauge = iota
MetricTypeCount
MetricTypeTiming
)
type FieldType uint8
const (
FieldTypeString = iota
FieldTypeInt64
FieldTypeInt32
FieldTypeInt16
FieldTypeInt8
FieldTypeUint64
FieldTypeUint32
FieldTypeUint16
FieldTypeUint8
FieldTypeFloat32
FieldTypeFloat64
)
type Field struct {
Type FieldType
Int int64
Str string
}
func (f Field) appendTo(b *buf) {
switch f.Type {
case FieldTypeString:
b.AppendString(f.Str)
case FieldTypeFloat64:
b.AppendFloat64(math.Float64frombits(uint64(f.Int)))
case FieldTypeFloat32:
b.AppendFloat32(math.Float32frombits(uint32(f.Int)))
case FieldTypeInt8:
b.AppendInt8(int8(f.Int))
case FieldTypeInt16:
b.AppendInt16(int16(f.Int))
case FieldTypeInt32:
b.AppendInt32(int32(f.Int))
case FieldTypeInt64:
b.AppendInt64(f.Int)
case FieldTypeUint8:
b.AppendUint8(uint8(f.Int))
case FieldTypeUint16:
b.AppendUint16(uint16(f.Int))
case FieldTypeUint32:
b.AppendUint32(uint32(f.Int))
case FieldTypeUint64:
b.AppendUint64(uint64(f.Int))
default:
panic(fmt.Sprintf("unknown field type: %v", f.Type))
}
}
func String(val string) Field {
return Field{Type: FieldTypeString, Str: val}
}
func Int64(val int64) Field {
return Field{Type: FieldTypeInt64, Int: val}
}
func Int32(val int32) Field {
return Field{Type: FieldTypeInt32, Int: int64(val)}
}
func Int16(val int16) Field {
return Field{Type: FieldTypeInt16, Int: int64(val)}
}
func Int8(val int8) Field {
return Field{Type: FieldTypeInt8, Int: int64(val)}
}
func Uint64(val uint64) Field {
return Field{Type: FieldTypeUint64, Int: int64(val)}
}
func Uint32(val uint32) Field {
return Field{Type: FieldTypeUint32, Int: int64(val)}
}
func Uint16(val uint16) Field {
return Field{Type: FieldTypeUint16, Int: int64(val)}
}
func Uint8(val uint8) Field {
return Field{Type: FieldTypeUint8, Int: int64(val)}
}
func Float32(val float32) Field {
return Field{Type: FieldTypeFloat32, Int: int64(math.Float32bits(val))}
}
func Float64(val float64) Field {
return Field{Type: FieldTypeFloat64, Int: int64(math.Float64bits(val))}
}
func encode(typ MetricType, val Field, prefix string, hostname string, bucket []Field) *buf {
n := len(bucket)
if n == 0 {
return nil
}
b := getBuf()
if prefix != "" {
b.AppendString(prefix)
b.AppendString(".")
}
if hostname != "" {
b.AppendString(hostname)
b.AppendString(".")
}
last := n - 1
for i := range bucket {
bucket[i].appendTo(b)
if i < last {
b.AppendString(".")
}
}
b.AppendString(":")
val.appendTo(b)
b.AppendString("|")
switch typ {
case MetricTypeGauge:
b.AppendString("g\n")
case MetricTypeCount:
b.AppendString("c\n")
case MetricTypeTiming:
b.AppendString("ms\n")
default:
panic(fmt.Sprintf("unknown field type: %v", typ))
}
return b
}
func encodeTpl(typ MetricType, val Field, prefix string, hostname string, template string, fmtArgs []interface{}) *buf {
msg := template
if msg == "" && len(fmtArgs) > 0 {
msg = fmt.Sprint(fmtArgs...)
} else if msg != "" && len(fmtArgs) > 0 {
msg = fmt.Sprintf(template, fmtArgs...)
}
return encode(typ, val, prefix, hostname, []Field{String(msg)})
}